如何使用Windows窗体中的图片框中的鼠标选择来裁剪图像 [英] How to crop image using mouse selection in picturebox in windows form

查看:104
本文介绍了如何使用Windows窗体中的图片框中的鼠标选择来裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

裁剪图像错误。我哪里错了?







  int  cropX; 
int cropY;
int cropWidth;
int cropHeight;
public Pen cropPen;

public DashStyle cropDashStyle = DashStyle.DashDot;
public bool Makeselection = false ;

< pre> private void button4_Click( object sender,EventArgs e)
{
Makeselection = true ;
button4.Enabled = true ;

Cursor = Cursors.Default;

尝试
{
如果(cropWidth < span class =code-keyword>< 1
{
返回;
}
Rectangle rect = new 矩形(cropX,cropY,cropWidth,cropHeight);
Bitmap OriginalImage = new 位图(pictureBox1.Image,pictureBox1.Width,pictureBox1.Height);
位图_img = 位图(cropWidth,cropHeight);
图形g = Graphics.FromImage(_img);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(OriginalImage, 0 0 ,rect,GraphicsUnit.Pixel);

pictureBox1.Image = _img;
pictureBox1.Width = _img.Width;
pictureBox1.Height = _img.Height;
}
catch (例外情况)
{

}
}

private void pictureBox1_MouseDown( object sender,MouseEventArgs e)
{

Cursor = Cursors.Default;
if (Makeselection)
{

试用
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
cropX = e.X;
cropY = e.Y;

cropPen = new 笔(Color.Red, 1 );
cropPen.DashStyle = DashStyle.DashDotDot;


}
pictureBox1.Refresh();
}
catch (例外情况)
{

}
}

}

private void pictureBox1_MouseUp( object sender,MouseEventArgs e)
{
if (Makeselection)
{
Cursor = Cursors.Default;
}
}

私有 void pictureBox1_MouseMove( object sender,MouseEventArgs e)
{
Cursor = Cursors.Default;
if (Makeselection)
{

试用
{
if (pictureBox1.Image == null
返回;


if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
cropWidth = e.X - cropX;
cropHeight = e.Y - cropY;
pictureBox1.CreateGraphics()。DrawRectangle(cropPen,cropX,cropY,cropWidth,cropHeight);
}



}
catch (例外情况)
{
// if(ex.Number == 5)
// return;
}
}

}





我的尝试:



裁剪图像是错误的。哪里我错了?

解决方案

可能它正在缩放。检查PictureBox设置,特别是 PictureBox .SizeMode属性(System.Windows.Forms)| Microsoft Docs [ ^ ] - 如果未设置为Normal,则您使用的鼠标坐标相对于Picturebox而不是它包含的图像。如果图像完全缩放,那么当你使用鼠标坐标时,它们与你剪裁的实际图片不匹配。



其他一些东西:

1)图形上下文是稀缺资源 - 您负责在创建它们时处置它们。如果不这样做,那么在GC调用之后很久就会出现内存不足错误。 使用块是最好的方法,因为它会在对象超出范围时自动处理,无论它如何退出块。

< pre lang =c#> 使用(图形g = Graphics.FromImage(_img))
{
g.InterpolationMode = System.Drawing。 Drawing2D.InterpolationMode.HighQualityBicubic;
...
}



2)为什么要创建这么多位图?你不需要他们中的一半!

3)永远不要吞下例外:

 catch(Exception ex)
{

}

当你这样做时,你会丢弃所有可能有助于你以后解决问题的信息,以及隐藏问题所以你甚至都不会知道它发生了,并且无法及早修复它以免造成混乱。记录它们,向用户显示它们,在调试控制台上显示它们 - 无论什么对你有用。但是,不要只是把它们吞下去!


Cropping image is wrong. Where I'm wrong?



        int cropX;
        int cropY;
        int cropWidth;
        int cropHeight;
        public Pen cropPen;

        public DashStyle cropDashStyle = DashStyle.DashDot;
        public bool Makeselection = false;

<pre>private void button4_Click(object sender, EventArgs e)
        {
            Makeselection = true;
            button4.Enabled = true;

            Cursor = Cursors.Default;

            try
            {
                if (cropWidth < 1)
                {
                    return;
                }
                Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
                Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
                Bitmap _img = new Bitmap(cropWidth, cropHeight);
                Graphics g = Graphics.FromImage(_img);

                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);

                pictureBox1.Image = _img;
                pictureBox1.Width = _img.Width;
                pictureBox1.Height = _img.Height;
            }
            catch (Exception ex)
            {

            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
         
                Cursor = Cursors.Default;
                if (Makeselection)
                {

                    try
                    {
                        if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        {
                            Cursor = Cursors.Cross;
                            cropX = e.X;
                            cropY = e.Y;

                            cropPen = new Pen(Color.Red, 1);
                            cropPen.DashStyle = DashStyle.DashDotDot;


                        }                       
                        pictureBox1.Refresh();
                    }
                    catch (Exception ex)
                    {

                    }
                }
            
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (Makeselection)
            {
                Cursor = Cursors.Default;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
                Cursor = Cursors.Default;
                if (Makeselection)
                {

                    try
                    {
                        if (pictureBox1.Image == null)
                            return;


                        if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        {
                            pictureBox1.Refresh();
                            cropWidth = e.X - cropX;
                            cropHeight = e.Y - cropY;
                            pictureBox1.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
                        }



                    }
                    catch (Exception ex)
                    {
                        //if (ex.Number == 5)
                        //    return;
                    }
                }
            
        }



What I have tried:

Cropping image is wrong. Where I'm wrong?

解决方案

Probably, it's scaling. Check your PictureBox settings, specifically the PictureBox.SizeMode Property (System.Windows.Forms) | Microsoft Docs[^] - if it isn't set to "Normal" then the mouse coordinates you are using are relative to the Picturebox but not the the image it contains. If the image is scaled at all, then when you use mouse coordinates at all they don't match the actual picture you are clipping from.

A couple of other things:
1) Graphics contexts are scarce resources - and you are responsible for Disposing of them when you create them. If you don't, you will get "out of memory" errors long before the GC gets called in to tidy up after you. A using block is the best way, as it automatically Disposes when the object goes out of scope, regardless of how it exits the block.

using (Graphics g = Graphics.FromImage(_img))
   {
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   ...
   }


2) Why are you creating so many bitmaps? You don't need half of them!
3) Never swallow exceptions:

catch (Exception ex)
{

}

When you do that, you throw away all information that might help you fix a problem later, as well as "hiding" the problem so you don't even know it happened, and can't fix it early enough to not make a proper mess. Log 'em, Show 'em to the user, display them on the Debug console - whatever works for you. But don;t just blanket swallow them!


这篇关于如何使用Windows窗体中的图片框中的鼠标选择来裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆