blah blah blah is here! blah blah » Close

up1down
link

Hello,

Short and simple question: How to save the drawing (contents) on a panel as an image?

Thanks in advance!
Best regards,
Sam

last answered one year ago

4 answers

up1down
link

maybe you can try something like this

//pnl is the panel you have
Bitmap bmp = new Bitmap(pnl.Bounds.Width, pnl.Bounds.Height);
pnl.DrawToBitmap(bmp, pnl.Bounds);
bmp.Save("C:\\myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

SAS_Sam
30

Hello, thank you for the fast reply. This seems to be returning something weird. The panel looks like this: [url]http://users.telenet.be/seti2k/panel.jpg[/url] The saved image like this: [url]http://users.telenet.be/seti2k/myimage.jpg[/url] thanks in advance.

vulpes
17279

Muster's code looks OK to me but I do know that the DrawToBitmap method can't cope with RichTextBoxes and Active X controls. Do you have either of those in your panel?

SAS_Sam
30

Not really, the only thing that is on that panel are graphics (drawstring, drawimage, drawline, etc) , it's really weird this thingie :|

up0down
link

If you're just drawing on the panel then I don't understand why the previous code doesn't work properly but see if it's any better if you do it a different way:

Bitmap bmp = new Bitmap(pnl.Bounds.Width, pnl.Bounds.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.CopyFromScreen(this.PointToScreen(pnl.Bounds.Location), new Point(0, 0), pnl.Bounds.Size);
bmp.Save("C:\\myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
gfx.Dispose();
bmp.Dispose();

SAS_Sam
30

I have no idea why. Your last code works, but that's more taking a "screenshot", so if my panel is hidden or on another tab this won't work.

vulpes
17279

That's true though, if DrawToBitmap doesn't work, I don't know what else to suggest unless you create and draw to the bitmap at the same time as you draw to the panel. Could you momentarily make the panel or tab visible whilst you do the screen copy and then change it back or would this be too jerky?

up0down
link

I must be doing something wrong with the painting itself, I made a new project with just this in it.
The stuff is coming on the panel, so I see the red circle & line, but the result looks like this when I save: http://users.telenet.be/seti2k/myimage.jpg

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

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Graphics g = pnlTest.CreateGraphics();
g.DrawEllipse(new Pen(new SolidBrush(Color.Red)), 10, 10, 20, 20);
g.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(5, 5), new Point(30, 30));

Bitmap bmp = new Bitmap(pnlTest.Bounds.Width, pnlTest.Bounds.Height);
pnlTest.DrawToBitmap(bmp, pnlTest.Bounds);
bmp.Save("myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}

up0down
link

I think the problem here is graphics persistence.

The DrawToBitmap method will only persist something that's been drawn in the panel's Paint eventhandler using the graphics object provided by the argument, e, to that handler.

So, I'd try instead:

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

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pnlTest.Bounds.Width, pnlTest.Bounds.Height);
pnlTest.DrawToBitmap(bmp, pnlTest.Bounds);
bmp.Save("myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
}

private void pnlTest_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawEllipse(new Pen(new SolidBrush(Color.Red)), 10, 10, 20, 20);
g.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(5, 5), new Point(30, 30));
}
}
}

SAS_Sam
30

Ok thank you all, this seems to work fine!

Feedback