gpt4 book ai didi

c++ - WIN32显示图像,为什么不显示?

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

我想在我在窗口内创建的图片框中加载位图图像...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);

我只是不明白,为什么上面的代码不起作用?

提前致谢

最佳答案

您误解了 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. originalMemDc 拉伸(stretch)到 resizedMemDc
  7. 取消选择位图。
  8. 使用STM_SETIMAGE设置hResizedBitmap为标签图片

应该可以!

关于c++ - WIN32显示图像,为什么不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800167/

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