sauvegarder un HBITMAP ou BITMAP
Bonjour, j'aimerais sauvegarder une image BMP via un simple flux.
Cette image à été récupérée d'une caméra IP au format jpeg puis convertit en BMP.
J'ai testé avec le code suivant, cela fonctionne pour mon image jpeg mais pas pour mon image BMP.
Code:
1 2 3 4
| ofstream fichier;
fichier.open("fichier.bmp",ios::binary);
fichier << this->hbmp;
fichier.close(); |
Voici ma fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| HRESULT hr;
CoInitialize(0);
HBITMAP hbmp_dst = 0;
SIZE_T nSize = this->tailleImage + 1;
// copie image jpeg dans global
HGLOBAL hgbl =(HGLOBAL)GlobalAlloc(GMEM_FIXED, nSize);
memcpy(hgbl, &this->bufImage[0], nSize);
// création du stream d'échange
IStream* stream = 0;
// image jpeg dans global dans stream
hr = CreateStreamOnHGlobal(hgbl, TRUE, &stream);
if(!SUCCEEDED(hr) || !stream) { // si erreur libération des objets déja crées
GlobalFree(hgbl);
CoUninitialize();
return false;
} else {
IPicture* picture = 0;
// conversion stream vers picture
hr = OleLoadPicture(stream, nSize, 0, IID_IPicture, (void**)&picture);
if(!SUCCEEDED(hr) || !picture) { // si erreur libération des objets déja crées
stream->Release();
//GlobalFree(hgbl);
CoUninitialize();
return false;
} else {
// recuperation du handle de la 'picture'
HBITMAP hbmp_src;
picture->get_Handle((OLE_HANDLE *)&hbmp_src);
// recuperation du handle du bitmap de la 'picture'
BITMAP bmp;
GetObject(hbmp_src, sizeof bmp, &bmp);
// bmp est le bitmap resultant mais son pointeur vers le contenu pointe vers le contenu de 'picture'
// comme on va dechargé la 'picture' on copie dans une autre zone memoire
hbmp_dst = (HBITMAP)CopyImage(hbmp_src, IMAGE_BITMAP, 0, 0, 0);
picture->Release();
stream->Release();
//GlobalFree(hgbl);
CoUninitialize();
this->hbmp = hbmp_dst; |