使用GDI+ API 绘制图像

Using the GDI+ API to draw an image

我使用 GDI+ API 只是为了在屏幕上显示图像 (bmp)。这是执行任务的函数代码:

void drawImg_ArmorInfo_PupupWnd(HDC hdc) {

    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    Image* image = new Image(L"C:/Users/Darek/F2 stuff/art/intrface/armor_info_1.bmp");
    int a1 = image->GetWidth();
    int a2 = image->GetHeight();

    Graphics graphics(hdc);
    graphics.DrawImage(image, 0, 0);

    delete image;
    GdiplusShutdown(gdiplusToken);
}

问题是我遇到异常:Access violation writing location 0xXXXXXXXX 在调用 GdiplusShutdown 函数后。当我注释掉

时有什么有趣的
Graphics graphics(hdc);
graphics.DrawImage(image, 0, 0);

部分程序运行没有任何问题,但当然没有绘制图像。当我注释掉对 GdiplusShutdown 函数的调用时 - 再次没有问题。

代码有什么问题,问题的原因是什么?

graphicsGdiplusShutdown 之后超出范围,因此无法正确析构。

试试这个:

Graphics * graphics = new Graphics(hdc);
graphics->DrawImage(image, 0, 0);
delete graphics;