Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying image in WIN32, Why its not displayed?

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,

like image 374
Amit Avatar asked Feb 24 '23 09:02

Amit


2 Answers

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...):

  1. Create a compatible DC for the original size bitmap, for example originalMemDc.
  2. Select hBitmap into originalMemDc.
  3. Create a compatible DC for the resized bitmap, for example resizedMemDc.
  4. Create a compatible bitmap of the wanted size for the resized bitmap, for example hResizedBitmap.
  5. Select hResizedBitmap into resizedMemDc.
  6. Stretch-blit from originalMemDc to resizedMemDc.
  7. De-select the bitmaps.
  8. Set hResizedBitmap as the label image with STM_SETIMAGE

Should work!

like image 173
Johann Gerell Avatar answered Feb 27 '23 00:02

Johann Gerell


The static control won't stretch the image to its size. You could use SS_CENTERIMAGE but it either clips or fills the empty space with the color of the top left pixel (see http://msdn.microsoft.com/en-US/library/b7w5x74z.aspx). You'd have to stretch the bitmap yourself before sending it to the static control.

like image 24
Marius Bancila Avatar answered Feb 26 '23 22:02

Marius Bancila