将面板保存为图像

我正在做这个油漆应用程序。 这很简单。 它由一个我将绘制的面板组成,然后最终我将保存为JPG或BMP或PNG文件。

我的应用程序工作完美,但我遇到的问题是,当我保存输出不是面板上绘制的黑色图像没有什么只是黑色。

我的所有工作都被保存为

Thepic = new Bitmap(panel1.ClientRectangle.Width, this.ClientRectangle.Height); 

在我的鼠标(向下,向上的东西)上

 snapshot = (Bitmap)tempDraw.Clone(); 

并且它保存了正常的工作但是再次声音是黑色的图像不是面板包含的内容。

我认为问题可能是你正在使用“克隆”方法。

尝试“ DrawToBitmap ” – 这在过去对我有用

这是一个从名为“plotPrinter”的控件中保存位图的示例:

  int width = plotPrinter.Size.Width; int height = plotPrinter.Size.Height; Bitmap bm = new Bitmap(width, height); plotPrinter.DrawToBitmap(bm, new Rectangle(0, 0, width, height)); bm.Save(@"D:\TestDrawToBitmap.bmp", ImageFormat.Bmp); 
  Be aware of saving directly to the C directly as this is not permitted with newer versions of window, try using SaveFileDialog. 
  SaveFileDialog sf = new SaveFileDialog(); sf.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf"; sf.ShowDialog(); var path = sf.FileName; 

你可以尝试这个,它适合我,而不是我sed MemoryStream。

 MemoryStream ms = new MemoryStream(); Bitmap bmp = new Bitmap(panel1.Width, panel1.Height); panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, panel1.Width, panel1.Height)); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //you could ave in BPM, PNG etc format. byte[] Pic_arr = new byte[ms.Length]; ms.Position = 0; ms.Read(Pic_arr, 0, Pic_arr.Length); ms.Close();