c# - BitBlt 代码不工作

标签 c# .net gdi+ pinvoke bitblt

我正在尝试使用此代码将 Bitmap 直接绘制到 PictureBox 上:

Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Users\Ken\Desktop\Load2.bmp");
Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle);
Graphics grSrc = Graphics.FromImage(bmp);
IntPtr hdcDest = grDest.GetHdc();
IntPtr hdcSrc = grSrc.GetHdc();
BitBlt(hdcDest, 0, 0, pictureBox1.Width, pictureBox1.Height,
    hdcSrc, 0, 0, (uint)TernaryRasterOperations.SRCCOPY); // 0x00CC0020
grDest.ReleaseHdc(hdcDest);
grSrc.ReleaseHdc(hdcSrc);

但不是渲染 Bitmap 的内容,它只是绘制一个接近黑色的实心 block 。我很确定问题出在源 hDC 上,因为如果我在上面的代码中将 SRCCOPY 更改为 WHITENESS,它会按预期绘制一个纯白色 block 。

注意:下一个片段可以正常工作,因此位图本身没有任何问题:

Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Users\Ken\Desktop\Load2.bmp");
pictureBox1.Image = bmp;

最佳答案

这是因为在使用 SelectObject 之前,设备上下文包含一个 1x1 黑色位图。无论出于何种原因,Graphics.FromImage 都会为您提供与位图兼容的设备上下文,但它不会自动将位图选择到设备上下文中。

以下代码将使用SelectObject

当然,如果可能,您应该使用托管的 Graphics.DrawImage 而不是 BitBlt,但我认为您有充分的理由使用 BitBlt

private void Draw()
{
    using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Jason\forest.jpg"))
    using (Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle))
    using (Graphics grSrc = Graphics.FromImage(bmp))
    {
        IntPtr hdcDest = IntPtr.Zero;
        IntPtr hdcSrc = IntPtr.Zero;
        IntPtr hBitmap = IntPtr.Zero;
        IntPtr hOldObject = IntPtr.Zero;

        try
        {
            hdcDest = grDest.GetHdc();
            hdcSrc = grSrc.GetHdc();
            hBitmap = bmp.GetHbitmap();

            hOldObject = SelectObject(hdcSrc, hBitmap);
            if (hOldObject == IntPtr.Zero)
                throw new Win32Exception();

            if (!BitBlt(hdcDest, 0, 0, pictureBox1.Width, pictureBox1.Height,
                hdcSrc, 0, 0, 0x00CC0020U))
                throw new Win32Exception();
        }
        finally
        {
            if (hOldObject != IntPtr.Zero) SelectObject(hdcSrc, hOldObject);
            if (hBitmap != IntPtr.Zero) DeleteObject(hBitmap);
            if (hdcDest != IntPtr.Zero) grDest.ReleaseHdc(hdcDest);
            if (hdcSrc != IntPtr.Zero) grSrc.ReleaseHdc(hdcSrc);
        }
    }
}

[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern System.IntPtr SelectObject(
    [In()] System.IntPtr hdc,
    [In()] System.IntPtr h);

[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject(
    [In()] System.IntPtr ho);

[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BitBlt(
    [In()] System.IntPtr hdc, int x, int y, int cx, int cy,
    [In()] System.IntPtr hdcSrc, int x1, int y1, uint rop);

关于c# - BitBlt 代码不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2302550/

相关文章:

c++ - 如何使用 GDI+ 从资源加载图像?

c# - 基准测试 .Net 正则表达式选项 - 或者 RightToLeft 做什么?

c++ - 如何在gdi+中绘制 "pouring"效果?

c# - 将图像缩放到多种尺寸以进行深度缩放

.net - Docket.DotNet 的 Docker 桌面问题,第二次调用 _dockerClient.Containers.StartContainerAsync() 将失败并出现 OperationCanceledException

c# - 您可以在选择项目之前在 linq 查询中设置属性吗?

.net - 在 MVVM 中创建 ViewModel 的最佳方法

c# - FileSystemWatcher 网络断开

c# - 使用 LINQ 查询 HTML 文件

c# - ASP.NET Core 如何将任何类型转换为 Controller 操作的 ActionResult<T> 返回类型?