gpt4 book ai didi

c++ - 单色位图 C++ GDIPlus

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:01:28 30 4
gpt4 key购买 nike

我目前在向我的表单显示单色位图时遇到问题。我相信我以某种方式错误地设置了我的位图,但我现在看不到它在哪里。要“修复”这个问题,我所要做的就是将 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

如有任何帮助,我们将不胜感激。

最佳答案

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

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

This method also fails if the image has one of the following pixel formats:

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

关于c++ - 单色位图 C++ GDIPlus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5750330/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com