使用 GDI+ API 绘制图像 [英] Using the GDI+ API to draw an image

查看:24
本文介绍了使用 GDI+ API 绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the GDI+ API just to display an image (bmp) on the screen. Here is the code of the function doing the task:

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);
}

问题是我在调用 GdiplusShutdown 函数后得到一个异常:访问冲突写入位置 0xXXXXXXXX.当我注释掉

The problem is that I'm getting an exception: Access violation writing location 0xXXXXXXXX after calling the GdiplusShutdown function. What's interesting when I comment out the

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

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

part the program runs without any problems but, of course, the image isn't drawn. When I comment out the call to GdiplusShutdown function - no problems again.

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

What's wrong with the code and what can be the reason of the problem here?

推荐答案

graphicsGdiplusShutdown 之后超出范围,因此无法正确销毁.

graphics falls out of the scope after the GdiplusShutdown so it cannot destruct correctly.

试试这个:

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

这篇关于使用 GDI+ API 绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆