Redimenssion image avec hbitmap

Résolu
glipper Messages postés 246 Date d'inscription dimanche 2 juin 2002 Statut Membre Dernière intervention 11 septembre 2016 - 13 juil. 2006 à 00:08
gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010 - 13 juil. 2006 à 15:14
bonjour,

j'ai reussi à charger une image depuis un fichier, et la faire apparaître dans un boutons (dans une dialogbox). J'ai donc procédé comme ceci :

HBITMAP hbmp;
BITMAP bmp;
hbmp = FileToBitmap("./photos/photo.jpg");     // charge l'image depuis un fichier
GetObject(hbmp,sizeof(bmp),&bmp);      // apporte des informations sur la bitmap dont la taille en pixels

// on redimenssionne le bouton à la taille de la bitmap :
SetWindowPos(GetDlgItem(hwnd,IDC_PHOTO),NULL,20,60,bmp.bmWidth,bmp.bmHeight,SWP_SHOWWINDOW);

// on affiche le bitmap dans le bouton
SendMessage((GetDlgItem(hwnd, IDC_PHOTO)),BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)(HANDLE)(hbmp));

Mais dans le cas où l'image est trop grosse, je voudrais l'afficher à une taille inférieur à sa taille d'origine (miniature !). La fonction Bitblt fonctionne avec des HDC, moi j'ai un HBITMAP. Comment dois-je m'y prendre ?

Merci,
Glipper

5 réponses

gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010
13 juil. 2006 à 11:13
hDC = GetDC(hwnd);
hDC2 = CreateCompatibleDC(hDC);
hDC3 = CreateCompatibleDC(hDC);




hbmp_resized = CreateCompatibleBitmap(hDC, bmp.bmWidth / 2, bmp.bmHeight / 2);








SelectObject(hDC3, hbmp_resized);






SelectObject(hDC2, hbmp);








StretchBlt(hDC3, 0, 0, bmp.bmWidth / 2, bmp.bmHeight / 2, hDC2, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
3
gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010
13 juil. 2006 à 09:49
Crée un autre HBITMAP en utilisant:
- GetDC
- CreateCompatibleBitmap
- CreateCompatibleDC
- StretchBlt
0
glipper Messages postés 246 Date d'inscription dimanche 2 juin 2002 Statut Membre Dernière intervention 11 septembre 2016 1
13 juil. 2006 à 10:47
Voila ce que j'ai fait :

HBITMAP hbmp, hbmp_resized;
BITMAP bmp;
HDC hDC, hDC2;

hbmp = FileToBitmap("./photos/photo.jpg");    // charge l'image depuis un fichier
GetObject(hbmp,sizeof(bmp),&bmp);      // apporte des informations sur la bitmap dont la taille en pixels

hDC = GetDC(hwnd);
hDC2 = CreateCompatibleDC(hDC);
hbmp_resized = CreateCompatibleBitmap(hDC, bmp.bmWidth / 2, bmp.bmHeight / 2);

SelectObject(hDC, hbmp_resized);
SelectObject(hDC2, hbmp);

StretchBlt(hDC, 0, 0, bmp.bmWidth / 2, bmp.bmHeight / 2, hDC2, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);

SendMessage((GetDlgItem(hwnd, IDC_PHOTO)), BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)(HANDLE)(hbmp_resized));

....

L'image n'apparaît pas. Je pense que ça vient du SendMessage à la fin, ou je lui passe hbmp_resized comme dernier parametre...

Glipper
0
glipper Messages postés 246 Date d'inscription dimanche 2 juin 2002 Statut Membre Dernière intervention 11 septembre 2016 1
13 juil. 2006 à 11:38
Ok j'ai trouvé le probleme. il fallait faire ton code, et ensuite ne pas oublier de reselectionner l'objet par defaut :

hOldBmp2 = (HBITMAP) SelectObject(hDC3, hbmp_resized);
hOldBmp = (HBITMAP) SelectObject(hDC2, hbmp);

....SelectObject(hDC2, hOldBmp);
SelectObject(hDC3, hOldBmp2);

SendMessage((GetDlgItem(hwnd, IDC_PHOTO)), BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)(HANDLE)(hbmp_resized));

sinon, il n'apparait rien dans le bouton.

Merci beaucoup pour ton aide gagah1 tu m'as bien aidé.
Glipper
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010
13 juil. 2006 à 15:14
N'oublie pas de supprimer les handles créés après le SendMessage(...):
DeleteDC(hDC2);
DeleteDC(hDC3);
ReleaseDC(hwnd, hDC);
DeleteObject(hbmp_resized);
0
Rejoignez-nous