Adsence750x90

Wednesday, May 14, 2008

Screen Shots in C# - code for taking desktop image (screen capture)

How to capture Screenshots in C#.Net

This C# code for capturing screen shots. This code returns bitmap image save this image in in computer.

private static Bitmap BitMapCreater()
{
Rectangle rect = Screen.PrimaryScreen.Bounds;
int color = Screen.PrimaryScreen.BitsPerPixel;
PixelFormat pFormat;
switch (color)
{
case 8:
case 16:
pFormat = PixelFormat.Format16bppRgb565;
break;

case 24:
pFormat = PixelFormat.Format24bppRgb;
break;

case 32:
pFormat = PixelFormat.Format32bppArgb;
break;

default:
pFormat = PixelFormat.Format32bppArgb;
break;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, pFormat);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
return bmp;
}
call this method to SAVE file
on Button Click()
{
Bitmap b = BitMapCreater();
printScreen = string.Format("{0}{1}", Path.GetTempPath(), "screen" + i + ".jpg");
b.Save(printScreen, ImageFormat.Jpeg);

}

1 comment:

Anonymous said...

What template do you use in C#... I get a bunch of errors when trying to build the solution.