C# 拍摄MEmu的截图

C# 拍摄MEmu的截图,c#,bitmap,screenshot,C#,Bitmap,Screenshot,我下载了一个带有便捷方法的类,我用它拍摄了一段时间的其他程序的截图 using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace EmulatorObserver.model { /// <summary> /// Provides functions to capture the entire

我下载了一个带有便捷方法的类,我用它拍摄了一段时间的其他程序的截图

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

namespace EmulatorObserver.model
{
    /// <summary>
    ///     Provides functions to capture the entire screen, or a particular window, and save it to a file.
    /// </summary>
    public static class ScreenCapture
    {
        /// <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 static Bitmap CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = GetWindowDC(handle);
            // get the size
            RECT windowRect = new RECT();
            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 = CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = SelectObject(hdcDest, hBitmap);
            // bitblt over
            BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
            // restore selection
            SelectObject(hdcDest, hOld);
            // clean up 
            DeleteDC(hdcDest);
            ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            Bitmap raw;
            using(Image img = Image.FromHbitmap(hBitmap))
            {
                raw = (Bitmap)img.Clone();
            }

            // free up the Bitmap object
            DeleteObject(hBitmap);

            return raw;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        // Win32 API
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

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

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

        [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("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);

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

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);

        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

    }
}
使用系统;
使用系统图;
使用系统、绘图、成像;
使用System.Runtime.InteropServices;
命名空间EmulatorObserver.model
{
/// 
///提供捕获整个屏幕或特定窗口并将其保存到文件的功能。
/// 
公共静态类截屏
{
/// 
///创建包含特定窗口的屏幕快照的图像对象
/// 
///窗口的句柄。(在windows窗体中,这是通过句柄属性获得的)
/// 
公共静态位图捕获窗口(IntPtr句柄)
{
//获取目标窗口的te hDC
IntPtr hdcSrc=GetWindowDC(句柄);
//知道尺寸了吗
RECT windowRect=new RECT();
GetWindowRect(句柄,参照windowRect);
int width=windowRect.right-windowRect.left;
int height=windowRect.bottom-windowRect.top;
//创建可以复制到的设备上下文
IntPtr hdcDest=CreateCompatibleDC(hdcSrc);
//创建一个我们可以复制到的位图,
//使用GetDeviceCaps获取宽度/高度
IntPtr hBitmap=CreateCompatibleBitmap(HDCSC、宽度、高度);
//选择位图对象
IntPtr hOld=SelectObject(hdcDest、hBitmap);
//结束
BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,SRCCOPY);
//恢复选择
选择对象(hdcDest,保持);
//清理
DeleteDC(hdcDest);
释放DC(手柄,hdcSrc);
//为它获取一个.NET图像对象
位图原始;
使用(Image img=Image.FromHbitmap(hBitmap))
{
raw=(位图)img.Clone();
}
//释放位图对象
删除对象(hBitmap);
返回原材料;
}
////////////////////////////////////////////////////////////////////////////////////////////
//Win32 API
[StructLayout(LayoutKind.Sequential)]
公共结构矩形
{
公共int左;
公共int top;
公共权利;
公共int底部;
}
public const int SRCCOPY=0x00CC0020;//BitBlt dwRop参数
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetWindowRect(IntPtr hWnd,ref RECT RECT);
[DllImport(“gdi32.dll”)]
公共静态外部bool BitBlt(IntPtr hObject、intnxtest、intnydest、intnwidth、intnheight、IntPtr hObjectSource、intnxsrc、intnysrc、intdwrop);
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetWindowDC(IntPtr hWnd);
[DllImport(“gdi32.dll”)]
公共静态外部布尔删除DC(IntPtr hDC);
[DllImport(“gdi32.dll”)]
公共静态外部bool DeleteObject(IntPtr-hObject);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr SelectObject(IntPtr hDC、IntPtr hObject);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr CreateCompatibleBitmap(IntPtr hDC、int nWidth、int nHeight);
[DllImport(“user32.dll”)]
公共静态外部IntPtr ReleaseDC(IntPtr hWnd、IntPtr hDC);
[DllImport(“gdi32.dll”)]
公共静态外部IntPtr CreateCompatibleDC(IntPtr hDC);
}
}
到目前为止,它工作得很好,但当尝试拍摄Android模拟器MEmu的屏幕截图时

生成的图片如下所示

运行“ScreenCapture.CaptureWindow(h).Save(@“screenshot.bmp”);”时,什么内容不能截图?我用什么方法(快速)拍到这些照片