简体   繁体   English

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

[英]How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area?

First i'm drawing a rectangle on the pictureBox1 with the mouse 首先,我用鼠标在pictureBox1上绘制一个矩形

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);
        }
    }
}

The variable rect is global Rectangle and painting is global bool. 变量rect是全局Rectangle,绘画是全局bool。

Then I did inside the pictureBox1 mouseup event 然后我在pictureBox1 mouseup事件中做了

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

And the method SaveRectanglePart 和方法SaveRectanglePart

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;
}

What i want to do is when i finish drawing the rectangle in the mouseup event to clear the pictureBox1 and replace the image in there with the rectangle image only. 我想要做的是当我完成在mouseup事件中绘制矩形以清除pictureBox1并仅用矩形图像替换其中的图像。

But i'm getting exception parameter not valid in the mouseup event 但是我在mouseup事件中得到异常参数无效

pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);

And should i dispose somewhere the variable bmptoreturn ? 我应该在变量bmptoreturn的哪个地方处置?

In the function SaveRectanglePart the variable bmp is Dispose of before the function returns as a result of the using statement. 在函数SaveRectanglePart ,变量bmp是函数返回之前的Dispose ,作为using语句的结果。 You need to remove the using statement and the code should work. 您需要删除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;
}

But we have the issue of what bmptoreturn and pictureBox1.Image were referencing before they were set. 但是我们遇到了bmptoreturnpictureBox1.Image在设置之前引用的问题。 The old Image / Bitmap the reference will be lost in memory until garbage collection comes along to free their memory. 旧的Image / Bitmap引用将在内存中丢失,直到垃圾收集来释放它们的内存。 To be a good programmer we need to Dispose of these Image / Bitmap when we are done with them. 要成为一名优秀的程序员,我们需要在完成它们时Dispose这些Image / Bitmap

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();

Also, I am not sure why you are using bmptoreturn but it is not needed in the code from what I can tell. 此外,我不知道你为什么使用bmptoreturn但我不知道在代码中是否需要它。 You can simply return bmp if bmptoreturn is not being used elsewhere. 如果没有在其他地方使用bmptoreturn你可以简单地返回bmp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图片从PictureBox1复制到Excel? - How to copy image from PictureBox1 to excel? 图片从picturebox1到picturebox2 - image from picturebox1 to picturebox2 我正在尝试将pictureBox1内的图像保存到我的硬盘上,但是将其保存而没有pictureBox1内的矩形,这是为什么呢? - Im trying to save now the image inside the pictureBox1 to my hard disk but its saving it without the rectangle inside the pictureBox1 why? 移动trackBar1时,如何保持在图片盒1中的图像上绘制的点? - How can i keep the points im drawing on the image in the picturebox1 when moving the trackBar1? 我无法从 C# 中的代码更改 pictureBox1 中的图像 - I cannot change the image in pictureBox1 from code in C# DatagridView图片图像到C#中的PictureBox1 - DatagridView picture image to PictureBox1 in C# 如何在不离开pictureBox区域的情况下在pictureBox1内部移动按钮? - How to move the button inside the pictureBox1 without leave the pictureBox area? 如何解决picturebox1是null? - How to solve picturebox1 is null? 如何在将鼠标移到pictureBox1上时自动在pictureBox2上绘制点? - How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2? 单击C#中的按钮,将图像从“ pictureBox1”保存到文件 - Saving image from “pictureBox1” to a file with a click on a button in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM