开发者

How to grab an image and save it in a folder [c# windows application]

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 t开发者_如何学Che image .


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.


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! =)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜