GDI+ VC++ Access Violation
-
Can someone please tell me why the following code is causing an access violation. I am using WTL 7.1 int DrawImage() { using namespace Gdiplus; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; WCHAR ImgFile[] = L"C:\\test\\test.bmp"; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Image* img = new Image(ImgFile, 0); Font myFont(L"Arial", 16); PointF origin(0.0f, 0.0f); SolidBrush blackBrush(Color(255, 0, 0, 0)); WCHAR string[] = L"This is a test"; Graphics g(img); g.DrawString(string, -1 , &myFont, origin, &blackBrush); // Get the class identifier for the BMP encoder. CLSID bmpClsid; ZeroMemory(&bmpClsid, sizeof(bmpClsid)); GetEncoderClsid(L"image/bmp", &bmpClsid); img->Save(L"C:\\test\\test1.bmp", &bmpClsid); GdiplusShutdown(gdiplusToken); return 0; } int CWTLTestView::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { unsigned int num = 0; // number of image encoders unsigned int size = 0; // size of the image encoder array in bytes Gdiplus::GetImageEncodersSize(&num, &size); if(size == 0)return -1; Gdiplus::ImageCodecInfo* imageCodecInfo = new Gdiplus::ImageCodecInfo[size]; Gdiplus::GetImageEncoders(num, size, imageCodecInfo); for(unsigned int i = 0; i < num; ++i) { if( wcscmp(imageCodecInfo[i].MimeType, format) == 0 ) { *pClsid = imageCodecInfo[i].Clsid; delete [] imageCodecInfo ; return i; } } delete [] imageCodecInfo; return -1; } Forever Developing
-
Can someone please tell me why the following code is causing an access violation. I am using WTL 7.1 int DrawImage() { using namespace Gdiplus; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; WCHAR ImgFile[] = L"C:\\test\\test.bmp"; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Image* img = new Image(ImgFile, 0); Font myFont(L"Arial", 16); PointF origin(0.0f, 0.0f); SolidBrush blackBrush(Color(255, 0, 0, 0)); WCHAR string[] = L"This is a test"; Graphics g(img); g.DrawString(string, -1 , &myFont, origin, &blackBrush); // Get the class identifier for the BMP encoder. CLSID bmpClsid; ZeroMemory(&bmpClsid, sizeof(bmpClsid)); GetEncoderClsid(L"image/bmp", &bmpClsid); img->Save(L"C:\\test\\test1.bmp", &bmpClsid); GdiplusShutdown(gdiplusToken); return 0; } int CWTLTestView::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { unsigned int num = 0; // number of image encoders unsigned int size = 0; // size of the image encoder array in bytes Gdiplus::GetImageEncodersSize(&num, &size); if(size == 0)return -1; Gdiplus::ImageCodecInfo* imageCodecInfo = new Gdiplus::ImageCodecInfo[size]; Gdiplus::GetImageEncoders(num, size, imageCodecInfo); for(unsigned int i = 0; i < num; ++i) { if( wcscmp(imageCodecInfo[i].MimeType, format) == 0 ) { *pClsid = imageCodecInfo[i].Clsid; delete [] imageCodecInfo ; return i; } } delete [] imageCodecInfo; return -1; } Forever Developing
-
After it leaves the the function. It happens after the function exits. The debugger breaks in gdiplusgraphics.h ~Graphics() { DllExports::GdipDeleteGraphics(nativeGraphics); } Thanks Nick Forever Developing
-
Can someone please tell me why the following code is causing an access violation. I am using WTL 7.1 int DrawImage() { using namespace Gdiplus; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; WCHAR ImgFile[] = L"C:\\test\\test.bmp"; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Image* img = new Image(ImgFile, 0); Font myFont(L"Arial", 16); PointF origin(0.0f, 0.0f); SolidBrush blackBrush(Color(255, 0, 0, 0)); WCHAR string[] = L"This is a test"; Graphics g(img); g.DrawString(string, -1 , &myFont, origin, &blackBrush); // Get the class identifier for the BMP encoder. CLSID bmpClsid; ZeroMemory(&bmpClsid, sizeof(bmpClsid)); GetEncoderClsid(L"image/bmp", &bmpClsid); img->Save(L"C:\\test\\test1.bmp", &bmpClsid); GdiplusShutdown(gdiplusToken); return 0; } int CWTLTestView::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { unsigned int num = 0; // number of image encoders unsigned int size = 0; // size of the image encoder array in bytes Gdiplus::GetImageEncodersSize(&num, &size); if(size == 0)return -1; Gdiplus::ImageCodecInfo* imageCodecInfo = new Gdiplus::ImageCodecInfo[size]; Gdiplus::GetImageEncoders(num, size, imageCodecInfo); for(unsigned int i = 0; i < num; ++i) { if( wcscmp(imageCodecInfo[i].MimeType, format) == 0 ) { *pClsid = imageCodecInfo[i].Clsid; delete [] imageCodecInfo ; return i; } } delete [] imageCodecInfo; return -1; } Forever Developing
Fixed my own problem need to free the Graphics, Font, Brush object before calling shutdown. Forever Developing