Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像?_C#_.net_Winforms - Fatal编程技术网

C# 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像?

C# 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像?,c#,.net,winforms,C#,.net,Winforms,首先,我用鼠标在图片B上画一个矩形 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rect = new Rectangle(e.X, e.Y, 0, 0); painting = true; } } private void pictureBox1_MouseMove(ob

首先,我用鼠标在图片B上画一个矩形

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        rect = new Rectangle(e.X, e.Y, 0, 0);
        painting = true;
    }
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        rect = new Rectangle(
            rect.Left, 
            rect.Top, 
            Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left), 
            Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
    }
    this.pictureBox1.Invalidate();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (painting == true)
    {
        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}
变量rect是全局矩形,绘画是全局布尔

然后我在pictureBox1鼠标事件中

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    pictureBox1.Image = SaveRectanglePart(pictureBox1.Image, rect);
}
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
以及保存矩形部分的方法

Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
    using (var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height))
    {
        using (var graphics = Graphics.FromImage(bmp))
        {
            graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
        }
        bmptoreturn = bmp;
    }

    return bmptoreturn;
}
我想做的是,当我在mouseup事件中完成绘制矩形时,清除pictureBox1并将其中的图像仅替换为矩形图像

但我在mouseup事件中得到的异常参数无效

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    pictureBox1.Image = SaveRectanglePart(pictureBox1.Image, rect);
}
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);

我是否应该在函数
SaveRectanglePart
中的某个地方处理变量bmptoreurn?

在函数返回之前,变量
bmp
dispose
的,这是
using
语句的结果。您需要使用语句删除
,代码应该可以正常工作

Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
    var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height)
    using (var graphics = Graphics.FromImage(bmp))
    {
        graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
    }
    bmptoreturn = bmp;

    return bmptoreturn;
}
但是我们有一个问题,
bmptoreurn
pictureBox1.Image
在设置之前引用了什么。旧的
图像
/
位图
引用将丢失在内存中,直到垃圾回收来释放内存。要成为一名优秀的程序员,我们需要在处理完这些
图像
/
位图
后处理它们

Image tmp = bmptoreturn;
bmptoreturn = bmp;
if(tmp != null)
    tmp.Dispose();
...
Image tmp = pictureBox1.Image;
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
if(tmp != null)
    tmp.Dispose();

另外,我不确定您为什么要使用
bmptoreurn
,但据我所知,代码中不需要它。如果
bmptoreurn
没有在其他地方使用,您可以简单地返回
bmp

在函数
SaveRectanglePart
中,变量
bmp
Dispose
的,函数返回前的
using
语句的结果。您需要使用
语句删除
,代码应该可以正常工作

Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
    var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height)
    using (var graphics = Graphics.FromImage(bmp))
    {
        graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
    }
    bmptoreturn = bmp;

    return bmptoreturn;
}
但是我们有一个问题,
bmptoreurn
pictureBox1.Image
在设置之前引用了什么。旧的
图像
/
位图
引用将丢失在内存中,直到垃圾回收来释放内存。要成为一名优秀的程序员,我们需要在处理完这些
图像
/
位图
后处理它们

Image tmp = bmptoreturn;
bmptoreturn = bmp;
if(tmp != null)
    tmp.Dispose();
...
Image tmp = pictureBox1.Image;
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
if(tmp != null)
    tmp.Dispose();

另外,我不确定您为什么要使用
bmptoreurn
,但据我所知,代码中不需要它。如果
bmptoreurn
未在其他地方使用,您只需返回
bmp

KDecker我现在尝试了该代码,我根据您的解决方案更改了方法SaveRectanglePart,现在只使用bmp。在mouseup事件中,我添加了带有图像tmp=pictureBox1.Image;。。。。还有ret,但它的作用就像将图像放大到矩形。它不是替换矩形部分,而是进行放大。@DanielVoit从我的问题中可以看出“清除图片box1并将其中的图像仅替换为矩形图像。”这正是您想要做的。KDecker我现在尝试了代码,我根据您的解决方案更改了SaveRectanglePart方法,并且现在只使用bmp。在mouseup事件中,我添加了带有图像tmp=pictureBox1.Image;。。。。还有ret,但它的作用就像将图像放大到矩形。它不是替换矩形部分,而是进行放大。@DanielVoit从“清除图片box1并将其中的图像仅替换为矩形图像”的问题中可以看出,这正是您想要做的。