![](/img/trans.png)
[英]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?
[英]Saving inside of rectangle in picturebox
我有类似的东西-图片从文件加载到picturebox1:
然后按下按钮“ SelectArea”后,我可以在加载的图片上绘制矩形:
最后,按下另一个按钮“ SaveArea”后,我想将img保存在创建的矩形内,因此,我将像这样将图像保存在驱动器上:
如何编写代码帮助我完成最后一步->保存在矩形内部?
Tnx。
首先重新制作尺寸正确的位图
然后创建一个图形对象为这个Graphics.FromImage然后使用的DrawImage生成的图形对象上的方法来绘制你的大图像的部分到位图上。
最后,使用Save保存位图对象:
public static void SaveBitmapPart(System.Drawing.Image image, System.Drawing.RectangleF sourceRect, string pathToSave )
{
using (var bmp = new System.Drawing.Bitmap((int)sourceRect.Width, (int)sourceRect.Height))
{
using (var graphics = System.Drawing.Graphics.FromImage(bmp))
{
graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, System.Drawing.GraphicsUnit.Pixel);
}
bmp.Save(pathToSave);
}
}
因此只需调用:
SaveBitmapPart(picturebox1.Image, myRectangle, @"c:\Temp\Test.bmp");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.