单色位图C ++ GDIPlus

时间:2011-04-21 22:13:05

标签: c++ visual-studio-2010 gdi+

我目前在向表单显示单色位图时遇到问题。我相信我不知何故错误地设置了我的位图,但我无法看到目前的位置。我需要做的就是“修复”这个问题,将biBitCount设置为24,但是当我调用DWORD d = GetCharacterPlacementA后,我看到后来损坏的内存。所以我试图保持我对单色位图的原始需求,而不是泄漏内存。

目前我将位图信息存储在标准变量中:

BITMAPINFO bi24BitInfo;
HDC hMemoryDC;
HBITMAP hMemoryBitmap;
HGDIOBJ hDefaultBitmap;
HBITMAP hGdiBitmap;

这就是我设置位图的方式:

hMemoryDC = CreateCompatibleDC(NULL);

bi24BitInfo.bmiHeader.biSize = sizeof(bi24BitInfo.bmiHeader);   // size of this struct
bi24BitInfo.bmiHeader.biWidth = 600;//sizeRect.cx;      // width of window
bi24BitInfo.bmiHeader.biHeight = 600;//sizeRect.cy; // height of window
bi24BitInfo.bmiHeader.biPlanes = 1;
bi24BitInfo.bmiHeader.biBitCount = 1;               // monochrome // rgb 8 bytes for each component(3)
bi24BitInfo.bmiHeader.biCompression = BI_RGB;   // Means its uncompressed. Has nothing to do with color.
bi24BitInfo.bmiHeader.biSizeImage = 0;
bi24BitInfo.bmiHeader.biXPelsPerMeter = pelsPerMeter;
bi24BitInfo.bmiHeader.biYPelsPerMeter = pelsPerMeter;
bi24BitInfo.bmiHeader.biClrUsed = 2;
bi24BitInfo.bmiHeader.biClrImportant = 0;
bi24BitInfo.bmiColors[0].rgbBlue = 0;
bi24BitInfo.bmiColors[0].rgbRed = 0;
bi24BitInfo.bmiColors[0].rgbGreen = 0;
bi24BitInfo.bmiColors[0].rgbReserved = 0;
bi24BitInfo.bmiColors[1].rgbBlue = (BYTE)0xFF;
bi24BitInfo.bmiColors[1].rgbRed = (BYTE)0xFF;
bi24BitInfo.bmiColors[1].rgbGreen = (BYTE)0xFF;
bi24BitInfo.bmiColors[1].rgbReserved = 0;

// Create the memory bitmap
if (hMemoryBitmap != NULL)
{
    DeleteObject(hMemoryBitmap);
}
hMemoryBitmap = CreateCompatibleBitmap(hMemoryDC, 600, 600);
hDefaultBitmap = SelectObject(hMemoryDC, hMemoryBitmap);
HGDIOBJ hOldFont = SelectObject(hMemoryDC, GetStockObject(NULL_BRUSH));
// Do not fill background
int nOldBkMode = GetBkMode(hMemoryDC);
SetBkMode(hMemoryDC, TRANSPARENT);

int nRet(0);

if (hDIB != NULL)
{
    GlobalFree(hDIB);
}
DWORD dwBmpSize = ((bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biBitCount + 31) / 32) * 4 * bi24BitInfo.bmiHeader.biHeight;
//DWORD dwBmpSize = ((bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight)/8);
// Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that call HeapAlloc using a handle
// to the process's default heap. Therefore, GlobalAlloc and LocalAlloc have greater overhead than HeapAlloc.
hDIB = GlobalAlloc(GHND, dwBmpSize);
DWORD D = GetLastError();
pBytes = (BYTE*)GlobalLock(hDIB);
D = GetLastError();
nRet = GetDIBits(hMemoryDC, hMemoryBitmap, 0, (UINT)bi24BitInfo.bmiHeader.biHeight, pBytes, (BITMAPINFO*)&bi24BitInfo, DIB_RGB_COLORS);
D = GetLastError();
if (pBitmap != NULL)
{
    delete pBitmap;
    pBitmap = NULL;
}
pBitmap = new Gdiplus::Bitmap(&bi24BitInfo, pBytes);
D = GetLastError();
GlobalUnlock(hDIB);
D = GetLastError();

然后在所有这些之后,我收到以下错误:

pGraphics = Graphics::FromImage(pBitmap);
// set a graphics property
s = pGraphics->SetTextRenderingHint(TextRenderingHintAntiAlias);

在此pGraphics->之后,LastResult现在设置为“OutOfMemory”,并且s设置为枚举“InvalidParameter”来自:http://msdn.microsoft.com/en-us/library/ms534175(v=VS.85).aspx

任何有关这方面的帮助都会受到关注。

1 个答案:

答案 0 :(得分:0)

BITMAPINFO仅包含一个调色板条目的空间;当你写到bi24BitInfo.bmiColors[1]时,你正在破坏后面的记忆,可能就是hMemoryDC。通过在前面创建另一个包含BITMAPINFO的结构和后面的调色板数组来解决此问题。

编辑:您似乎无法从单色位图创建图形对象。来自Graphics::FromImage文档:

  

如果图像,此方法也会失败   具有以下像素之一   格式:

     
      
  • PixelFormatUndefined
  •   
  • PixelFormatDontCare
  •   
  • PixelFormat1bppIndexed
  •   
  • PixelFormat4bppIndexed
  •   
  • PixelFormat8bppIndexed
  •   
  • PixelFormat16bppGrayScale
  •   
  • PixelFormat16bppARGB1555
  •