Calling GDI+ DrawImage function throws _BLOCK_TYPE_IS_VALID error
-
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) valueshbm = ::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
-
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) valueshbm = ::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
-
Quote:
delete pCurRowPixel;
What's the purpose of the above line? It looks plain wrong, to me.
I saw that too. But it is guarded by
m_BUpdate
. -
I saw that too. But it is guarded by
m_BUpdate
. -
The crashing call too is guarded by
m_BUpdate
. Anyway, it is wrong callingdelete
on the memory allocated viaCreateDIBSection
.CPallini wrote:
Anyway, it is wrong calling
delete
on the memory allocated viaCreateDIBSection
.Which might be the reason for the exception because
_BLOCK_TYPE_IS_VALID
is thrown when trying to delete memory that wasn't allocated usingnew
.