如何将屏幕截图直接保存到 Windows 中的文件? [英] How can I save a screenshot directly to a file in Windows?

查看:65
本文介绍了如何将屏幕截图直接保存到 Windows 中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种一键式的方式可以将屏幕截图直接保存到 Windows 中的文件中?


TheSoftwareJedi 准确地回答了 Windows 8 和 10 的上述问题.以下原始额外材料留给后人.<块引用>

这是一个非常重要的问题,因为截至 2021 年的观看次数为 316K.在 2008 年被问到,SO 在 2015 年左右关闭了这个问题,因为它是题外话,可能是因为下面的最后一个问题.

<块引用>

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

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

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

解决方案

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

这是开始捕获并保存到文件的内容.您只需要挂上打印屏幕"键即可.

使用系统;使用 System.Drawing;使用 System.IO;使用 System.Drawing.Imaging;使用 System.Runtime.InteropServices;公共类 CaptureScreen{static public void Main(string[] args){尝试{位图捕获 = CaptureScreen.GetDesktopImage();字符串文件 = Path.Combine(Environment.CurrentDirectory, "screen.gif");ImageFormat 格式 = ImageFormat.Gif;捕获.保存(文件,格式);}捕获(例外 e){Console.WriteLine(e);}}公共静态位图 GetDesktopImage(){WIN32_API.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);返回 System.Drawing.Image.FromHbitmap(m_HBitmap);}返回空;}受保护的静态 IntPtr m_HBitmap;}公共类 WIN32_API{公共结构尺寸{公共 int cx;公共信息;}公共常量 int SRCCOPY = 13369376;公共常量 int SM_CXSCREEN=0;公共常量 int SM_CYSCREEN=1;[DllImport("gdi32.dll",EntryPoint="DeleteDC")]public static extern IntPtr DeleteDC(IntPtr hDc);[DllImport("gdi32.dll",EntryPoint="DeleteObject")]公共静态外部 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")]公共静态外部 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(和其他键)的代码:

挂钩代码

Is there a one button way to save a screenshot directly to a file in Windows?


TheSoftwareJedi accurately answered above question for Windows 8 and 10. Below original extra material remains for posterity.

This is a very important question as the 316K views shows as of 2021. Asked in 2008, SO closed this question around 2015 as being off-topic, probably because of the last question below.

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 Paint.net or Photoshop, simply to paste an image, then save it?

解决方案

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

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

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

Hook code

这篇关于如何将屏幕截图直接保存到 Windows 中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆