首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#中的徒手图像裁剪

C#中的徒手图像裁剪
EN

Stack Overflow用户
提问于 2011-11-24 21:00:29
回答 3查看 1.2K关注 0票数 0

我是图像处理的新手。我想知道如何在C# windows窗体应用程序中进行徒手图像裁剪?首先,我想要在图像中绘制对象的边界,然后根据绘制的边界对对象进行裁剪。有什么方法可以做到这一点呢?

谢谢!

EN

Stack Overflow用户

发布于 2016-06-13 21:23:17

我已经编写了裁剪图像的代码。简单地说,我们需要两个图片盒。一个用于原始图像,另一个用于裁剪图像。然后看下面的代码。这些代码必须写在事件处理程序部分。

代码语言:javascript
运行
复制
int cropX, cropY, cropWidth, cropHeight;
        //here rectangle border pen color=red and size=2;
        Pen borderpen = new Pen(Color.Red, 2);
        System.Drawing.Image _orgImage;
        Bitmap crop;

        //fill the rectangle color =white
        SolidBrush rectbrush = new SolidBrush(Color.FromArgb(100, Color.White));

 private void pic_scan_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
                {
                    pic_scan.Refresh();
                    cropX = e.X;
                    cropY = e.Y;
                    Cursor = Cursors.Cross;
                }
                pic_scan.Refresh();
            }
            catch { }
        }
 private void pic_scan_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (pic_scan.Image == null)
                    return;

                if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
                {
                    pic_scan.Refresh();
                    cropWidth = e.X - cropX;
                    cropHeight = e.Y - cropY;
                }
                pic_scan.Refresh();
            }
            catch { }
        }

        private void pic_scan_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                Cursor = Cursors.Default;
                if (cropWidth < 1)
                {
                    return;
                }
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(cropX, cropY, cropWidth, cropHeight);
                Bitmap bit = new Bitmap(pic_scan.Image, pic_scan.Width, pic_scan.Height);
                crop = new Bitmap(cropWidth, cropHeight);
                Graphics gfx = Graphics.FromImage(crop);
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;//here add  System.Drawing.Drawing2D namespace;
                gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;//here add  System.Drawing.Drawing2D namespace;
                gfx.CompositingQuality = CompositingQuality.HighQuality;//here add  System.Drawing.Drawing2D namespace;
                gfx.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);

            }
            catch { }
        }

        private void pic_scan_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(cropX, cropY, cropWidth, cropHeight);
            Graphics gfx = e.Graphics;
            gfx.DrawRectangle(borderpen, rect);
            gfx.FillRectangle(rectbrush, rect);
        }

它起作用了。试试吧,享受它的免费编码。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8257497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档