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. Calling GDI+ DrawImage function throws _BLOCK_TYPE_IS_VALID error

Calling GDI+ DrawImage function throws _BLOCK_TYPE_IS_VALID error

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsc++winformsdata-structureshelp
5 Posts 3 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.
  • K Offline
    K Offline
    Kiran Satish
    wrote on last edited by
    #1

    I am trying to use GDI+ within my MFC dialog based application with multiple child dialogs. On one of the child dialogs, I am trying to draw an image from an array of 8 bit BYTE pixel data. Here is how my flow process is for this- Variables declared in my child dialog's header file.

    LONG DIBSecWidth;
    LONG DIBSecHeight;
    LONG BitsPerPixel;
    LONG Stride;
    BITMAPINFO* bmi;
    BYTE* pDIBSectionBits;
    HBITMAP hbm;
    Graphics* imagedisp;
    Bitmap* offscreenBitmap;
    RECT m_imagedisp_rect;
    BOOL m_BUpdate;

    Then in my OnInitDialog() function, I do the following

    DIBSecWidth = 512;
    DIBSecHeight = 512;
    BitsPerPixel = 8;

    Stride = ((DIBSecWidth* BitsPerPixel + 31L) & (~31L)) / 8L;

    bmi = (BITMAPINFO *)new BYTE[sizeof(BITMAPINFO) + UCHAR_MAX * sizeof(RGBQUAD)];
    bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi->bmiHeader.biWidth = DIBSecWidth;
    bmi->bmiHeader.biHeight = DIBSecHeight;
    bmi->bmiHeader.biPlanes = 1;
    bmi->bmiHeader.biBitCount = (WORD)BitsPerPixel;
    bmi->bmiHeader.biCompression = BI_RGB;
    bmi->bmiHeader.biSizeImage = 0;
    bmi->bmiHeader.biXPelsPerMeter = 0;
    bmi->bmiHeader.biYPelsPerMeter = 0;
    bmi->bmiHeader.biClrUsed = 0;
    bmi->bmiHeader.biClrImportant = 0;
    memcpy(bmi->bmiColors, summercmap, 256*sizeof(RGBQUAD)); //summercmap is a color table of 256 RGB (COLORREF) values

    hbm = ::CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, (void**)&pDIBSectionBits, NULL, 0);

    offscreenBitmap = new Bitmap(bmi, pDIBSectionBits);
    ImageDisplay.GetClientRect(&m_imagedisp_rect);
    imagedisp = new Graphics(ImageDisplay.GetDC()->GetSafeHdc());
    m_BUpdate = true;

    and in OnPaint() message handler I have the following

    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    if (m_BUpdate)
    {
    BYTE *pCurRowPixel = (BYTE *)(pDIBSectionBits);
    memcpy(pCurRowPixel, m_pImgBuffPrc, DIBSecWidth*DIBSecHeight);
    imagedisp->DrawImage(offscreenBitmap, 0, 0, DIBSecWidth, DIBSecWidth);
    m_BUpdate = false;
    delete pCurRowPixel;
    }

    Whenever I run my application, it throws an error at imagedisplay->DrawImage function, if I comment it out it doesn't throw the error. I am sure there is something simple that I am missing here or the way I am trying to draw is not right, any ideas/suggestions? thanks

    PKNT

    CPalliniC 1 Reply Last reply
    0
    • K Kiran Satish

      I am trying to use GDI+ within my MFC dialog based application with multiple child dialogs. On one of the child dialogs, I am trying to draw an image from an array of 8 bit BYTE pixel data. Here is how my flow process is for this- Variables declared in my child dialog's header file.

      LONG DIBSecWidth;
      LONG DIBSecHeight;
      LONG BitsPerPixel;
      LONG Stride;
      BITMAPINFO* bmi;
      BYTE* pDIBSectionBits;
      HBITMAP hbm;
      Graphics* imagedisp;
      Bitmap* offscreenBitmap;
      RECT m_imagedisp_rect;
      BOOL m_BUpdate;

      Then in my OnInitDialog() function, I do the following

      DIBSecWidth = 512;
      DIBSecHeight = 512;
      BitsPerPixel = 8;

      Stride = ((DIBSecWidth* BitsPerPixel + 31L) & (~31L)) / 8L;

      bmi = (BITMAPINFO *)new BYTE[sizeof(BITMAPINFO) + UCHAR_MAX * sizeof(RGBQUAD)];
      bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
      bmi->bmiHeader.biWidth = DIBSecWidth;
      bmi->bmiHeader.biHeight = DIBSecHeight;
      bmi->bmiHeader.biPlanes = 1;
      bmi->bmiHeader.biBitCount = (WORD)BitsPerPixel;
      bmi->bmiHeader.biCompression = BI_RGB;
      bmi->bmiHeader.biSizeImage = 0;
      bmi->bmiHeader.biXPelsPerMeter = 0;
      bmi->bmiHeader.biYPelsPerMeter = 0;
      bmi->bmiHeader.biClrUsed = 0;
      bmi->bmiHeader.biClrImportant = 0;
      memcpy(bmi->bmiColors, summercmap, 256*sizeof(RGBQUAD)); //summercmap is a color table of 256 RGB (COLORREF) values

      hbm = ::CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, (void**)&pDIBSectionBits, NULL, 0);

      offscreenBitmap = new Bitmap(bmi, pDIBSectionBits);
      ImageDisplay.GetClientRect(&m_imagedisp_rect);
      imagedisp = new Graphics(ImageDisplay.GetDC()->GetSafeHdc());
      m_BUpdate = true;

      and in OnPaint() message handler I have the following

      CPaintDC dc(this); // device context for painting
      // TODO: Add your message handler code here
      if (m_BUpdate)
      {
      BYTE *pCurRowPixel = (BYTE *)(pDIBSectionBits);
      memcpy(pCurRowPixel, m_pImgBuffPrc, DIBSecWidth*DIBSecHeight);
      imagedisp->DrawImage(offscreenBitmap, 0, 0, DIBSecWidth, DIBSecWidth);
      m_BUpdate = false;
      delete pCurRowPixel;
      }

      Whenever I run my application, it throws an error at imagedisplay->DrawImage function, if I comment it out it doesn't throw the error. I am sure there is something simple that I am missing here or the way I am trying to draw is not right, any ideas/suggestions? thanks

      PKNT

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Quote:

      delete pCurRowPixel;

      What's the purpose of the above line? It looks plain wrong, to me.

      In testa che avete, signor di Ceprano?

      J 1 Reply Last reply
      0
      • CPalliniC CPallini

        Quote:

        delete pCurRowPixel;

        What's the purpose of the above line? It looks plain wrong, to me.

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        I saw that too. But it is guarded by m_BUpdate.

        CPalliniC 1 Reply Last reply
        0
        • J Jochen Arndt

          I saw that too. But it is guarded by m_BUpdate.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          The crashing call too is guarded by m_BUpdate. Anyway, it is wrong calling delete on the memory allocated via CreateDIBSection.

          In testa che avete, signor di Ceprano?

          J 1 Reply Last reply
          0
          • CPalliniC CPallini

            The crashing call too is guarded by m_BUpdate. Anyway, it is wrong calling delete on the memory allocated via CreateDIBSection.

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            CPallini wrote:

            Anyway, it is wrong calling delete on the memory allocated via CreateDIBSection.

            Which might be the reason for the exception because _BLOCK_TYPE_IS_VALID is thrown when trying to delete memory that wasn't allocated using new.

            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