//用户设置矩形区域颜色(有的图片白色居多,可能需要别的颜色) private void pictureBox7_Click(object sender, EventArgs e) { //修改背景颜色 ColorDialog cd = new System.Windows.Forms.ColorDialog(); cd.ShowDialog();//显示颜色选取框 //这里可以不选取颜色,因为默认为白色 pictureBox7.BackColor = cd.Color; //显示用户选取的颜色 paint.pen = new Pen(cd.Color, 2); //定义画笔 if (cluster.iscluster == true) //如果已经绘制矩形,则直接重新绘制,否则什么都不做等着绘制 { Graphics g = pictureBox2.CreateGraphics(); PicturePaint.Paint(g); } }
/// <summary> /// 鼠标移动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox2_MouseMove(object sender, MouseEventArgs e) { //检测鼠标左键是否按下 if (e.Button == MouseButtons.Left) { if ((paint.flag == 1) || (paint.flag == 3))//标志位为1,说明鼠标按下时的位置有效(处于右下角),执行的是拖动矩形的大小。标志位为3,执行的是鼠标位于矩形下边界时对矩形进行操作 { //预判当此次操作过后是否矩形将会超出区域 float new_Height = e.Y - paint.Rec_y; float new_Width = Variable.user_scale * new_Height; //计算新的边界区域 float newRight = paint.Rec_x + new_Width; float newBottom = paint.Rec_y + new_Height; //判断鼠标的位置有没有到图片框外面(没有到外面才执行) if ((newRight > paint.Rec_x + 2 * paint.threshold) && (newRight < (float)(pictureBox2.Width - paint.threshold)) && (newBottom > paint.Rec_y + 2 * paint.threshold) && (newBottom < (float)(pictureBox2.Height - 2 * paint.threshold))) { pictureBox2.Refresh(); //清除之前绘制的图像 Graphics g = pictureBox2.CreateGraphics(); //为了在鼠标移动过程中矩形都有重新绘制,故每次移动鼠标时矩形都重新绘制 paint.Rec_height = new_Height; //对矩形的高度进行更新 paint.Rec_width = new_Width; //新的高度对应新的宽度 PicturePaint.Paint(g); //重新绘制矩形 } } else if (paint.flag == 2)//按下时鼠标处于矩形中间,则执行的是移动矩形 { Graphics g = pictureBox2.CreateGraphics(); //判断移动前鼠标与左上角的相对位置 float relativeX = mouse.formX - paint.Rec_x; float relativeY = mouse.formY - paint.Rec_y; //判断新位置坐标 是否超出边界 float newX = e.X - relativeX; float newY = e.Y - relativeY; if ((newX > paint.threshold) && (newX + paint.Rec_width) < (pictureBox2.Width - paint.threshold) && (newY > paint.threshold) && (newY + paint.Rec_height) < (pictureBox2.Height - paint.threshold)) { pictureBox2.Refresh();//清除之前绘制的图像 //把全局变量中的鼠标位置更新为现在的鼠标位置 mouse.formX = e.X; mouse.formY = e.Y; //计算矩形的新位置坐标 paint.Rec_x = newX; paint.Rec_y = newY; PicturePaint.Paint(g); } } //以x坐标为准进行矩形的放大缩小 else if (paint.flag == 4) { //计算改变后的矩形宽度和高度 float new_Width = e.X - paint.Rec_x; float new_Height = new_Width / Variable.user_scale; //计算新的边界区域 float newRight = paint.Rec_x + new_Width; float newBottom = paint.Rec_y + new_Height; //判断矩形边界的位置有没有到图片框外面(没有到外面才执行) if ((newRight > paint.Rec_x + 2 * paint.threshold) && (newRight < (float)(pictureBox2.Width - paint.threshold)) && (newBottom > paint.Rec_y + 2 * paint.threshold) && (newBottom < (float)(pictureBox2.Height - 2 * paint.threshold))) { pictureBox2.Refresh(); //清除之前绘制的图像 Graphics g = pictureBox2.CreateGraphics(); //为了在鼠标移动过程中矩形都有重新绘制,故每次移动鼠标时矩形都重新绘制 paint.Rec_width = e.X - paint.Rec_x; //对矩形的宽度进行更新 paint.Rec_height = paint.Rec_width / Variable.user_scale; //新的宽度对应新的高度 PicturePaint.Paint(g); //重新绘制矩形 } } } else//鼠标没有按下(判断鼠标所处的位置,对鼠标指针进行变换) { //获取鼠标现在所处的位置,看是否处于阈值内 //计算右下角坐标的阈值框的四个值 float leftX = paint.Pos[3].x - paint.threshold; float rightX = paint.Pos[3].x + paint.threshold; float topY = paint.Pos[3].y - paint.threshold; float bottomY = paint.Pos[3].y + paint.threshold; if ((e.X > leftX) && (e.X < rightX) && (e.Y > topY) && (e.Y < bottomY))//处于右下角阈值内(执行拖动),进行操作 { this.pictureBox2.Cursor = Cursors.SizeNWSE; } //鼠标位置处于矩形中间(执行移动) else if ((e.X > paint.Pos[0].x + 2 * paint.threshold) && (e.X < paint.Pos[2].x - 2 * paint.threshold) && (e.Y > paint.Pos[0].y + 2 * paint.threshold) && (e.Y < paint.Pos[1].y - 2 * paint.threshold)) { this.pictureBox2.Cursor = Cursors.SizeAll;//改变鼠标样式为“移动” } //鼠标处于矩形下边界 else if ((e.Y > (paint.Pos[1].y - paint.threshold)) && (e.Y < (paint.Pos[1].y + paint.threshold))) { this.pictureBox2.Cursor = Cursors.SizeNS;//改变鼠标形状为“南北”移动 } //鼠标处于矩形右边界 else if ((e.X > (paint.Pos[2].x - paint.threshold)) && (e.X < (paint.Pos[2].x + paint.threshold))) { this.pictureBox2.Cursor = Cursors.SizeWE;//西东图标 } else { this.pictureBox2.Cursor = Cursors.Default;//如果鼠标没有处于任何一个区域内,则鼠标为默认指针 } } }
/// <summary> /// 用户在“篇幅”处输入长,图片框中可以由用户自己选择需要聚类的图片区域 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { if (pic.isPic == true) //图片已经选择,矩形选择才有意义 { pictureBox2.Refresh(); //清除之前绘制的图像 //将pictureBox2设为画布,在该控件上绘制矩形 Graphics g = pictureBox2.CreateGraphics(); //计算绘制的位置坐标(初始位置位于中心)--------------------------------------- //---------------------------------Begin------------------------------------- //先计算PictureBox2中图片的大小,并找到中心 int picBox2Pic_wid = pictureBox2.Width; int picBox2Pic_hei = pictureBox2.Height; //获取用户输入的数据:长为textBox7.Text,宽为1.4米,计算比例,求一半的值 //更新用户输入的数据(更新的是全局,后期在绘制时也可以用) Variable.user_hei = float.Parse(textBox7.Text); //string转float Variable.user_wid = float.Parse("1.4"); //string转float Variable.user_scale = Variable.user_wid / Variable.user_hei; //比例为 宽度/高度 //更新要显示的矩形的大小 paint.Rec_height = Variable.user_hei; paint.Rec_width = Variable.user_wid; //判断这两个值谁大谁小,大的值放大到pictureBox2的二分之一 if (Variable.user_wid > Variable.user_hei) //宽度大 { paint.Rec_width = picBox2Pic_wid / 2; //把宽度放大到1/2 paint.Rec_height = paint.Rec_width / Variable.user_scale; //高度等比例放大 } else//高度大或相同 { paint.Rec_height = picBox2Pic_hei / 2; //把高度放大到1/2 paint.Rec_width = paint.Rec_height * Variable.user_scale; //高度等比例放大 } //计算绘制的位置坐标(初始位置位于中心)--------------------------------------- //----------------------------------End----------------------------------- //更新要显示的矩形的坐标------------Bagin----------------------------------------- paint.Rec_x = picBox2Pic_wid / 2 - paint.Rec_width / 2; paint.Rec_y = picBox2Pic_hei / 2 - paint.Rec_height / 2; //更新要显示的矩形的坐标------------End------------------------------------------- //绘制 PicturePaint.Paint(g); cluster.iscluster = true; //显示矩形,如果矩形未显示,则不能进行聚类 cluster.clusterFinished = false; //图片已选择、重新选择了区域 } else//提示选择图像 { MessageBox.Show("图像未选择,此时选择矩形区域将无意义!", "提示", MessageBoxButtons.OK); } //更新聚类结果 pictureBox3.BackColor = Color.White; pictureBox4.BackColor = Color.White; pictureBox5.BackColor = Color.White; pictureBox6.BackColor = Color.White; }