Save the Form and other controls as Bitmap


This article represent sample code for how to Save the Form and other controls as Bitmap

Here’s a simple code, which enables save the form and other control as bitmap. This is very use full for the project documentation as well as for the reports. “DrawToBitmap” method provided for all the windows controls, which is used to perform this operation. Also we are not creating any “graphics”


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

SaveAsBitmap(this,"C:\\f1.bmp");

//The same way you can pass the controls like button,dataGrid etc
//SaveAsBitmap(mydataGrid,"C:\\report.bmp")
}

public void SaveAsBitmap(Control control, string fileName)
{
//getthe instance of the graphics from the control
Graphics g = control.CreateGraphics();

//new bitmap object to save the image
Bitmap bmp = new Bitmap(control.Width, control.Height);

//Drawing control to the bitmap
control.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height));

bmp.Save(fileName);
bmp.Dispose();

}

}


Comments

Author: Javier22 Jan 2009 Member Level: Bronze   Points : 1

I need to include in the report a graphics that I drew on one page of a tabbed control. How can I save it as bitmap? Note that the page may not be active or visible. Using the code above doesn't work in that case. Thank you.

Javier

Author: Joymon23 Jan 2009 Member Level: Gold   Points : 0

What is the use of
Graphics g = control.CreateGraphics();
?

Author: BrianP27 Jan 2009 Member Level: Gold   Points : 1

@ Joy

CreateGraphics returns a reference to the control's drawing surface (in this case, it's the form's drawing surface)

Author: BrianP27 Jan 2009 Member Level: Gold   Points : 2

@ Manikandan Balakrishnan

Nice job! I was looking for some code for creating thumbnails of forms for a doc project. Your code was a great starting place. See my code below for actually resizing the image. By the way, many of the System.Drawing objects are pretty resource intensive. I think you should add a g.Dispose to the end of your code.

Here's the code for resizing the image:

private void Form1_Load(object sender, EventArgs e)
{
SaveAsScaledBitmap(this, @"c:\temp\test1.bmp", 0.50);
}

public void SaveAsScaledBitmap(Control control, string fileName, double scale)
{
//getthe instance of the graphics from the control
using (Graphics g = control.CreateGraphics())
{
//new bitmap object to save the image
using (Bitmap bmp = new Bitmap(control.Width, control.Height))
{
//Drawing control to the full-sized bitmap
control.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height));
// resize the image based on the scale (which is percentage of full-sized)
Size scaledSize = new Size();
scaledSize.Height = Convert.ToInt32(control.Height * scale);
scaledSize.Width = Convert.ToInt32(control.Width * scale);
using (Bitmap resizedImage = new Bitmap(bmp, scaledSize))
{
// save the resized image to the file
resizedImage.Save(fileName);
}
}
}
}

Author: Joymon27 Jan 2009 Member Level: Gold   Points : 1

I know it returns the drawing surface.
But my doubt was why you are unneccessarily creating that reference?
I can't find out usage of that object anywhere in your code.

Author: Heartly24 Jan 2011 Member Level: Gold   Points : 0

I got an error : A generice error occured in GDI+

Why??



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: