Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GDI+ VC++ Access Violation

GDI+ VC++ Access Violation

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicswinformsdata-structures
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nicholas Cardi
    wrote on last edited by
    #1

    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

    A N 2 Replies Last reply
    0
    • N Nicholas Cardi

      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

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      What line is causing the AV?

      N 1 Reply Last reply
      0
      • A Anonymous

        What line is causing the AV?

        N Offline
        N Offline
        Nicholas Cardi
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • N Nicholas Cardi

          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

          N Offline
          N Offline
          Nicholas Cardi
          wrote on last edited by
          #4

          Fixed my own problem need to free the Graphics, Font, Brush object before calling shutdown. Forever Developing

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups