我用C ++编写了一个程序,它必须在屏幕上打印图像并在2秒后退出。在程序必须退出之前,一切都很好。当程序返回0时;指示,它崩溃了。我认为这是因为错误的处理,但代码似乎是正确的。
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
using namespace std;
using namespace Gdiplus;
HDC hdc = GetDC(NULL);
int main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Graphics graphics(hdc);
Rect dst(500, 500, 17, 17);
Image* image = Image::FromFile(L"image.png", false);
cout<<"Image size: "<<image->GetWidth()<<", "<<image->GetHeight()<<endl;
graphics.DrawImage(image, dst);
delete image;
GdiplusShutdown(gdiplusToken);
Sleep(2000);
return 0;
}
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
using namespace std;
using namespace Gdiplus;
HDC hdc = GetDC(NULL);
int main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Graphics graphics(hdc);
Rect dst(500, 500, 17, 17);
Image* image = Image::FromFile(L"image.png", false);
cout<<"Image size: "<<image->GetWidth()<<", "<<image->GetHeight()<<endl;
graphics.DrawImage(image, dst);
delete image;
GdiplusShutdown(gdiplusToken);
Sleep(2000);
return 0;
}
我有最新的编译器,我编译:
g ++ -c main.cpp -std = g ++ 11
g ++ -o main main.o -lgdiplus
答案 0 :(得分:1)
图形析构函数在GdiplusShutdown之后执行。你需要用
包围块Graphics graphics(hoc);
{} 中的