嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 1 元微信扫码支付:1 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
C#鼠标改变ROI窗口大小
/// <summary>
/// 改变鼠标样式
/// </summary>
private void ChangeCursor(Point loc, bool DrawOrMove)
{
if (DrawOrMove)
{
if (m_OperateRectangle[0].Contains(loc) || m_OperateRectangle[7].Contains(loc))
this.Cursor = Cursors.SizeNWSE;
else if (m_OperateRectangle[1].Contains(loc) || m_OperateRectangle[6].Contains(loc))
this.Cursor = Cursors.SizeNS;
else if (m_OperateRectangle[2].Contains(loc) || m_OperateRectangle[5].Contains(loc))
this.Cursor = Cursors.SizeNESW;
else if (m_OperateRectangle[3].Contains(loc) || m_OperateRectangle[4].Contains(loc))
this.Cursor = Cursors.SizeWE;
else if (CaptureRectangle.Contains(loc))
this.Cursor = Cursors.SizeAll;
else
this.Cursor = Cursors.Default;
}
}
/// <summary>
/// MouseState=1:当鼠标双击ROI之外的图像区域时候,保留已存在ROI区域;
/// MouseState=2:根据鼠标在8个矩形框位置来确定是改变宽度还是高度;
/// </summary>
private void KeepMouseState(int MouseState, MouseEventArgs e)
{
if (m_OperateRectangle[0].Contains(e.Location))
{
MouseMovePoint.X = this.CaptureRectangle.Right;
MouseMovePoint.Y = this.CaptureRectangle.Bottom;
}
else if (m_OperateRectangle[1].Contains(e.Location))
{
MouseMovePoint.Y = this.CaptureRectangle.Bottom;
BlockWidth = true;//锁定ROI宽度
}
else if (m_OperateRectangle[2].Contains(e.Location))
{
MouseMovePoint.X = this.CaptureRectangle.X;
MouseMovePoint.Y = this.CaptureRectangle.Bottom;
}
else if (m_OperateRectangle[3].Contains(e.Location))
{
MouseMovePoint.X = this.CaptureRectangle.Right;
BlockHeight = true;//锁定ROI高度
}
else if (m_OperateRectangle[4].Contains(e.Location))
{
MouseMovePoint.X = this.CaptureRectangle.X;
BlockHeight = true;//锁定ROI高度
}
else if (m_OperateRectangle[5].Contains(e.Location))
{
MouseMovePoint.X = this.CaptureRectangle.Right;
MouseMovePoint.Y = this.CaptureRectangle.Y;
}
else if (m_OperateRectangle[6].Contains(e.Location))
{
MouseMovePoint.Y = this.CaptureRectangle.Y;
BlockWidth = true;//锁定ROI宽度
}
else if (m_OperateRectangle[7].Contains(e.Location))
{
MouseMovePoint = this.CaptureRectangle.Location;
}else if(this.CaptureRectangle.Contains(e.Location))
{
}
else
{
if (MouseState == 1)//当在ROI矩形外按下鼠标,保留现有的ROI,防止ROI被刷掉
{
StartDraw = false;
}
}
}
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
StartDraw = true;
BlockWidth = false;
BlockHeight = false;
KeepMouseState(1,e);
MouseDownPoint = new Point(e.X, e.Y);
//通过鼠标移动坐标位置来判断是改变ROI大小还是移动ROI操作
if (FinishedDraw)
{
if (this.CaptureRectangle.Contains(e.Location))
{
IsMoving = true;
}
else
{
IsMoving = false;
}
}
}
}
/// <summary>
/// 鼠标弹起事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
StartDraw = false;
TempStart_Point = CaptureRectangle.Location;//记录下鼠标弹起时,ROI当前位置
CutImage();
}
}
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMouseMove(object sender, MouseEventArgs e)
{
ChangeCursor(e.Location, true);
if (StartDraw)
{
KeepMouseState(2, e);
}
if (StartDraw)
{
if (IsMoving)
{
this.CaptureRectangle.X = TempStart_Point.X e.X - MouseDownPoint.X;
this.CaptureRectangle.Y = TempStart_Point.Y e.Y - MouseDownPoint.Y;
if (this.CaptureRectangle.X < 0) this.CaptureRectangle.X = 0;
if (this.CaptureRectangle.Y < 0) this.CaptureRectangle.Y = 0;
if (this.CaptureRectangle.Right > OriginImage.Width) this.CaptureRectangle.X = OriginImage.Width - this.CaptureRectangle.Width - 1;
if (this.CaptureRectangle.Bottom > OriginImage.Height) this.CaptureRectangle.Y = OriginImage.Height - this.CaptureRectangle.Height - 1;
}
else
{
if (Math.Abs(e.X - MouseMovePoint.X) > 1 || Math.Abs(e.Y - MouseMovePoint.Y) > 1)
{
if ((e.X >= 0 && e.X <= this.OriginImage.Width) && (e.Y >= 0 && e.Y <= this.OriginImage.Height))
{
//当前坐标在图像区域
}
else
{
//当前坐标不在图像区域
return;
}
if (!BlockWidth)
{
CaptureRectangle.X = MouseMovePoint.X - e.X < 0 ? MouseMovePoint.X : e.X;//以CaptureRectangle的Left或Right为正负分界线,MouseMovePoint.X - e.X < 0:正向方向改变大小,MouseMovePoint.X - e.X > 0:负向方向改变大小
CaptureRectangle.Width = Math.Abs(MouseMovePoint.X - e.X);
}
if (!BlockHeight)
{
CaptureRectangle.Y = MouseMovePoint.Y - e.Y < 0 ? MouseMovePoint.Y : e.Y;//以CaptureRectangle的Top或Bttom为正负分界线,MouseMovePoint.Y - e.Y < 0:正向方向改变大小,MouseMovePoint.Y - e.Y > 0:负向方向改变大小
CaptureRectangle.Height = Math.Abs(MouseMovePoint.Y - e.Y);
}
}
}
//使画布无效,从而执行OnPaint()
this.DisplayImage_pictureBox.Invalidate();
}
}
/// <summary>
/// 控件Paint事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPaint(object sender, PaintEventArgs e)
{
if (FinishedDraw)
{
Pen p = new Pen(Color.YellowGreen, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawRectangle(p, CaptureRectangle);
//绘制8个小矩形框
DrawOperationBox(e.Graphics);
}
}
/// <summary>
/// 绘制8个小矩形框
/// </summary>
/// <param name="g"></param>
protected virtual void DrawOperationBox(Graphics g)
{
m_OperateRectangle[0].X = this.CaptureRectangle.X - 5;
m_OperateRectangle[0].Y = this.CaptureRectangle.Y - 5;
m_OperateRectangle[1].X = this.CaptureRectangle.X this.CaptureRectangle.Width / 2 - 5;
m_OperateRectangle[1].Y = m_OperateRectangle[2].Y = this.CaptureRectangle.Y - 7;
m_OperateRectangle[2].X = this.CaptureRectangle.Right - 5;
m_OperateRectangle[2].Y = this.CaptureRectangle.Y - 5;
m_OperateRectangle[3].X = this.CaptureRectangle.X - 7;
m_OperateRectangle[3].Y = this.CaptureRectangle.Y this.CaptureRectangle.Height / 2 - 5;
m_OperateRectangle[4].X = this.CaptureRectangle.Right - 2;
m_OperateRectangle[4].Y = this.CaptureRectangle.Y this.CaptureRectangle.Height / 2 - 5;
m_OperateRectangle[5].X = this.CaptureRectangle.X - 5;
m_OperateRectangle[5].Y = this.CaptureRectangle.Bottom - 5;
m_OperateRectangle[6].X = this.CaptureRectangle.X this.CaptureRectangle.Width / 2 - 5;
m_OperateRectangle[6].Y = this.CaptureRectangle.Bottom - 2;
m_OperateRectangle[7].X = this.CaptureRectangle.Right - 5;
m_OperateRectangle[7].Y = this.CaptureRectangle.Bottom - 5;
if (this.CaptureRectangle.Width > 10 && this.CaptureRectangle.Height > 10)
{
SolidBrushObject.Color = Color.YellowGreen;
foreach (Rectangle rect in m_OperateRectangle)
{
g.FillRectangle(SolidBrushObject, rect);
}
}
}
/// <summary>
/// 画矩形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDrawRectangle(object sender, EventArgs e)
{
CaptureRectangle.X = OriginImage.Width/2;
CaptureRectangle.Width = OriginImage.Width / 4;
CaptureRectangle.Y = OriginImage.Height / 2;
CaptureRectangle.Height = OriginImage.Height / 4;
TempStart_Point = CaptureRectangle.Location;
FinishedDraw = true;
IsMoving = false;
this.DisplayImage_pictureBox.Invalidate();
}
/// <summary>
/// 剪切图像
/// </summary>
private Bitmap CutImage()
{
Bitmap CutBitmap = new Bitmap(CaptureRectangle.Width, CaptureRectangle.Height);
Graphics GraphicsObject = Graphics.FromImage(CutBitmap);
GraphicsObject.DrawImage(OriginImage, new Rectangle(0, 0, CaptureRectangle.Width, CaptureRectangle.Height), CaptureRectangle, GraphicsUnit.Pixel);
this.CutImage_pictureBox.Image = CutBitmap;
this.CutImage_pictureBox.Width = CutBitmap.Width;
this.CutImage_pictureBox.Height = CutBitmap.Height;
GraphicsObject.Dispose();
return CutBitmap;
}