Reputation: 2943
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
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
Reputation: 54532
Control.DrawToBitMap
is still supported in .Net 4. With the following caveats.
From above link:
The DrawToBitmap method has the following limitations:
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