【问题标题】:Capture the Screen into a Bitmap将屏幕捕获为位图
【发布时间】:2008-12-12 14:57:59
【问题描述】:

我想在我的代码中捕获屏幕以获取图像 - 比如使用键盘上的“打印屏幕”按钮。

有人知道怎么做吗?我没有起点。

【问题讨论】:

    标签: c# .net screenshot


    【解决方案1】:

    如果使用 .NET 2.0(或更高版本)框架,您可以使用此处详述的 CopyFromScreen() 方法:

    http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

    //Create a new bitmap.
    var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                   Screen.PrimaryScreen.Bounds.Height,
                                   PixelFormat.Format32bppArgb);
    
    // Create a graphics object from the bitmap.
    var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    
    // Take the screenshot from the upper left corner to the right bottom corner.
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                Screen.PrimaryScreen.Bounds.Y,
                                0,
                                0,
                                Screen.PrimaryScreen.Bounds.Size,
                                CopyPixelOperation.SourceCopy);
    
    // Save the screenshot to the specified path that the user has chosen.
    bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
    

    【讨论】:

    • 您的答案不正确,请使用 ( g.CopyFromScreen( 0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 进行更新;
    • 是的,看看 .NET Screen 对象以获取其他屏幕。注意在上面的调用中我使用Screen.PrimaryScreen 来获取主屏幕?您可以使用Screen.AllScreens 来获取所有这些数组。 Screen.AllScreens[n].Bounds.Width 等等...
    • 我们在 2016 年,这仍然很有效。谢谢!
    • 如果更改显示设置更改所有项目的大小为更大,代码只能捕获部分屏幕。如何解决?
    【解决方案2】:

    我接受的答案有两个问题。

    1. 它不会捕获多显示器设置中的所有屏幕。
    2. 在使用显示缩放且您的应用程序未声明dpiAware 时,Screen 类返回的宽度和高度不正确。

    这是我使用 Screen.AllScreens 静态属性并使用 p/invoke 调用 EnumDisplaySettings 以获得真实屏幕分辨率的更新解决方案。

    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    class Program
    {
        const int ENUM_CURRENT_SETTINGS = -1;
    
        static void Main()
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                DEVMODE dm = new DEVMODE();
                dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
                EnumDisplaySettings(screen.DeviceName, ENUM_CURRENT_SETTINGS, ref dm);
    
                using (Bitmap bmp = new Bitmap(dm.dmPelsWidth, dm.dmPelsHeight))
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.CopyFromScreen(dm.dmPositionX, dm.dmPositionY, 0, 0, bmp.Size);
                    bmp.Save(screen.DeviceName.Split('\\').Last() + ".png");
                }
            }
        }
    
        [DllImport("user32.dll")]
        public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
    
        [StructLayout(LayoutKind.Sequential)]
        public struct DEVMODE
        {
            private const int CCHDEVICENAME = 0x20;
            private const int CCHFORMNAME = 0x20;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
            public string dmDeviceName;
            public short dmSpecVersion;
            public short dmDriverVersion;
            public short dmSize;
            public short dmDriverExtra;
            public int dmFields;
            public int dmPositionX;
            public int dmPositionY;
            public ScreenOrientation dmDisplayOrientation;
            public int dmDisplayFixedOutput;
            public short dmColor;
            public short dmDuplex;
            public short dmYResolution;
            public short dmTTOption;
            public short dmCollate;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
            public string dmFormName;
            public short dmLogPixels;
            public int dmBitsPerPel;
            public int dmPelsWidth;
            public int dmPelsHeight;
            public int dmDisplayFlags;
            public int dmDisplayFrequency;
            public int dmICMMethod;
            public int dmICMIntent;
            public int dmMediaType;
            public int dmDitherType;
            public int dmReserved1;
            public int dmReserved2;
            public int dmPanningWidth;
            public int dmPanningHeight;
        }
    }
    

    参考资料:

    https://stackoverflow.com/a/36864741/987968 http://pinvoke.net/default.aspx/user32/EnumDisplaySettings.html?diff=y

    【讨论】:

      【解决方案3】:
      // Use this version to capture the full extended desktop (i.e. multiple screens)
      
      Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, 
                                     SystemInformation.VirtualScreen.Height, 
                                     PixelFormat.Format32bppArgb);
      Graphics screenGraph = Graphics.FromImage(screenshot);
      screenGraph.CopyFromScreen(SystemInformation.VirtualScreen.X, 
                                 SystemInformation.VirtualScreen.Y, 
                                 0, 
                                 0, 
                                 SystemInformation.VirtualScreen.Size, 
                                 CopyPixelOperation.SourceCopy);
      
      screenshot.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
      

      【讨论】:

      • 您的代码没有输出正确的图像。它应该是 JPEG 还是 PNG?
      • 运行良好,但 UI 普遍冻结。对于需要每秒分析屏幕 10-20 次的项目(如我的)不兼容。
      【解决方案4】:

      试试this代码

      Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
      Graphics gr = Graphics.FromImage(bmp);
      gr.CopyFromScreen(0, 0, 0, 0, bmp.Size);
      pictureBox1.Image = bmp;
      bmp.Save("img.png",System.Drawing.Imaging.ImageFormat.Png);
      

      【讨论】:

        【解决方案5】:
        Bitmap memoryImage;
        //Set full width, height for image
        memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                               Screen.PrimaryScreen.Bounds.Height,
                               PixelFormat.Format32bppArgb);
        Size s = new Size(memoryImage.Width, memoryImage.Height);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
        string str = "";
        try
        {
            str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                  @"\Screenshot.png");//Set folder to save image
        }
        catch { };
        memoryImage.save(str);
        

        【讨论】:

        • 建议在您的代码中添加一些解释,以便阅读您的答案的人能够更好、更容易地理解。
        猜你喜欢
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 2012-01-16
        • 1970-01-01
        • 2013-06-16
        相关资源
        最近更新 更多