• 0

[C#] Saving a Panel to an Image


Question

Hey all,

I'm currently writing a program in C#, whereby the program draws a graph based on a set of data given to from a text file. I'm drawing the graph using GDI+ in a panel, but I'd like for the user to be able to save the graph as an image type (JPEG, PNG, GIF, bitmap, whatever, it doesn't really matter the format), but I don't know how. Any tips on where to look functions wise?

TIA,

-- El Sid

Link to comment
https://www.neowin.net/forum/topic/562947-c-saving-a-panel-to-an-image/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

If you're doing the rendering yourself based on the file, you should be able to just render to an Image or Bitmap (not sure which) using the same rendering techniques you're using to render to the panel and then save it into the specified file/format... I know that's not very specific help, but i hope it does help you.

  • 0

You can move all of your drawing code over to a little function like this:

private void DrawGraph(Graphics g)

{

//draw graph here.

}

and call DrawGraph from your onpaint method. Then, you can create a new Bitmap, and do something like this:

using (Graphics g = Graphics.FromImage(YourImage))

{

DrawGraph(g);

}

  • 0

It's actually pretty easy if you use the DrawToBitmap method built into most of the Windows Form controls, assuming you have a panel called p:

			using(Bitmap graphicSurface = new Bitmap(p.Width, p.Height))
			{
				using(StreamWriter bitmapWriter =new StreamWriter("TEST.JPEG"))
				{
					p.DrawToBitmap(graphicSurface, new Rectangle(0, 0, p.Width, p.Height));
					graphicSurface.Save(bitmapWriter.BaseStream, ImageFormat.Jpeg);
				}		
			}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.