在WIN32中显示图像,为什么不显示? [英] Displaying image in WIN32, Why its not displayed?
问题描述
我想在窗口中创建的pic框中加载BitMap图像... picBoxDisp是使用以下机制创建的。
picBoxDisp = CreateWindow(STATIC,image box,
WS_VISIBLE | WS_CHILD | SS_BITMAP | WS_TABSTOP | WS_BORDER,
50,50,250,300,hwnd,(HMENU) 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);
//现在分配新图像
//为原始大小的位图创建一个兼容的DC,例如originalMemDc。
HDC originalDC = GetDC((HWND)hBitmap);
HDC originalMemDC = CreateCompatibleDC(originalDC);
if(originalMemDC == NULL){
MessageBox(NULL,创建DC时出现问题,错误,MB_OK);
}
//将hBitmap选择为originalMemDc。
SelectObject(originalMemDC,hBitmap);
//为调整大小的位图创建兼容的DC,例如resizedMemDc。
HDC picBoxDC = GetDC(picBoxDisp);
HDC resizedMemDC = CreateCompatibleDC(picBoxDC);
//为调整大小的位图创建一个兼容的位图,
HBITMAP hResizedBitmap = CreateCompatibleBitmap(picBoxDC,250,300);
//将hResizedBitmap选择为resizedMemDc。
SelectObject(resizedMemDC,hResizedBitmap);
//从originalMemDc到resizedMemDc的Stretch-blit。
// 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);
////取消选择位图。
if((resizedMemDC == NULL)||(hResizedBitmap == NULL)){
MessageBox(NULL,Something is NULL,Error,MB_OK)
}
else
//使用STM_SETIMAGE
设置hResizedBitmap作为标签图像SendMessage(picBoxDisp,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hResizedBitmap);
我不明白为什么上述代码不起作用? / p>
提前感谢,
您误解了 STM_SETIMAGE
用法。请执行以下操作:
hBitmap =(HBITMAP):: LoadImage(NULL,szFileName,IMAGE_BITMAP,
0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
if(hBitmap!= NULL)
{
:: SendMessage(picBoxDisp,STM_SETIMAGE,
(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
}
EDIT:如果要调整位图在将其设置为标签图像之前,请按照此方案进行最简单的操作(在调整大小的图像中具有次优质量...):
- 为原始大小的位图创建兼容的DC,例如
originalMemDc
。 - 选择
hBitmap
intooriginalMemDc
。 - 为调整大小的位图创建兼容DC,例如
resizedMemDc
。 - 为调整大小的位图创建所需大小的兼容位图,例如
hResizedBitmap
。 - 在
resizedMemDc
中选择hResizedBitmap
- 从
原始MemDc
到resizedMemDc
的弹跳。 - 取消选择位图。
- 将
hResizedBitmap
设置为STM_SETIMAGE
应该有效!
I want to load a BitMap image in a pic box I created inside a window...picBoxDisp is created using following mechanism..
picBoxDisp = CreateWindow("STATIC", "image box",
WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
50, 50, 250, 300, hwnd , (HMENU)10000, NULL, NULL);
Now Next I created a hBitmap object and loaded an image in to it...
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);
I just cant understand, why the above code is not working ?
Thanks in advance,
You misunderstood the STM_SETIMAGE
usage. Do this:
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);
}
EDIT: If you want to resize the bitmap before setting it as the label image, then follow this scheme for the simplest possible way to do it (with sub-optimal quality in the resized image...):
- Create a compatible DC for the original size bitmap, for example
originalMemDc
. - Select
hBitmap
intooriginalMemDc
. - Create a compatible DC for the resized bitmap, for example
resizedMemDc
. - Create a compatible bitmap of the wanted size for the resized bitmap, for example
hResizedBitmap
. - Select
hResizedBitmap
intoresizedMemDc
. - Stretch-blit from
originalMemDc
toresizedMemDc
. - De-select the bitmaps.
- Set
hResizedBitmap
as the label image withSTM_SETIMAGE
Should work!
这篇关于在WIN32中显示图像,为什么不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!