gdiplusshutdown

场景:GdiPlus 使用的有关问题(GdiplusShutdown抛错误)

GdiPlus 使用的问题(GdiplusShutdown抛异常)
下列代码是我在一个MFC单视图模式下对CView派生类的OnDraw函数的重载,希望在视图中显示一张图片:
void CMy6_7View::OnDraw(CDC* pDC)
{
  GdiplusStartupInput gdiplusStartupInput;
  ULONG_PTR gdiplusToken;
   
  // Initialize GDI+.
  GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

  Graphics pGraph(pDC->GetSafeHdc());
  Image *pImage = Image::FromFile(L"ABC.bmp");
  if (NULL == pImage)
  {
  AfxMessageBox(_T("Fail"));
  }
  pGraph.DrawImage(pImage, 0, 0);

  delete pImage;
  pImage = NULL;

  GdiplusShutdown(gdiplusToken);
}

程序执行时抛出异常,位置是在调用GdiplusShutdown(gdiplusToken);时,Graphics的析构函数抛出的:
~Graphics()
{
  DllExports::GdipDeleteGraphics(nativeGraphics); // 在这儿抛出了异常
}

将GdiplusShutdown(gdiplusToken);注释掉之后没有了异常但也没有将图片显示出来

麻烦请了解的大侠给讲解一下,谢谢!

------解决方案--------------------
不调用 GdiplusShutdown 是可以画图的。
DrawImage中height、width、坐标都指定一下。
------解决方案--------------------
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
GdiplusShutdown(gdiplusToken);
这两个不要放在ondraw里面,分别放在app的初始化与退出里面!
在本程序中,你先调GdiplusShutdown(gdiplusToken);而后执行Graphics的析构函数,所以,出错!如果实在要放到里面,这样:

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{

Graphics pGraph(pDC->GetSafeHdc());
Image *pImage = Image::FromFile(L"ABC.bmp");
if (NULL == pImage)
{
AfxMessageBox(_T("Fail"));
}
pGraph.DrawImage(pImage, 0, 0);

delete pImage;
pImage = NULL;
}

GdiplusShutdown(gdiplusToken);

加一对大括号来管理pGraph的生命周期!





------解决方案--------------------
探讨
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
GdiplusShutdown(gdiplusToken);
这两个不要放在ondraw里面,分别放在app的初始化与退出里面!
在本程序中,你先调GdiplusShutdown(gdiplusToken);而后执行Graphics的析构函数,所以,出错!……