C#屏幕截图

C#屏幕截图,c#,screenshot,C#,Screenshot,我正在尝试使用Visual Studio 12(抱歉,我们必须使用12)C#捕获屏幕。 我在这里找到了一个算法: 我尝试了此操作,但收到一条错误消息,表示找不到类型或命名空间。 这并不意外。我想我只需要在顶部再写一句“使用…”的话。 我试图搜索要“使用”哪个软件包来获取屏幕截图(),但我空手而归。有人知道吗?根据您需要使用的Windows.Media.Capture。请注意 重要的 只有运行Windows Phone 8.1的移动设备才支持屏幕捕获功能。Windows 10不支持此API 您可以

我正在尝试使用Visual Studio 12(抱歉,我们必须使用12)C#捕获屏幕。 我在这里找到了一个算法:

我尝试了此操作,但收到一条错误消息,表示找不到类型或命名空间。 这并不意外。我想我只需要在顶部再写一句“使用…”的话。 我试图搜索要“使用”哪个软件包来获取屏幕截图(),但我空手而归。有人知道吗?

根据您需要使用的
Windows.Media.Capture
。请注意

重要的

只有运行Windows Phone 8.1的移动设备才支持屏幕捕获功能。Windows 10不支持此API


您可以检查哪些在所有windows桌面平台上都有效。

我认为这可能给您在使用System.Runtime.InteropServices时出现了一个错误


您需要添加DLL System.Runtime.InteropServices的引用

此库在NuGet上似乎不存在。但是,您可以在找到它

下载部分只包含一个包含以下类的zip文件,您可以在VS项目中复制粘贴

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace ScreenShotDemo
{
    /// <summary>
    /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
    /// </summary>
    public class ScreenCapture
    {
        /// <summary>
        /// Creates an Image object containing a screen shot of the entire desktop
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreen() 
        {
            return CaptureWindow( User32.GetDesktopWindow() );
        }

        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle,ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height); 
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest,hOld);
            // clean up 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle,hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return img;
        }

        /// <summary>
        /// Captures a screen shot of a specific window, and saves it to a file
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) 
        {
            Image img = CaptureWindow(handle);
            img.Save(filename,format);
        }

        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format) 
        {
            Image img = CaptureScreen();
            img.Save(filename,format);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
                int nWidth,int nHeight,IntPtr hObjectSource,
                int nXSrc,int nYSrc,int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, 
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }

            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);

        }


    }
}
使用系统;
使用System.Runtime.InteropServices;
使用系统图;
使用系统、绘图、成像;
名称空间截图演示
{
/// 
///提供捕获整个屏幕或特定窗口并将其保存到文件的功能。
/// 
公共类截图
{
/// 
///创建包含整个桌面的屏幕快照的图像对象
/// 
/// 
公共图像捕获屏幕()
{
返回CaptureWindow(User32.GetDesktopWindow());
}
/// 
///创建包含特定窗口的屏幕快照的图像对象
/// 
///窗口的句柄。(在windows窗体中,这是通过句柄属性获得的)
/// 
公共图像捕获窗口(IntPtr句柄)
{
//获取目标窗口的te hDC
IntPtr hdcSrc=User32.GetWindowDC(句柄);
//知道尺寸了吗
User32.RECT windowRect=新的User32.RECT();
User32.GetWindowRect(句柄,ref windowRect);
int width=windowRect.right-windowRect.left;
int height=windowRect.bottom-windowRect.top;
//创建可以复制到的设备上下文
IntPtr hdcDest=GDI32.CreateCompatibleDC(hdcSrc);
//创建一个我们可以复制到的位图,
//使用GetDeviceCaps获取宽度/高度
IntPtr hBitmap=GDI32.CreateCompatibleBitmap(hdcSrc,宽度,高度);
//选择位图对象
IntPtr hOld=GDI32。选择对象(hdcDest、hBitmap);
//结束
GDI32.BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,GDI32.srcopy);
//恢复选择
GDI32.选择对象(hdcDest,保持);
//清理
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(句柄,hdcSrc);
//为它获取一个.NET图像对象
图像img=图像。来自hBitmap(hBitmap);
//释放位图对象
GDI32.DeleteObject(hBitmap);
返回img;
}
/// 
///捕获特定窗口的屏幕快照,并将其保存到文件中
/// 
/// 
/// 
/// 
public void CaptureWindowToFile(IntPtr句柄、字符串文件名、ImageFormat格式)
{
图像img=捕获窗口(手柄);
保存(文件名、格式);
}
/// 
///捕获整个桌面的屏幕快照,并将其保存到文件中
/// 
/// 
/// 
public void CaptureScreenToFile(字符串文件名、图像格式)
{
图像img=CaptureScreen();
保存(文件名、格式);
}
/// 
///包含Gdi32 API函数的帮助器类
/// 
私有类GDI32
{
public const int SRCCOPY=0x00CC0020;//BitBlt dwRop参数
[DllImport(“gdi32.dll”)]
公共静态外部bool BitBlt(IntPtr hObject、intnxtest、intnydest、,
int nWidth、int nHeight、IntPtr hObjectSource、,
int nXSrc、int nYSrc、int dwRop);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
内特(右);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport(“gdi32.dll”)]
公共静态外部布尔删除DC(IntPtr hDC);
[DllImport(“gdi32.dll”)]
公共静态外部bool DeleteObject(IntPtr-hObject);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr SelectObject(IntPtr hDC、IntPtr hObject);
}
/// 
///包含user32api函数的Helper类
/// 
私有类User32
{
[StructLayout(LayoutKind.Sequential)]
公共结构矩形
{
公共int左;
公共int top;
公共权利;
公共int底部;
}
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetDesktopWindow();
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetWindowDC(IntPtr hWnd);
[DllImport(“user32.dll”)]
公共静态外部IntPtr ReleaseDC(IntPtr hWnd、IntPtr hDC);
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetWindowRect(IntPtr hWnd,ref RECT RECT);
}
}
}

如果您正在查看-它仅在Win mobile上可用。请按照顶部答案上的链接(来自您链接的问题)进行操作。
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace ScreenShotDemo
{
    /// <summary>
    /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
    /// </summary>
    public class ScreenCapture
    {
        /// <summary>
        /// Creates an Image object containing a screen shot of the entire desktop
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreen() 
        {
            return CaptureWindow( User32.GetDesktopWindow() );
        }

        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle,ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height); 
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest,hOld);
            // clean up 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle,hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return img;
        }

        /// <summary>
        /// Captures a screen shot of a specific window, and saves it to a file
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) 
        {
            Image img = CaptureWindow(handle);
            img.Save(filename,format);
        }

        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format) 
        {
            Image img = CaptureScreen();
            img.Save(filename,format);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
                int nWidth,int nHeight,IntPtr hObjectSource,
                int nXSrc,int nYSrc,int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, 
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }

            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);

        }


    }
}