![](/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.