我想在现有引擎中创建农作物功能。这就是我已经拥有的:

bool Bitmap::Crop(RECT cropArea)
{
BITMAP bm;
GetObject(m_Handle, sizeof(bm), &bm);

HDC hSrc = CreateCompatibleDC(NULL);
SelectObject(hSrc, m_Handle);

HDC hNew = CreateCompatibleDC(NULL);
HBITMAP hBmp = CreateCompatibleBitmap(hNew, bm.bmWidth, bm.bmHeight);
HBITMAP hOld = (HBITMAP)SelectObject(hNew, hBmp);

BitBlt(hNew, 0, 0, bm.bmWidth, bm.bmHeight, hSrc, 0, 0, SRCCOPY);

SelectObject(hNew, hOld);

DeleteDC(hSrc);
DeleteDC(hNew);

DeleteObject(m_Handle);

m_Handle = hBmp;
}

我希望它只能将整个图像复制到新的HBITMAP,然后用它替换旧图像。所以我知道它有效。之后,它只是使用BITBLT参数。

m_handle是类位图的HBITMAP。

此代码的结果只是一个黑屏。

有帮助吗?

解决方案

切勿从“新鲜”内存DC创建兼容的位图。除非那是您要创建一个1BPP位图 - 在新内存DC中选择的默认位图为1x1 1BPP位图 - 因此,您创建的任何兼容位图都可以匹配。这确实会导致全黑输出。

您在HSRC中的颜色位图,因此请使用 DC制作新的位图。

其他提示

感谢您帮助我。该功能现在正常工作。

bool Bitmap::Crop(RECT cropArea)
{
HDC hSrc = CreateCompatibleDC(NULL);
SelectObject(hSrc, m_Handle);

HDC hNew = CreateCompatibleDC(hSrc);
HBITMAP hBmp = CreateCompatibleBitmap(hSrc, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top); 
HBITMAP hOld = (HBITMAP)SelectObject(hNew, hBmp);

bool retVal = (BitBlt(hNew, 0, 0, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top, hSrc, cropArea.left, cropArea.top, SRCCOPY))?true:false;

SelectObject(hNew, hOld);

DeleteDC(hSrc);
DeleteDC(hNew);

DeleteObject(m_Handle);

m_Handle = hBmp;

return retVal;
}

两个小变化:

HBITMAP hBmp = CreateCompatibleBitmap(hNew, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top); 

BitBlt(hNew, 0, 0, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top, hSrc, cropArea.left, cropArea.top, SRCCOPY); 

您可能希望进行更多检查,以确保所请求的区域落在原始位图的大小之内。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top