GDI+ C# 在图片上画矩形
2013-03-22 17:52
585 查看
//定义全局变量
private int pointStartX, pointStartY, pointEndX, pointEndY
private Bitmap bitmapSource = null;
//初始化中
string strPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\22.jpg";
bitmapSource = new Bitmap(strPath);
//在MouseDown事件中记下起始点
pointStartX = e.X;
pointStartY = e.Y;
//C#中利用GDI+ ,在MouseMove事件中绘制矩形
int iWidth = e.X - pointStartX;
int iHeight = e.Y - pointStartY;
if (e.Button == MouseButtons.Left)
{
// 每次鼠标移动都拷贝原图bitmapSource,去除之前的留下的矩形
Bitmap bitmap = new Bitmap(bitmapSource, 500, 500);
Pen pen = new Pen(Color.Red);
Graphics gh = Graphics.FromImage(bitmap);
Rectangle rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight);
// 画矩形
gh.DrawRectangle(pen, rectNew);
// 显示在画板上
this.CreateGraphics().DrawImage(bitmap, 0, 0, 500, 500);
}
private int pointStartX, pointStartY, pointEndX, pointEndY
private Bitmap bitmapSource = null;
//初始化中
string strPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\22.jpg";
bitmapSource = new Bitmap(strPath);
//在MouseDown事件中记下起始点
pointStartX = e.X;
pointStartY = e.Y;
//C#中利用GDI+ ,在MouseMove事件中绘制矩形
int iWidth = e.X - pointStartX;
int iHeight = e.Y - pointStartY;
if (e.Button == MouseButtons.Left)
{
// 每次鼠标移动都拷贝原图bitmapSource,去除之前的留下的矩形
Bitmap bitmap = new Bitmap(bitmapSource, 500, 500);
Pen pen = new Pen(Color.Red);
Graphics gh = Graphics.FromImage(bitmap);
Rectangle rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight);
// 画矩形
gh.DrawRectangle(pen, rectNew);
// 显示在画板上
this.CreateGraphics().DrawImage(bitmap, 0, 0, 500, 500);
}
相关文章推荐
- C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域 C# 使用 GDI+ 画图
- C#利用GDI+给图片添加文字(文字自适应矩形区域)
- C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域
- c#裁剪图片(根据鼠标画的矩形裁剪图片)
- 简单的GDI+处理图片大小(C#代码)
- C#中基于GDI+(Graphics)图像处理系列之图片压缩优化
- c# 旋转图片 无GDI+一般性错误
- (原創) 如何将图片左右翻转? (.NET) (ASP.NET) (C#) (GDI+) (Image Processing)
- Windows程序(C# VS 2008)杂记之GDI+只能向右下奔跑的矩形
- (原創) 如何将图片以对角线做映射(Mirror)? (.NET) (ASP.NET) (C#) (GDI+) (Image Processing)
- C#中基于GDI+(Graphics)图像处理系列之文字或者图片水印(透明、任意角度旋转)
- C# GDI+ 绘制圆角实心矩形
- (原創) 如何将图片转成Binary Image? (.NET) (ASP.NET) (C#) (GDI+) (Image Processing)
- 怎样用 c# GDI+ 截取一张图片的一部分,然后再替换另一张图片的一部分
- 学习FotoVision 进行C# colorMatrix 对图片的处理 : 亮度调整 抓屏 翻转 随鼠标画矩形
- C#中GDI+导出excel (含图片, 数据)
- C#保存图片时提示“GDI+ 中发生一般性错误”
- C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果,滤波,图像截取) 对图片的处理 : 亮度调整 抓屏 翻转 随鼠标画矩形
- C#中基于GDI+(Graphics)图像处理系列之UEditor上传图片自动压缩优化(添加水印)
- C# GDI+ 利用 Rectangle GraphicsPath 判断 矩形或多边形 图形关系