[英]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. 但是我们遇到了
bmptoreturn
和pictureBox1.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.