2012-07-23 83 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