Yorthen

I have a SplitContainer with two custom controls in it, what I'd like to do is to dump the whole thing to an image, like if you take a screenshot and cut away all but the SplitContainer. I've read somewhere that you could do something like this by creating an image and the get an Graphics object from the image and let the controls draw on that. I understand that since the SplitContainer is a standard control this might not be possible, but I can change the two controls if necessary, just as long as I can find a way to specify where the controls will draw on the image.


My problem is that after looking at System.Drawing.Image and not having found any way to get a Graphics objects from I'm not sure where to start, so any pointers, example, or such would be much appreciated.


Re: Windows Forms General Dump controles to image /saving to image

Bruno Silveira

Yorthen,

To get a graphics object you need to override the Paint event of your splitContainer. Then you'll have a PaintEventArgs object as a paremeter, which you can use it to access the graphics object inside it.

Please do not forget to call Base.Paint() so all drawing from the base class will be done. Otherwise you will be responsible for all control repaint, which is not so easy. Smile

Was this helpful

Bruno






Re: Windows Forms General Dump controles to image /saving to image

nobugz

You can use the DrawToBitmap() method to create an image of the SplitContainer. It will draw it exactly as it appears on the screen, you'll have to position the controls yourself if that's your requirement. Any RichTextBox or WebBrowser control embedded on one of the panels won't draw correctly.

private void button1_Click(object sender, EventArgs e) {
using (Bitmap bmp = new Bitmap(splitContainer1.ClientSize.Width, splitContainer1.ClientSize.Height)) {
splitContainer1.DrawToBitmap(bmp, splitContainer1.ClientRectangle);
bmp.Save(@"c:\temp\test.bmp");
}
}