[英]C# - picturebox, graphics.DrawImage and picturebox.Image = null, why?
I'm trying to crop and resize an image in PictureBox1. 我正在尝试在PictureBox1中裁剪图像并调整其大小。 My code:
我的代码:
//original image for eventually undo
undoImage = pictureBox1.BackgroundImage.Clone() as Image;
Bitmap sourceBitmap = new Bitmap(pictureBox1.BackgroundImage, pictureBox1.Width, pictureBox1.Height);
Graphics g = pictureBox2.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
And it working properly on two PictureBox
es. 并且它可以在两个
PictureBox
上正常工作。 But PictureBox2.Image
, PictureBox2.BackgroundImage
(and any other including ErrorImage...) = null
. 但是
PictureBox2.Image
, PictureBox2.BackgroundImage
(以及其他任何包含ErrorImage ...) = null
。 I tried PictureBox.DrawToBitmap
, and other, like g.GetHdc()
found on google, but unsuccessful. 我尝试了
PictureBox.DrawToBitmap
和其他工具,例如在Google上找到的g.GetHdc()
,但未成功。
My question: 我的问题:
How do I properly copy the edited image from PictureBox2
to PictureBox1
? 如何将编辑后的图像从
PictureBox2
正确复制到PictureBox1
?
Trivial solution: 平凡的解决方案:
undoImage = pictureBox1.BackgroundImage.Clone() as Image;
Bitmap sourceBitmap = new Bitmap(pictureBox1.BackgroundImage, pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(sourceBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width,pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
}
pictureBox1.BackgroundImage = sourceBitmap;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.