C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Images »

Save the Form & other controls as Bitmap


Posted Date: 09 Mar 2007    Resource Type: Code Snippets    Category: Images
Author: Manikandan BalakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 10



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();

}

}



Responses

Author: Javier    22 Jan 2009Member 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: Joymon    23 Jan 2009Member Level: Gold   Points : 0
What is the use of
Graphics g = control.CreateGraphics();
?


Author: BrianP    27 Jan 2009Member 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: BrianP    27 Jan 2009Member 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: Joymon    27 Jan 2009Member 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.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Create Thumbnail of an image
Previous Resource: Creating a ThumbNail Image on Fly
Return to Discussion Resource Index
Post New Resource
Category: Images


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use