控件取图片和矩形框 小测试

#region 获取相机图片
/// <summary>
/// 剪裁
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}

int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}

if (StartX + iWidth > w)
{
iWidth = w - StartX;
}

if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new System.Drawing.Rectangle(0, 0, iWidth, iHeight), new System.Drawing.Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
#endregion

 

#region 点击图片事件
bool MouseIsDown = false;
System.Drawing.Rectangle MouseRect = System.Drawing.Rectangle.Empty; //矩形(为鼠标画出矩形选区)

/// <summary>
/// 获取点温度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{
MouseIsDown = true;
DrawStart(sender, e.Location);
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{
if (MouseIsDown)
{
ResizeToRectangle(sender, e.Location);
}
}

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (pictureBox1.Image == null) return;

this.Capture = false;
Cursor.Clip = System.Drawing.Rectangle.Empty;
MouseIsDown = false;
DrawRectangle(sender);

if (MouseRect.Height < 2 && MouseRect.Width < 2)
{
pictureBox1.Refresh();
MouseRect = new Rectangle(MouseRect.X, MouseRect.Y, 2, 2);
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(Color.Black), MouseRect);
string temp = _Irservice.GetMaxTemp(MouseRect.X.ToString(), MouseRect.Y.ToString()).TempMax.ToString();
g.DrawString(temp + "℃", new Font("宋体", 8, FontStyle.Regular), Brushes.Black, MouseRect.X, MouseRect.Y, StringFormat.GenericDefault);
}
else {
pictureBox1.Refresh();
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(Color.Black), MouseRect);
WindowsFormsApplication1.Tools.IrService.Temp te = new IrService.Temp();
te = (WindowsFormsApplication1.Tools.IrService.Temp)(_Irservice.GetMaxTemp(MouseRect.X.ToString(), MouseRect.Y.ToString(), MouseRect.Width.ToString(), MouseRect.Height.ToString()));
g.DrawString(te.TempMax + "℃" + "\n\r" + "X:" + te.MaxX + "\n\r" + "Y:" + te.MaxY, new Font("宋体", 8, FontStyle.Regular), Brushes.Black, MouseRect.X + 2, MouseRect.Y + 2, StringFormat.GenericDefault);
}
MouseRect = System.Drawing.Rectangle.Empty;
}

private void ResizeToRectangle(object sender, System.Drawing.Point p)
{
DrawRectangle(sender);
MouseRect.Width = p.X - MouseRect.Left;
MouseRect.Height = p.Y - MouseRect.Top;
DrawRectangle(sender);
}
private void DrawRectangle(object sender)
{
System.Drawing.Rectangle rect = ((PictureBox)sender).RectangleToScreen(MouseRect);
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
}
private void DrawStart(object sender, System.Drawing.Point StartPoint)
{
((PictureBox)sender).Capture = true;
Cursor.Clip = ((PictureBox)sender).RectangleToScreen(((PictureBox)sender).Bounds);
MouseRect = new System.Drawing.Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
}
#endregion

posted @ 2017-02-28 11:30  pengde  阅读(443)  评论(0编辑  收藏  举报