darek_911
darek_911

Reputation: 77

Using the GDI+ API to draw an image

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

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

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?

Upvotes: 1

Views: 1107

Answers (1)

Oleg Korzhukov
Oleg Korzhukov

Reputation: 681

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

Try this:

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

Upvotes: 2

Related Questions