c++ gdi+ 将资源文件bmp图像 加载到 Bitmap
时间: 2024-01-31 21:04:06 浏览: 188
在 C++ 中使用 GDI+ 加载位图可以通过以下步骤实现:
1. 在资源文件中添加位图资源,并为其分配一个 ID。
2. 在代码中使用 LoadBitmap 函数加载位图资源。
3. 将加载的位图赋值给 GDI+ 的 Bitmap 对象。
下面是一个示例代码:
```cpp
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 加载位图资源
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
// 创建 GDI+ Bitmap 对象
Bitmap* pBitmap = Bitmap::FromHBITMAP(hBitmap, NULL);
// 绘制位图
HDC hdc = GetDC(NULL);
Graphics graphics(hdc);
graphics.DrawImage(pBitmap, 0, 0);
// 释放资源
ReleaseDC(NULL, hdc);
delete pBitmap;
DeleteObject(hBitmap);
GdiplusShutdown(gdiplusToken);
return 0;
}
```
在这个示例中,我们使用 LoadBitmap 函数加载了位图资源,并通过 Bitmap::FromHBITMAP 函数创建了 GDI+ Bitmap 对象。最后,我们使用 Graphics::DrawImage 函数将位图绘制在屏幕上,并在退出程序时释放了相关资源。
阅读全文
相关推荐














