How to save the panel in my form as an image?

I have a form with two panels. I am trying to save the contents of Panel2 as an image. I saw a thread that talked about using screen capture to do this, but I can no longer find the stream. Also read about using the DrawToBitMap method, but it is from visual studio 2005 information, not sure if this is the most relevant or suitable solution for this. So what do you recommend to save my Panel2 as an image, preferably jpg?

UPDATE: I applied the code below for DrawToBitMap, but it saves half of my panel2 (left half if that matters). Since it saved half of my panel2, I multiplied the width call by "2" to keep the full shape. Something strange and does not make sense to me, since the width of panel 2 should be a full panel, not half?

//multiplies the width of panel2 call by 2 to make it save the full panel Bitmap bmp = new Bitmap(splitContainer1.Panel2.Width * 2, splitContainer1.Panel2.Height); splitContainer1.Panel2.DrawToBitmap(bmp, splitContainer1.Panel2.Bounds); bmp.Save(@"C:\Test.bmp"); 
+4
source share
1 answer

Control.DrawToBitMap is still supported in .Net 4. With the following caveats.

Top link:

  • The DrawToBitmap method is not supported by ActiveX controls. You can override the OnPrint event and provide custom print logic if required.

The DrawToBitmap method has the following limitations:

  • An ArgumentException may occur for large bitmaps. The maximum allowable size depends on the machine.
  • DrawToBitmap does not support ink controls for Windows XP Tablet PC Edition 2005.
  • DrawToBitmap does not draw a child TextBox if the Visible property of the TextBox is set to false.
  • The controls inside the containers are displayed in reverse order.
  • DrawToBitmap is not fully functional for RichTextBox; only the borders of the picture.

Edit Added example and image:

 Bitmap bmp = new Bitmap(panel1.Width,panel1.Height); panel1.DrawToBitmap(bmp, panel1.Bounds); bmp.Save(@"C:\Temp\Test.bmp"); 

+7
source

Source: https://habr.com/ru/post/1389396/


All Articles