2

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 .

Karthik
  • 218
  • 1
  • 7
  • 18

3 Answers3

3

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.

Nika G.
  • 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
1

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.

  1. TeboScreen: Basic C# Screen Capture Application;
  2. Capture a Screen Shot;
  3. C# – Screen capture with Direct3D;
  4. Capture DeskTop Screen;
  5. 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.)
  6. 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! =)

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
0

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

Jethro
  • 5,896
  • 3
  • 23
  • 24
  • 1
    from 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