- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用此代码将 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/
我正在使用下面的代码使用 bltblt 捕获窗口的屏幕截图。但是,标题栏在捕获的屏幕截图中显示为完全黑色。我在 Windows 8.1 上运行代码。有没有一种方法可以正确捕获标题栏。 // Retri
我有一个将多页 tiff 拆分为单页的函数,它使用 Windows BitBlt 函数。在性能方面,显卡对拆分有影响吗?是否值得直接使用 C/C++ 库? 最佳答案 显卡不会参与任何事件,除非它是 B
我正在尝试编写一个程序,当勾选一个复选框时,它会在屏幕的大面积区域显示一个网格。我正在尝试使用 BitBlt 在 WM_PAINT 中执行此操作,并且我的显示函数肯定是使用正确的 bool 值调用的。
我正在使用 MFC,我正在尝试将图像绘制到屏幕上。我有以下 OnDraw 函数: void CgraphicstestView::OnDraw(CDC* pDC) { CgraphicstestDo
我正在将剪贴板中的图像粘贴到我的程序中。位图被放置在背景 memDC 位图上,然后在绘制到屏幕之前与另一个 memDC 合成。背景位图是每像素 8 位。问题是 bitblt 将 Black(0x000
我正在使用 BitBlt 在我的按钮上显示位图。对于大多数情况,它很好,但是存在内存泄漏,导致程序在一段时间后崩溃。我做错了什么? int Springboard::DrawBasicButtons(
我正在为一个简单的 Direct3D 游戏制作闪屏,尽管屏幕本身已正确创建和销毁,但未显示要投影到闪屏上的 BITMAP。到目前为止我有这个: //variable declarations HWND
我有一个 MFC 控件,我将一个句柄传递给一个位图 (HBITMAP)。在控件 OnPaint 方法中,我使用 BitBlt 来呈现位图。但是位图被颠倒了。 作为测试,我从这个句柄创建了一个 CBit
我正在尝试在 Windows 10 下使用 GDI 捕获屏幕的正方形部分。这就是我尝试的方式: //Get screen DC desktop = GetDC(NULL); //Create a co
我想在现有引擎中创建裁剪功能。这是我已经拥有的: bool Bitmap::Crop(RECT cropArea) { BITMAP bm; GetObject(m_Handle, sizeof(bm
我正在尝试使用此代码将 Bitmap 直接绘制到 PictureBox 上: Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Users\Ken\Desktop\L
我正在尝试绘制到屏幕外设备上下文/位图,并使用 bitblt 将图像移动到主 hdc。这是我目前看到的结果: 左边的蓝色、黄色和绿色条被直接绘制到窗口的 hdc。右边看起来很奇怪的那些被绘制到后台缓冲
我想创建一个幻灯片效果:在窗体的 Canvas 上从右到左绘制一个位图。为此,我使用 BitBlt。 我在定时器(20 毫秒)中调用此函数: var ViewPort: TRect; ViewPort
您好我试图捕获屏幕截图,但图像出来损坏,任何人都可以看到代码有什么问题,基本上我试图使用createdibsection以便我可以直接访问这些位。 这就是结果图片 http://oi47.tinypi
将一个 QImage 复制到 Qt4 中特定位置的另一个 QImage 的推荐方法是什么? QImage::bitblt 在 Qt4 中被删除。 QImage 现在需要转换为 PixMap 并返回吗?
我在这个程序中使用 BitBlt 时遇到问题。您调整窗口大小,椭圆也会随之调整大小。当然,用普通的hdc方法,是断断续续的。我尝试了 BitBlt 方法,但这也不起作用(可能是因为我做错了)。有人可以
这是我目前正在做的: 通过GetWindowDC获取窗口DC 使用 CreateCompatibleDC 创建一个兼容的 DC 在我的兼容 DC 上调用 GetPixel 不幸的是,我所有的 GetP
现在我正在使用 GetPixel() 从桌面检索大约 64 个像素以获得它们的颜色。我读到有关 GetPixel() 的信息很慢,但我认为这对几个像素并不重要,但每次我运行例程都需要 1.5 秒。在做
我编写了以下代码 (C++ Win32) 来捕获游戏窗口屏幕并从图像中获取像素颜色数组。函数 autoB() 完成这项工作。 然后我将结果数组绘制到我的窗口中以直观地检查我得到了什么。 问题是这个程序
我目前正在使用 Petzold 的书(第 5 版)回到一些 Windows 编程。我编译了following example使用 BitBlt 但它无法正常工作。 它应该复制 (CxSource, C
我是一名优秀的程序员,十分优秀!