I am Creating an windows application using c#.I have a button which should capture the image(the entire Desktop screen) and Save it in a folder .Also i need to show the preview of the image .
-
1What is the problem? What is the error? What have you tried? Your ""question"" is just a statement. – Smudge202 Jul 04 '11 at 13:57
-
How to Capture the desktop screen and save the image in a folder? – Karthik Jul 04 '11 at 14:03
3 Answers
Graphics.CopyFromScreen Method
sample code:
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Save();
bmp.Save("D:\\file.jpg", ImageFormat.Bmp);
as for show the preview. IMO not that hard to write it on ur own.

- 2,374
- 1
- 17
- 18
-
Nikko , what about if i want to save the displayed image as its full resolution ? – Royi Namir Feb 23 '13 at 15:15
-
@RoyiNamir Cant do that with Graphics.CopyFromScreen method. this method just copies a part of screen to destination. it has no knowledge of displayed image's full resolution. – Nika G. Feb 28 '13 at 06:01
There are different ways to perform what you bring here. Using the Screen
class, there are a few simple samples I found on the Internet. Others are using Direct3D.
- TeboScreen: Basic C# Screen Capture Application;
- Capture a Screen Shot;
- C# – Screen capture with Direct3D;
- Capture DeskTop Screen;
- Enhanced Desktop Recorder in .NET using C# and Windows Forms; (perhaps not suited for your question, but might get interesting if you plan further features.)
- Capturing the Screen Image Using C#.
In short, the idea consists of getting the image of the desktop using the Screen
class or your favorite way, store it into a Bitmap
object and save this bitmap into a file.
As for displaying a preview, once your Bitmap
instance is created, you simply need a PictureBox
and set its Image
property and show your form to the user so he may see the image.
Hope this helps! =)

- 23,773
- 22
- 96
- 162
You will need to do some importing of Interop dlls.
Take a look at the following example shows very well how to capture the screen shot and save to disk.
public void CaptureScreen(string fileName,ImageFormat imageFormat)
{
int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap);
GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8),
GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020);
SaveImageAs(hBitmap,fileName,imageFormat);
Cleanup(hBitmap,hdcSrc,hdcDest);
}
The above example taken from the website. All code by Perry Lee

- 5,896
- 3
- 23
- 24
-
1from the link u've posted /* Author: Perry Lee * Submission: Capture Screen (Add Screenshot Capability to Programs) * **Date of Submission: 12/29/03** */ no need to load all those dll. that code is a bit obsolete. with .net2.0 and above things are much easier – Nika G. Jul 04 '11 at 14:22
-
@Niko, true. net net has come a long way, and .net 4 improves even more. – Jethro Jul 04 '11 at 16:48