c++ - 如何将 GDI 位图转换为 GDI+ 位图、DrawImage?
问题描述
我正在将 C++ pgm 从 GDI 转换为 GDI+,以便利用 GDI+ 中的一些新功能。但我遇到了一个奇怪的问题:GDI+ 绘制的图像看起来大了约 1.3 倍,并且在屏幕上的位置略有不同。
这是 GDI 中的代码,它按预期工作——它将一个矩形从位图中复制到屏幕上,其大小与原始位图中的大小相同。我想知道如何让 GDI+ 做同样的事情。
HDC hdc = GetDC(hWnd);
HANDLE hDCMem = CreateCompatibleDC(hdc);
HANDLE hBitmap = LoadImage(NULL, L"C:\\MyBitmap.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HANDLE hSelect = SelectObject(hDCMem, hBitmap);
// In OnPaint:
// pt is the destination, in screen pixels px,py are the position in the source bitmap w and h are the size of the source and destination rectangles
DWORD rop = SRCCOPY;
bool ok = BitBlt(hdc, pt.x,pt.y, w, h, hDCMem, px, py, rop);
这是我希望做同样事情的 GDI+ 中的代码,但事实并非如此。尽管 的参数与DrawImage
GDI 的参数相同,但BitBlt
我得到了不同的结果。
GdiplusStartupInput gdiplusStartupInput;
Status gdiStatus=GdiplusStartup(&GDIPlusToken, &gdiplusStartupInput, NULL); // GDI+ version is 1
// Get the bitmap
Bitmap *tBitmap;
tBitmap = new Bitmap(L"C:\\MyBitmap.bmp");
Status st = tilesBitmap->GetLastStatus(); // ok
// Display part of the image
In OnPaint:
Graphics graphics(hdc);
// pt is the destination, in screen pixels px,py are the position in the source bitmap w and h are the size of the source and destination rectangles
Status st = graphics.DrawImage(tBitmap, (int)pt.x, (int)pt.y, px, py, w, h, UnitPixel);
如果我更改UnitPixel
为UnitDisplay
,我会收到来自 的无效参数错误DrawImage
。
解决方案
推荐阅读
- micronaut - NoSuchBeanException 在 Micronaut 中访问 RequestScope bean
- android - 行号面板尺寸大
- java - ArrayList 对象混杂在一起
- visual-studio-code - 如何让 VScode 自动修复缺少的分号 Apex?
- c - SetWindowsHook 的 HOOKPROC 中的 LPARAM 是什么?
- sql-server - 从 sql server 中的 JSON 中提取值
- solidity - 如何将 UniswapV2Router02 接口导入 Truffle 上的智能合约?
- html - 笔记本电脑和手机中媒体查询的最佳宽度范围是多少
- android - android studio canary 中的 build.gradle 文件
- firebase - 无法将数据从 Stripe webhook 发送到 Firebase