How can I save a screenshot directly to a file in Windows?


In Windows XP, one can press Alt-PrintScreen to copy an image of the active window, or Ctrl-PrintScreen to copy an image of the full desktop.

This can then be pasted into applications that accept images: Photoshop, Microsoft Word, etc.

I'm wondering: Is there a way to save the screenshot directly to a file? Do I really have to open an image program, like or Photoshop, simply to paste an image, then save it?

8/15/2017 4:41:57 PM

Accepted Answer

You can code something pretty simple that will hook the PrintScreen and save the capture in a file.

Here is something to start to capture and save to a file. You will just need to hook the key "Print screen".

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class CaptureScreen

    static public void Main(string[] args)

            Bitmap capture = CaptureScreen.GetDesktopImage();
            string file = Path.Combine(Environment.CurrentDirectory, "screen.gif");
            ImageFormat format = ImageFormat.Gif;
            capture.Save(file, format);
        catch (Exception e)


    public static Bitmap GetDesktopImage()
        WIN32_API.SIZE size;

        IntPtr  hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow()); 
        IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC); = WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN); = WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN);

        m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC,,;

        if (m_HBitmap!=IntPtr.Zero)
            IntPtr hOld = (IntPtr) WIN32_API.SelectObject(hMemDC, m_HBitmap);
            WIN32_API.BitBlt(hMemDC, 0, 0,,, hDC, 0, 0, WIN32_API.SRCCOPY);
            WIN32_API.SelectObject(hMemDC, hOld);
            WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC);
            return System.Drawing.Image.FromHbitmap(m_HBitmap); 
        return null;

    protected static IntPtr m_HBitmap;

public class WIN32_API
    public struct SIZE
        public int cx;
        public int cy;
    public  const int SRCCOPY = 13369376;
    public  const int SM_CXSCREEN=0;
    public  const int SM_CYSCREEN=1;

    public static extern IntPtr DeleteDC(IntPtr hDc);

    public static extern IntPtr DeleteObject(IntPtr hDc);

    public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,  int nWidth, int nHeight);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport ("gdi32.dll",EntryPoint="SelectObject")]
    public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);

    [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();

    public static extern IntPtr GetDC(IntPtr ptr);

    public static extern int GetSystemMetrics(int abc);

    public static extern IntPtr GetWindowDC(Int32 ptr);

    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);

Update Here is the code to hook the PrintScreen (and other key) from C#:

Hook code

10/12/2017 6:20:05 AM

Little known fact: in most standard Windows (XP) dialogs, you can hit Ctrl+C to have a textual copy of the content of the dialog.
Example: open a file in Notepad, hit space, close the window, hit Ctrl+C on the Confirm Exit dialog, cancel, paste in Notepad the text of the dialog.
Unrelated to your direct question, but I though it would be nice to mention in this thread.

Beside, indeed, you need a third party software to do the screenshot, but you don't need to fire the big Photoshop for that. Something free and lightweight like IrfanWiew or XnView can do the job. I use MWSnap to copy arbitrary parts of the screen. I wrote a little AutoHotkey script calling GDI+ functions to do screenshots. Etc.


Thanks for all the source code and comments - thanks to that, I finally have an app that I wanted :)

I have compiled some of the examples, and both sources and executables can be found here:

I use InterceptCaptureScreen.exe - simply run it in a command prompt terminal, and then press Insert when you want to capture a screenshot (timestamped filenames, png, in the same directory where the executable is); keys will be captured even if the terminal is not in focus.

(I use Insert key, since it should have an easier time propagating through, say, VNC than PrintScreen - which on my laptop requires that also Fn key is pressed, and that does not propagate through VNC. Of course, its easy to change what is the actual key used in the source code).

Hope this helps, Cheers!


Very old post I realize, but windows finally realized how inane the process was.

In Windows 8.1 (verified, not working in windows 7 (tnx @bobobobo))

windows key + prnt screen saves the screenshot into a folder in <user>/Pictures/Screenshots

Source -


Might I suggest WinSnap It provides an autosave option and capture the alt+printscreen and other key combinations to capture screen, windows, dialog, etc.


Dropbox now provides the hook to do this automagically. If you get a free dropbox account and install the laptop app, when you press PrtScr Dropbox will give you the option of automatically storing all screenshots to your dropbox folder.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]