C#图片剪裁例子_C#裁剪图片的特殊区域例子
2014-09-16 13:33:58 By: shinyuu
首先、我们要解释一下为什么我们需要这个方法来裁剪图像、我准备编码实现一个简单的应用程序来剪裁图片
在大项目中、我们需要关注图像的某些区域、所以当我开始研究这个要求、如何做到这一点、我也只见过矩形
圆形或正方形区域的裁切图像、如果我们要剪裁的区域不是矩形、方形、圆形等、我们要怎么办?
我们应该能够选择图像中的所有区域的点、你可以裁剪特定的影像区域
红色是要裁剪的区域
源代码
代码被分隔两个主要部分、其中之一是选择区域、我们使用pictureBox1_Mouse_Click()事件和pictureBox1_MouseMove()事件
MouseMove事件提供我们坐标值、当我们拖动鼠标光标置于包含我们的形象的图片框
鼠标点击事件提供我们添加多边形点、点击鼠标左键、我们可以单击鼠标右键取消、我们需要一些临时变量绘制多边形
//to choose points of crop region private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Left: temp_point = new System.Drawing.Point(e.X, e.Y); temp_count = polygonPoints.Count; polygonPoints.Add(new System.Drawing.Point(e.X, e.Y)); if (polygonPoints.Count > 1) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); temp_im = bmp.Clone(rect, PixelFormat.Format24bppRgb); this.DrawLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]); } break; // in this point we can undo easily, with pushing mouse right click case MouseButtons.Right: if (polygonPoints.Count > 0) { undo(); } break; } pictureBox1.Image = bmp; }第二个主要的部分是crop()的图像的、我们裁剪区域的方法
我们填充矩形第一创建黑色图像具有相同的尺寸与原始图像和填入“1”的RGB值的多边形的内部、
我们将两个图像使用Cv.Mul(CvArr src1,CvArr src2, CvArr dst, double scale)方法
private void crop() { timer1.Stop(); IplImage accc = Cv.CreateImage(new CvSize(bmp.Width, bmp.Height), BitDepth.U8, 3); Graphics Ga = Graphics.FromImage(bmp); //the black image Ga.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bmp.Width, bmp.Height)); //draw from the last point to first point Ga.DrawLine(new Pen(Color.Red, 3), polygonPoints[polygonPoints.Count - 1], polygonPoints[0]); //all of the rgb values are being set 1 inside the polygon SolidBrush Brush = new SolidBrush(Color.FromArgb(1, 1, 1)); //we have to prepare one mask of Multiplying operation for cropping region G.FillClosedCurve(Brush, polygonPoints.ToArray()); Cv.Mul(BitmapToIplImage(Source), BitmapToIplImage(bmp), accc, 1); computecrop(); //just show cropped region part of image croplast = accc.ToBitmap().Clone(rectcrop, Source.PixelFormat); pictureBox2.Image = croplast; // crop region of image }当然、我们需要一个方法、从位图转换为IplImage结构、这个方法是BitmapToIplImage(Bitmap src)
//we have to conversion from bitmap to IplImage to //use OpenCvSharp methods public static IplImage BitmapToIplImage(Bitmap bitmap) { IplImage tmp, tmp2; Rectangle bRect = new Rectangle(new System.Drawing.Point(0, 0), new Size((int)bitmap.Width, (int)bitmap.Height)); BitmapData bmData = bitmap.LockBits(bRect, ImageLockMode.ReadWrite, bitmap.PixelFormat); tmp = Cv.CreateImage(Cv.Size(bitmap.Width, bitmap.Height), BitDepth.U8, 3); tmp2 = Cv.CreateImage(Cv.Size(bitmap.Width, bitmap.Height), BitDepth.U8, 1); tmp.ImageData = bmData.Scan0; ; bitmap.UnlockBits(bmData); // Cv.CvtColor(tmp, tmp2, ColorConversion.RgbToGray); return tmp; }使用Graphics类的方法来创建蒙版对象
// we have refresh some assignments operators for //new image or cropping new region private void first_refresh_im() { Rectangle rect = new Rectangle(0, 0, resim.Width, resim.Height); resimi = resim.ToBitmap() ; bmp = resimi.Clone(rect, resimi.PixelFormat); Source = resimi.Clone(rect, resimi.PixelFormat); pictureBox1.Image = bmp; G = Graphics.FromImage(bmp); polygonPoints.Clear(); } // when we undo point of region, we have to create new //rectangular object with using ex-points // and we have to need new bmp Source object private void refresh_im() { Rectangle rect = new Rectangle(0, 0, resim.Width, resim.Height); bmp = resimi.Clone(rect, resimi.PixelFormat); Source = resimi.Clone(rect, resimi.PixelFormat); pictureBox1.Image = bmp; }最后给大家献上源代码链接: http://dwtedx.com/download.html?bdkey=s/1bnespOz 密码: 4se6
若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)
或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)
如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教
为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)
感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛
sssssssssss 2015-02-15 10:42:16 1 评 | 回复