Fuzz Evans
Fuzz Evans

Reputation: 2943

How can I save a Panel in my form as a picture?

I have a form that has 2 panels. I'm trying to save the contents of Panel2 as an image. I saw a thread that talked about using the screen capture to do this, but I can't find the thread anymore. Also read about using the DrawToBitMap method, but it's from visual studio 2005 info, not sure if it's the most current or suitable solution for this. So what do you recommend for saving my Panel2 as a picture, preferably a jpg?

UPDATE: I implemented the code recommended below for the DrawToBitMap, but it saves half of my panel2 (the left half if that makes a difference). Because it saved half my panel2, I multiplied the width call by '2' to make it save the full form. Kind of a weird thing and doesn't make sense to me since the width of panel 2 should be the full panel and not half of it?

//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");

Upvotes: 4

Views: 13852

Answers (2)

ivo
ivo

Reputation: 11

namespace PanelToPDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void btnExit_Click(object sender, EventArgs e)
    {
        pictureBox.Dispose();
        Application.Exit();
    }

    private static Bitmap DrawControlToBitmap(Control control)
    {
        Bitmap bitmap = new Bitmap(control.Width, control.Height);
        Graphics graphics = Graphics.FromImage(bitmap);
        Rectangle rect = control.RectangleToScreen(control.ClientRectangle);
        graphics.CopyFromScreen(rect.Location, Point.Empty, control.Size);
        return bitmap;
    }
     private void btnToImage_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = DrawControlToBitmap(panel);
        pictureBox.Image = bitmap;
    }     
}

}

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

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

From above link:

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

The DrawToBitmap method has the following limitations:

  • An ArgumentException might be thrown for large bitmaps. The maximum allowable size varies by machine.
  • DrawToBitmap does not support the Ink controls for the Windows XP Tablet PC Edition 2005 operating system.
  • DrawToBitmap does not draw a child TextBox if the Visible property of the TextBox is set to false.
  • Controls inside containers are rendered in reverse order.
  • DrawToBitmap is not fully functional for the RichTextBox; only the border of a bitmap is drawn.

Edit Added example and image:

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

Upvotes: 7

Related Questions