使用BitBlt截屏,如何? [英] Using BitBlt to capture screenshot, how?

查看:67
本文介绍了使用BitBlt截屏,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索中不时遇到这个BitBlt",但我不知道如何使用它.

I have run into this "BitBlt" from time to time in my searched, but i don´t get how to use it.

据人们所说,这似乎是捕获 Windows 显示的屏幕的最快方法.但是,我自己不能说任何事情,因为我没有让它工作.

From what people say, it seems to be the fastest way to capture a screen that Windows show. However, i can´t say anything about that myself as i don´t got it working.

我唯一设法至少尝试的方法是:

The only thing i have manage to atleast, try the method, is with this:

 gfxBmp.CopyFromScreen(0,0,0,0 rc.Size,CopyPixelOperation.CaptureBlt);

我猜是哪个用的?(rc.size = 某个窗口的大小)可悲的是,它什么也没做,我得到了一张黑色的照片.但是,如果我使用 SourceCopy,它可以工作,但这是正常方法.

Which i guess uses it? (rc.size = size of a certain window) Sadly, it doesn´t do anything, i get a black picture. if i use SourceCopy however, it works, but that is the normal method.

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

I am currently trying to replace some code, to use BltBit, but it isn´t working that well either:

    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 框架中的代码:

You are right, Graphics.CopyFromScreen already uses BitBlt internally. Following, the code from the .NET 4.0 Framework:

// [...]
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 函数 PrintWindow.

There are other possibilities to capture screenshots. You could use as well the WinAPI function PrintWindow.

但是对于图形卡加速的内容,两者都不起作用.硬件覆盖位于 gpu 内存中,您无法访问它.这就是为什么您经常在视频、游戏、...

But for graphiccard-accelerated content, both won't work. The hardware overlay resides in the gpu-memory, where you can't access it. That's why you often get a black image for videos, games, ...

这篇关于使用BitBlt截屏,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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