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

时间:2020-03-06 14:58:51  来源:igfitidea点击:

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

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

我想知道:有没有一种方法可以将屏幕截图直接保存到文件中?我真的必须打开图像程序(例如Paint.net或者Photoshop),只是粘贴图像然后保存吗?

解决方案

据我在XP中所知,是的,我们必须使用其他一些应用程序才能真正保存它。

Vista附带了Snipping工具,可简化此过程!

无需安装屏幕截图自动保存实用程序,是的。我们可以找到几个实用程序,但可以这样做。

例如:http://www.screenshot-utility.com/

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

这是开始捕获并保存到文件的内容。我们只需要钩住"打印屏幕"键即可。

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);
}

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

挂钩代码

当然,我们可以编写一个监视剪贴板的程序,并为剪贴板中的每个图像显示烦人的SaveAs对话框;-)。我猜我们甚至可以找出是否最后按下的键是PrintScreen来限制误报的数量。

当我在考虑它时..我们也可以在Google上搜索已经做到这一点的人。

编辑:..或者等待某人在这里发布源代码:-)

我们需要在XP中使用第三方屏幕抓取实用程序来实现该功能。我在斯科特·汉塞尔曼(Scott Hanselman)的博客中广泛介绍了一些很酷的工具,通常会在那儿找到这样的实用程序-可以肯定的是,他在这里写了几篇关于博客的文章。

Snagit ...很多技术人员都在使用它。

我们可能需要这样的东西:http://addons.mozilla.org/en-US/firefox/addon/5648

我认为有一个适用于IE的版本,还具有Explorer Explorer集成的版本。相当不错的软件。

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

对于Windows 8和更高版本

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

对于Windows 7

在win 7中,只需使用截屏工具:最容易的操作是通过按开始,然后键入" sni"(输入)。或者
然后`s进入

Windows的早期版本

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

  • Alt + PrintScreen
  • Win+R(" run")
  • 输入" mspaint"enter
  • Ctrl -V(粘贴)
  • Ctrl-S(保存)
  • 使用文件对话框
  • Alt-F4(关闭mspaint)

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

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

的确,除了旁边,我们还需要第三方软件来制作屏幕截图,但是我们无需为此而触发大型Photoshop。诸如IrfanWiew或者XnView之类的免费且轻量级的工具可以完成此任务。我使用MWSnap复制屏幕的任意部分。我写了一个小的AutoHotkey脚本来调用GDI +函​​数来截屏。等等。

这将在Delphi中完成。请注意,BitBlt函数是Windows API调用,而不是Delphi特有的函数。

编辑:添加了示例用法

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;

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

从他们的主页:

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

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

感谢所有的源代码和感谢,我终于有了一个我想要的应用程序:)

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

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

我使用InterceptCaptureScreen.exe只是在命令提示符终端中运行它,然后在要捕获屏幕快照(带有可执行文件的目录中带有时间戳的文件名png)时按Insert。即使终端没有对准焦点,也会捕获键。

(我使用插入键,因为它比通过PrintScreen传播VNC的时间要短,例如在笔记本电脑上要求同时按下Fn键并且不会通过VNC传播的PrintScreen。源代码中使用的实际密钥)。

希望这可以帮助,
干杯!