2012-07-23 61 views
1

我有一個System.Drawing.Graphics從一個picturebox中取出。c#將System.Drawing.Graphics保存到文件

基本上我畫了一個背景圖像的某些行存儲到PictureBox 現在我想保存包含背景圖像,一切都繪製那邊PNG ...

是有辦法做到這一點? 在此先感謝

回答

1

如果您繪製在Paint事件這些行,那麼你需要創建一個BitMap與BackgroundImage並使用Bitmap的圖形調用繪圖函數,然後調用Bitmap.Save()。

 Bitmap bmp = new Bitmap(MyPictureBox.BackgroundImage); 
     var g = Graphics.FromImage(bmp); 
     MyPictureBox.OnPaint(new System.Windows.Forms.PaintEventArgs(g, bmp.GetBounds()); 
0

很確定您可以轉換爲位圖並保存它?

看到這篇文章:

Saving System.Drawing.Graphics to a png or bmp

+0

感謝,我讀過這一點,但不工作... :( – ghiboz 2012-07-23 23:10:29

+0

不,你不能轉換爲任意圖形爲位圖並保存它。什麼鏈接顯示,儘管它的標題,通常當你有一個你正在渲染的圖形時,完成想要的結果的方法是INSTEAD使用'Graphics.FromImage(aBitmap)'創建一個圖形的渲染調用。如果你的代碼可以那麼你可以做'aBitmap.Save',因爲你有一個你已經渲染過的位圖。 – ToolmakerSteve 2015-04-13 18:53:32

4

是的,使用Image.Save method上的圖片框的圖片:

pictureBox.Image.Save(fullnameOfYourImage, System.Drawing.Imaging.ImageFormat.Png); 
+0

謝謝,但圖像是空的......我想因爲我用System來畫一些文本。 Drawing.Graphics – ghiboz 2012-07-23 23:09:45

+0

@ghiboz這聽起來像是你真正的問題,然後是如何繪製到一個PictureBox圖像。我建議創建一個ne W問題(顯示你的代碼),但也許[這](http://stackoverflow.com/questions/1063505/c-sharp-how-to-save-a-picturebox-control-as-a-jpeg-file-編輯後)將有所幫助。祝你好運! – 2012-07-24 19:33:33