Home > Software engineering >  Using the GDI API to draw an image
Using the GDI API to draw an image

Time:09-18

I'm using the GDI API just to display an (bmp) image 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 is'tr 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?

CodePudding user response:

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;
  • Related