我使用Target Machine 64位在VS2013 .Net 4中重新编译了现有的VC ++代码。编译很好,但由于某种原因,SetDIBits无法正常工作。图像黑了。这些是单色图像。 VS2008 .Net 2.0 32位编译使用相同的精确代码。任何想法都会有所帮助。
示例代码如下(此函数获取IntPtr句柄,然后用于其他绘图):
IntPtr GetGraphics(Bitmap ^src, BitmapData ^pData, IntPtr ^phBitmapOldIntPtr) {
HDC hdc;
IntPtr scan0;
BitmapData ^data;
System::Drawing::Rectangle rectSrc = System::Drawing::Rectangle(0, 0, src->Width, src->Height);
BYTE *pBits;
IntPtr pixels, hBitmapIntPtr, hdcIntPtr;
BITMAPINFO *pbmi;
int iScanLines;
int iCopied;
HBITMAP hBitmapSrc, hBitmapOld;
hdc = CreateCompatibleDC(NULL);
hBitmapSrc = CreateCompatibleBitmap(hdc, src->Width, src->Height);
data = src->LockBits(rectSrc, ImageLockMode::ReadWrite, src->PixelFormat);
pixels = data->Scan0;
pBits = (BYTE*)pixels.ToPointer();
//ZeroMemory(&pbmi, sizeof(BITMAPINFO));
pbmi = (BITMAPINFO*)GlobalAlloc(GMEM_FIXED, sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiHeader.biHeight = -src->Height;
pbmi->bmiHeader.biWidth = src->Width;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 0;
pbmi->bmiHeader.biXPelsPerMeter = 300;
pbmi->bmiHeader.biYPelsPerMeter = 300;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
pbmi->bmiColors[0].rgbBlue = 0;
pbmi->bmiColors[0].rgbGreen = 0;
pbmi->bmiColors[0].rgbRed = 0;
pbmi->bmiColors[1].rgbBlue = 255;
pbmi->bmiColors[1].rgbGreen = 255;
pbmi->bmiColors[1].rgbRed = 255;
iScanLines = src->Height;
iCopied = SetDIBits(hdc, hBitmapSrc, 0, iScanLines, pBits, pbmi, DIB_RGB_COLORS);
GlobalFree(pbmi);
if (pData) pData = data;
hBitmapOld = (HBITMAP)SelectObject(hdc, hBitmapSrc);
phBitmapOldIntPtr = IntPtr(hBitmapOld);
hdcIntPtr = IntPtr((void*)hdc);
return hdcIntPtr;
}
提前致谢!
答案 0 :(得分:0)
整个功能可以替换为:
src->Clone()
将位图复制到兼容的位图Graphics::FromImage()
使图形与克隆GetHdc()
,以获得现有绘图功能的HDC