How to capture the image of the screen according to the resolution using Dot Net

Hi

Today I was playing around with some window form and came up with a simple and interesting requirement. I wanted to take a print screen of the monitor from inside the application. What I wanted to do was allow the user to save the screen as and when they wanted it from inside the application.

So first thing I had to do for this was to find out the resolution of the screen that the user was working with. Because the screenshot need to be save of the same size as the screen resolution. This can be done by using the System.Windows.Forms.SystemInformation class. This class provides a static method PrimaryMonitorSize which gives us the monitor size. This property is of type Size. So we have width and height of the screen using the following code.

int width, height;

width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;

height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;

The following code will capture the screen and save it in the desktop as image.bmp;

Bitmap sBit;

Rectangle screenRegiion = Screen.AllScreens[0].Bounds;

sBit = new Bitmap(width, height, PixelFormat.Format32bppArgb);

Graphics sGraph = Graphics.FromImage(sBit);

sGraph.CopyFromScreen(screenRegiion.Left, screenRegiion.Top, 0, 0, screenRegiion.Size);

string savedPath =Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\VikramLakhotia.bmp";

sBit.Save(savedPath);

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 5/2/2007 3:45:21 AM

Your Comments

Posted on 5/11/2007 9:32:08 PM

Very interesting...
We are developing an embedded system for the blind through which they can feel images your project can take ours to the next level....by allowing him to save custom images..currently we have provided him a datbase of images...
I need some help Vikram regarding our project
Our s/w application also consists of a geometry application through this the user can select a figure(basic ones) and on entering the dimension it should create a figure...
Currently I m able to show the figures on canvas...
HOw do i make the dimension variable i.e using Gdi+ we can define shapes like rectangle ellipse and so on and so forth...but the problem is that in this the we have to define the dimensions and the pen so its harcoded.My question is is there anyway i can make these figures accept variable dimensions i.e the user can punch his own dimension based on requirement and the s/w has to develop this based on th user dimension.
Thanks

Posted on 5/13/2007 10:14:21 AM

This is really a nice one man. Keep up the good work.

Posted on 5/17/2007 7:53:07 PM

wow interesting tips again from you. I happened to find this recently as I want to capture error screen and save it to a file, so we - developer - will know what exactly the error is. Thanks.

p.s. about the drag-drop, I found out in msdn, that they recommend using listview, so it's working fine now :)

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia