在WIN32中显示图像,为什么不显示?

时间:2011-04-27 06:15:22

标签: c++ winapi visual-c++

我想在我在窗口内创建的图片框中加载BitMap图像... picBoxDisp是使用以下机制创建的。

picBoxDisp = CreateWindow("STATIC", "image box",
                      WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
                      50, 50, 250, 300, hwnd , (HMENU)10000, NULL, NULL);

现在接下来我创建了一个hBitmap对象并将图像加载到它...

hBitmap = (HBITMAP) LoadImage(NULL,szFileName,IMAGE_BITMAP,0,0,
                              LR_LOADFROMFILE| LR_DEFAULTSIZE);

SendMessage(picBoxDisp,STM_SETIMAGE,(WPARAM) IMAGE_BITMAP,(LPARAM) NULL);   
//now assign the new image

//Create a compatible DC for the original size bitmap, for example originalMemDc.
HDC originalDC = GetDC((HWND)hBitmap);
HDC originalMemDC = CreateCompatibleDC(originalDC);
if(originalMemDC==NULL){
    MessageBox(NULL,"Problem while creating DC.","Error",MB_OK);
}
//Select hBitmap into originalMemDc.
SelectObject(originalMemDC,hBitmap);

//Create a compatible DC for the resized bitmap, for example resizedMemDc.
HDC picBoxDC = GetDC(picBoxDisp);
HDC resizedMemDC = CreateCompatibleDC(picBoxDC);

//Create a compatible bitmap of the wanted size for the resized bitmap,
HBITMAP hResizedBitmap = CreateCompatibleBitmap(picBoxDC,250,300);

//Select hResizedBitmap into resizedMemDc.
SelectObject(resizedMemDC,hResizedBitmap);

//Stretch-blit from originalMemDc to resizedMemDc.
//BitBlt(resizedMemDC,0,0,250,300,originalMemDC,0,0,SRCCOPY);

BITMAP bmp_old,bmp_new;
GetObject(hBitmap,sizeof(bmp_old),&bmp_old);
GetObject(hResizedBitmap,sizeof(bmp_new),&bmp_new);

StretchBlt ( resizedMemDC,0,0,bmp_new.bmWidth,bmp_new.bmHeight,
            originalMemDC,0,0,bmp_old.bmWidth,bmp_new.bmHeight,
            SRCCOPY);
////De-select the bitmaps.

if((resizedMemDC==NULL)||(hResizedBitmap == NULL)) {
    MessageBox(NULL,"Something is NULL","Error",MB_OK);
}
else
    //Set hResizedBitmap as the label image with STM_SETIMAGE
    SendMessage(picBoxDisp,STM_SETIMAGE, (WPARAM) IMAGE_BITMAP,(LPARAM) hResizedBitmap);

我只是无法理解为什么上面的代码无效?

提前致谢,

3 个答案:

答案 0 :(得分:5)

您误解了STM_SETIMAGE用法。这样做:

hBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP,
                               0, 0, LR_LOADFROMFILE| LR_DEFAULTSIZE);

if (hBitmap != NULL)
{
    ::SendMessage(picBoxDisp, STM_SETIMAGE,
                  (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap); 
}

编辑:如果要在将位图设置为标签图像之前调整位图大小,请按照此方案以最简单的方式执行此操作(在调整大小的图像中具有次优质量)。 ..):

  1. 为原始大小的位图创建兼容的DC,例如originalMemDc
  2. 选择hBitmaporiginalMemDc
  3. 为调整大小的位图创建兼容的DC,例如resizedMemDc
  4. 为调整大小的位图创建所需大小的兼容位图,例如hResizedBitmap
  5. 选择hResizedBitmapresizedMemDc
  6. originalMemDcresizedMemDc的拉伸blit。
  7. 取消选择位图。
  8. hResizedBitmap设为STM_SETIMAGE
  9. 的标签图片

    应该工作!

答案 1 :(得分:2)

静态控件不会将图像拉伸到其大小。您可以使用SS_CENTERIMAGE,但它会使用左上角像素的颜色剪切或填充空白区域(请参阅http://msdn.microsoft.com/en-US/library/b7w5x74z.aspx)。在将位图发送到静态控件之前,您必须先自己拉伸位图。

答案 2 :(得分:1)

您正尝试将图像指定给静态控件,因此您无需绘制图像,只需在其上设置图像。

// set the image
HBITMAP hold = (HBITMAP)SendMessage(hpicBoxDc, STM_SETIMAGE, IMAGE_BITMAP, LPARAM(hBitmap));

// clear the old image
if(hold && hold != hBitmap)
   DeleteObject(hold );