所以我编写了一个加载位图的应用程序。但我想以一种所有这些都具有相同大小的方式拉伸加载的位图。如何使用StretchBlt实现这样的功能?这是我处理位图的函数:
hBitmap = (HBITMAP)::LoadImageA(NULL, userSelectedFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{
::MessageBox(NULL, TEXT("LoadImage Failed"), TEXT("Error"), MB_OK);
return false;
}
HDC hLocalDC;
hLocalDC = ::CreateCompatibleDC(hWinDC);
if (hLocalDC == NULL)
{
::MessageBox(NULL, TEXT("CreateCompatibleDC Failed"), TEXT("Error"), MB_OK);
return false;
}
BITMAP qBitmap;
int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP), reinterpret_cast<LPVOID>(&qBitmap));
if (!iReturn)
{
::MessageBox(NULL, TEXT("GetObject Failed"), TEXT("Error"), MB_OK);
return false;
}
HBITMAP hOldBmp = (HBITMAP)::SelectObject(hLocalDC, hBitmap);
if (hOldBmp == NULL)
{
::MessageBox(NULL, TEXT("SelectObject Failed"), TEXT("Error"), MB_OK);
return false;
}
BOOL qRetBlit = ::BitBlt(hWinDC, 0, 0, qBitmap.bmWidth, qBitmap.bmHeight, hLocalDC, 0, 0, SRCCOPY);
if (!qRetBlit)
{
::MessageBox(NULL, TEXT("Blit Failed"), TEXT("Error"), MB_OK);
return false;
}
::SelectObject(hLocalDC, hOldBmp);
::DeleteDC(hLocalDC);
::DeleteObject(hBitmap);
return true;
我是否必须用BitBlt替换StretchBlt?
更新:我设法让StretchBlt工作,但显然我的所有图像都相互重叠。这是迄今为止的代码:
hBitmap = (HBITMAP)::LoadImageA(NULL, myFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
// Verify that the image was loaded
if (hBitmap == NULL)
{
::MessageBox(NULL, TEXT("LoadImage Failed"), TEXT("Error"), MB_OK);
return false;
}
HDC hLocalDC;
hLocalDC = ::CreateCompatibleDC(hWinDC);
// Verify that the device context was created
if (hLocalDC == NULL)
{
::MessageBox(NULL, TEXT("CreateCompatibleDC Failed"), TEXT("Error"), MB_OK);
return false;
}
BITMAP qBitmap;
int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP), reinterpret_cast<LPVOID>(&qBitmap));
if (!iReturn)
{
::MessageBox(NULL, TEXT("GetObject Failed"), TEXT("Error"), MB_OK);
return false;
}
HBITMAP hOldBmp = (HBITMAP)::SelectObject(hLocalDC, hBitmap);
if (hOldBmp == NULL)
{
::MessageBox(NULL, TEXT("SelectObject Failed"), TEXT("Error"), MB_OK);
return false;
}
/*BOOL qRetBlit = ::BitBlt(hWinDC, xPos, yPos, qBitmap.bmWidth, qBitmap.bmHeight, hLocalDC, 0, 0, SRCCOPY);
if (!qRetBlit)
{
::MessageBox(NULL, TEXT("Blit Failed"), TEXT("Error"), MB_OK);
return false;
}*/
sx = GetSystemMetrics(SM_CXSCREEN);
sy = GetSystemMetrics(SM_CXSCREEN);
BOOL qStretchBlit = StretchBlt(hWinDC, 0, 0, sx/2, sy/2, hLocalDC, 0, 0, sx, sy, SRCCOPY);
if (!qStretchBlit)
{
MessageBox(NULL, TEXT("StretchBlt Failed"), TEXT("Error"), MB_OK);
return false;
}
// Adjust positioning (not perfect)
if (iOldCounter > iCounter)
{
xPos += MOVE_X_POS;
if (xPos >= NEW_ROW_POS)
{
xPos = 0;
yPos += MOVE_Y_POS;
}
}
::SelectObject(hLocalDC, hOldBmp);
::DeleteDC(hLocalDC);
::DeleteObject(hBitmap);
return true;
思想?
答案 0 :(得分:1)
我已经解决了我最初的问题。以下是我采取的步骤: