首页 > 解决方案 > 如何将 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+ 中的代码,但事实并非如此。尽管 的参数与DrawImageGDI 的参数相同,但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);

如果我更改UnitPixelUnitDisplay,我会收到来自 的无效参数错误DrawImage

标签: c++bitmapgdi+gdi

解决方案


推荐阅读