如何将屏幕截图直接保存到 Windows 中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:27:24  来源:igfitidea点击:

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

windowsscreenshot

提问by David Koelle

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.

在 Windows XP 中,可以按 Alt-PrintScreen 复制活动窗口的图像,或按 Ctrl-PrintScreen 复制整个桌面的图像。

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

然后可以将其粘贴到接受图像的应用程序中:Photoshop、Microsoft Word 等。

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

我想知道:有没有办法将屏幕截图直接保存到文件中?真的必须打开一个图像程序,比如 Paint.net 或 Photoshop,只是为了粘贴图像,然后保存它吗?

采纳答案by Patrick Desjardins

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

您可以编写一些非常简单的代码来挂钩 PrintScreen 并将捕获的内容保存在文件中。

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)
    {

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

    }

    public static Bitmap GetDesktopImage()
    {
        WIN32_API.SIZE size;

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

        size.cx = WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN);
        size.cy = WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN);

        m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, size.cx, size.cy);

        if (m_HBitmap!=IntPtr.Zero)
        {
            IntPtr hOld = (IntPtr) WIN32_API.SelectObject(hMemDC, m_HBitmap);
            WIN32_API.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC, 0, 0, WIN32_API.SRCCOPY);
            WIN32_API.SelectObject(hMemDC, hOld);
            WIN32_API.DeleteDC(hMemDC);
            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;

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

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

    [DllImport("gdi32.dll",EntryPoint="BitBlt")]
    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();

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

    [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
    public static extern int GetSystemMetrics(int abc);

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

    [DllImport("user32.dll",EntryPoint="ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);
}

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

更新这里是从 C# 挂钩 PrintScreen(和其他键)的代码:

Hook code

钩码

回答by TheSoftwareJedi

There is no way to save directly to a file without a 3rd party tool before Windows 8. Here are my personal favorite non-third party tool solutions.

在Windows 8之前没有第三方工具是没有办法直接保存到文件的。以下是我个人最喜欢的非第三方工具解决方案。

For Windows 8 and later

对于 Windows 8 及更高版本

Windows Key+ PrintScreensaves the screenshot into a folder in <user>/Pictures/Screenshots

Windows Key+PrintScreen将屏幕截图保存到文件夹中<user>/Pictures/Screenshots

For Windows 7

对于 Windows 7

In win 7 just use the snipping tool: Most easily accessed via pressing Start, then typing "sni" (enter). or Windows Keythen snienter

在 win 7 中只需使用截图工具:最容易访问的方法是按“开始”,然后键入“sni”(回车)。或者 Windows Key然后snienter

Prior versions of Windows

以前的 Windows 版本

I use the following keyboard combination to capture, then save using mspaint. After you do it a couple times, it only takes 2-3 seconds:

我使用以下键盘组合进行捕获,然后使用 mspaint 保存。做几次后,只需要 2-3 秒:

  1. Alt+PrintScreen
  2. Win+R("run")
  3. type "mspaint" enter
  4. Ctrl-V(paste)
  5. Ctrl-S(save)
  6. use file dialog
  7. Alt-F4(close mspaint)
  1. Alt+PrintScreen
  2. Win+ R(“运行”)
  3. 输入“mspaint” enter
  4. Ctrl- V(粘贴)
  5. Ctrl- S(保存)
  6. 使用文件对话框
  7. Alt- F4(关闭 mspaint)

In addition, Cropperis great (and open source). It does rectangle capture to file or clipboard, and is of course free.

此外,Cropper很棒(并且是开源的)。它可以将矩形捕获到文件或剪贴板,当然是免费的。

回答by PhiLho

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.

鲜为人知的事实:在大多数标准 Windows (XP) 对话框中,您可以按 Ctrl+C 来获得对话框内容的文本副本。
示例:在记事本中打开一个文件,按空格键,关闭窗口,在确认退出对话框上按 Ctrl+C,取消,将对话框的文本粘贴到记事本中。
与您的直接问题无关,但我认为在此线程中提及会很好。

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.

此外,确实,您需要第三方软件来进行屏幕截图,但您不需要为此启动大型 Photoshop。像 IrfanWiew 或 XnView 这样的免费和轻量级的东西可以完成这项工作。我使用 MWSnap 来复制屏幕的任意部分。我写了一个小的 AutoHotkey 脚本调用 GDI+ 函数来做屏幕截图。等等。

回答by sdaau

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:

我已经编译了一些示例,源代码和可执行文件都可以在这里找到:

http://sdaaubckp.svn.sourceforge.net/viewvc/sdaaubckp/xp-take-screenshot/

http://sdaaubckp.svn.sourceforge.net/viewvc/sdaaubckp/xp-take-screenshot/

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.

我使用 InterceptCaptureScreen.exe - 只需在命令提示符终端中运行它,然后在您想要捕获屏幕截图时按 Insert(带时间戳的文件名,png,在可执行文件所在的同一目录中);即使终端不在焦点上,也会捕获键。

(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).

(我使用 Insert 键,因为它应该比 PrintScreen 更容易通过 VNC 传播——这在我的笔记本电脑上还需要按下 Fn 键,并且不会通过 VNC 传播。当然,它很容易改变什么是源代码中使用的实际密钥)。

Hope this helps, Cheers!

希望这会有所帮助,干杯!

回答by Karthik T

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

我意识到很老的帖子,但 Windows 终于意识到这个过程是多么的愚蠢。

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

在 Windows 8.1 中(已验证,不适用于 Windows 7 (tnx @bobobobo))

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

windows key+prnt screen将屏幕截图保存到文件夹中<user>/Pictures/Screenshots

Source - http://windows.microsoft.com/en-in/windows/take-screen-capture-print-screen#take-screen-capture-print-screen=windows-8

来源 - http://windows.microsoft.com/en-in/windows/take-screen-capture-print-screen#take-screen-capture-print-screen=windows-8

回答by Jason

Might I suggest WinSnap http://www.ntwind.com/software/winsnap/download-free-version.html. It provides an autosave option and capture the alt+printscreen and other key combinations to capture screen, windows, dialog, etc.

我可以建议 WinSnap http://www.ntwind.com/software/winsnap/download-free-version.html。它提供了一个自动保存选项并捕获 alt+printscreen 和其他组合键来捕获屏幕、窗口、对话框等。

回答by mwengler

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.

Dropbox 现在提供了自动执行此操作的钩子。如果您有一个免费的 Dropbox 帐户并安装了笔记本电脑应用程序,当您按下 PrtScr Dropbox 时,您可以选择将所有屏幕截图自动存储到您的 Dropbox 文件夹中。

回答by Peter Meyer

You need a 3rd party screen grab utility for that functionality in XP. I dig Scott Hanselman's extensive blogging about cool toolsand usually look there for such a utility -- sure enough, he's blogged about a couple here.

在 XP 中,您需要一个第 3 方屏幕抓取工具来实现该功能。我查阅了 Scott Hanselman关于酷工具的大量博客,并且通常会在那里寻找这样的实用程序——果然,他在这里写了一些关于一对夫妇的博客。

回答by JosephStyons

This will do it in Delphi. Note the use of the BitBlt function, which is a Windows API call, not something specific to Delphi.

这将在 Delphi 中完成。注意 BitBlt 函数的使用,它是一个 Windows API 调用,不是特定于 Delphi 的。

Edit: Added example usage

编辑:添加示例用法

function TForm1.GetScreenShot(OnlyActiveWindow: boolean) : TBitmap;
var
  w,h : integer;
  DC : HDC;
  hWin : Cardinal;
  r : TRect;
begin
  //take a screenshot and return it as a TBitmap.
  //if they specify "OnlyActiveWindow", then restrict the screenshot to the
  //currently focused window (same as alt-prtscrn)
  //Otherwise, get a normal screenshot (same as prtscrn)
  Result := TBitmap.Create;
  if OnlyActiveWindow then begin
    hWin := GetForegroundWindow;
    dc := GetWindowDC(hWin);
    GetWindowRect(hWin,r);
    w := r.Right - r.Left;
    h := r.Bottom - r.Top;
  end  //if active window only
  else begin
    hWin := GetDesktopWindow;
    dc := GetDC(hWin);
    w := GetDeviceCaps(DC,HORZRES);
    h := GetDeviceCaps(DC,VERTRES);
  end;  //else entire desktop

  try
    Result.Width := w;
    Result.Height := h;
    BitBlt(Result.Canvas.Handle,0,0,Result.Width,Result.Height,DC,0,0,SRCCOPY);
  finally
    ReleaseDC(hWin, DC) ;
  end;  //try-finally
end;

procedure TForm1.btnSaveScreenshotClick(Sender: TObject);
var
  bmp : TBitmap;
  savdlg : TSaveDialog;
begin
  //take a screenshot, prompt for where to save it
  savdlg := TSaveDialog.Create(Self);
  bmp := GetScreenshot(False);
  try
    if savdlg.Execute then begin
      bmp.SaveToFile(savdlg.FileName);
    end;
  finally
    FreeAndNil(bmp);
    FreeAndNil(savdlg);
  end;  //try-finally
end;

回答by JosephStyons

Try this: http://www.screenshot-utility.com/

试试这个:http: //www.screenshot-utility.com/

From their homepage:

从他们的主页:

When you press a hotkey, it captures and saves a snapshot of your screen to a JPG, GIF or BMP file.

当您按下热键时,它会捕获屏幕快照并将其保存为 JPG、GIF 或 BMP 文件。