C# 如何使用BitBlt捕获屏幕截图?

C# 如何使用BitBlt捕获屏幕截图?,c#,printing,bitblt,C#,Printing,Bitblt,在我的搜索中,我经常遇到这个“BitBlt”,但我不知道如何使用它 据人们说,这似乎是捕获Windows显示的屏幕的最快方式。 然而,我自己也不能说什么,因为我没有让它工作 我能做到的唯一一件事就是,尝试一下这种方法,就是: gfxBmp.CopyFromScreen(0,0,0,0 rc.Size,CopyPixelOperation.CaptureBlt); 我猜是哪个用的? (rc.size=某个窗口的大小) 可悲的是,它没有任何作用,我得到了一张黑色的照片。 但是,如果我使用Sour

在我的搜索中,我经常遇到这个“BitBlt”,但我不知道如何使用它

据人们说,这似乎是捕获Windows显示的屏幕的最快方式。 然而,我自己也不能说什么,因为我没有让它工作

我能做到的唯一一件事就是,尝试一下这种方法,就是:

 gfxBmp.CopyFromScreen(0,0,0,0 rc.Size,CopyPixelOperation.CaptureBlt);
我猜是哪个用的? (rc.size=某个窗口的大小) 可悲的是,它没有任何作用,我得到了一张黑色的照片。 但是,如果我使用SourceCopy,它可以工作,但这是正常的方法

我目前正在尝试替换一些代码,以使用BltBit,但它也不能很好地工作:

    public MemoryStream CaptureWindow(IntPtr hwnd, EncoderParameters JpegParam)
    {
        NativeMethods.Rect rc;
        NativeMethods.GetWindowRect(hwnd, out rc);
        using (Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
        {

            using (Graphics gfxBmp = Graphics.FromImage(bmp))
            {
                IntPtr hdcBitmap = gfxBmp.GetHdc();
                try
                { 

                    NativeMethods.BitBlt(hdcBitmap, 0, 0, 0, 0, hwnd, 0, 0, 0xCC0020);

                }
                finally
                {
                    gfxBmp.ReleaseHdc(hdcBitmap);
                }
            }
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, GetEncoderInfo(ImageFormat.Jpeg), JpegParam);

            return ms;
        }
    }
你说得对, Graphics.CopyFromScreen已在内部使用BitBlt。下面是.NET 4.0框架中的代码:

// [...]
new UIPermission(UIPermissionWindow.AllWindows).Demand();
int width = blockRegionSize.Width;
int height = blockRegionSize.Height;
using (DeviceContext deviceContext = DeviceContext.FromHwnd(IntPtr.Zero))
{
    HandleRef hSrcDC = new HandleRef(null, deviceContext.Hdc);
    HandleRef hDC = new HandleRef(null, this.GetHdc());
    try
    {
        if (SafeNativeMethods.BitBlt(hDC, destinationX, destinationY, width, height, hSrcDC, sourceX, sourceY, (int)copyPixelOperation) == 0)
        {
            throw new Win32Exception();
        }
    }
    finally
    {
        this.ReleaseHdc();
    }
}
还有其他捕获截图的可能性。您也可以使用WinAPI函数


但对于graphiccard加速内容,两者都不起作用。硬件覆盖层驻留在gpu内存中,您无法访问它。这就是为什么你经常在视频、游戏中看到黑色图像的原因,

是吗?它是否与SourceCopy一起使用它?对于代码,我不知道如何在我的代码中实现它(我文章中的另一段代码)。我目前正在使用PrintWindow,但是,我对它有点问题,因为它迫使窗口一直刷新。有些应用程序不喜欢这样,代码只是CopyFromScreen代码的摘录。用ILSpy()自己看看。啊,那我就明白你为什么那样写了。嗯,尽管如此,我还是需要让它工作起来,因为我只有一个黑屏,不管我用BlBit捕捉到什么(可以是桌面或其他)。