Format bmp 24 bits

nicolovitch Messages postés 20 Date d'inscription jeudi 21 juillet 2005 Statut Membre Dernière intervention 5 juin 2007 - 16 mai 2007 à 14:34
SAKingdom Messages postés 3212 Date d'inscription lundi 7 novembre 2005 Statut Membre Dernière intervention 16 février 2009 - 16 mai 2007 à 20:09
Bonjour à tous,


 <?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>


J'ai un code qui me permet de faire un screenshoot de mon ecran, le problème est qu'il suavegarde l'image dans un fichier BMP en 32 bits alors que je le voudrais en 24 bits ...


 


voici le code :








int hwnd_to_bmp(HWND hwnd, char *pszflname)





{






  HDC memdc;






  HANDLE hfl;






  
DWORD dwBytes, dwNumColors;



  
void *pBits;






  HBITMAP hbmp;






  

BITMAPFILEHEADER fileheader;






  BITMAPINFOHEADER infoheader;






  RGBQUAD colors[256];






  BITMAPINFO bmpinfo;






  HGDIOBJ hret;






  RECT rct;





 






  HDC hdc = GetDC(hwnd);





 






  GetWindowRect(hwnd, &amp;rct);






  rct.bottom -= rct.top + 579;                          






  rct.right -= rct.left + 774;                           






  rct.top = GetDeviceCaps(hdc, BITSPIXEL);






  if(rct.top <= 8)






    dwNumColors = 256;






  else






    dwNumColors = 0;






  if(!(memdc = CreateCompatibleDC(hdc)))






  
  goto relHwndDc;





 






  bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);






  bmpinfo.bmiHeader.biWidth = rct.right;






  bmpinfo.bmiHeader.biHeight = rct.bottom;






  bmpinfo.bmiHeader.biPlanes = 1;






  bmpinfo.bmiHeader.biBitCount = (WORD) rct.top;






  bmpinfo.bmiHeader.biCompression = BI_RGB;






  bmpinfo.bmiHeader.biSizeImage = 0;






  bmpinfo.bmiHeader.biXPelsPerMeter = 0;






  bmpinfo.bmiHeader.biYPelsPerMeter = 0;






  bmpinfo.bmiHeader.biClrUsed = dwNumColors;






  bmpinfo.bmiHeader.biClrImportant = dwNumColors;






  hbmp = CreateDIBSection(hdc, &amp;bmpinfo, DIB_PAL_COLORS, &amp;pBits, NULL, 0);





 






  if(!hbmp) goto errato;






  hret = SelectObject(memdc, hbmp);






  if(!hret || (hret == HGDI_ERROR)) goto errato;





 






  if(!BitBlt(memdc, 0, 0, rct.right, rct.bottom, hdc, 558, 126, SRCCOPY)) goto errato;






  








  
if(dwNumColors)



    dwNumColors = GetDIBColorTable(memdc, 0, dwNumColors, colors);


 



  fileheader.bfType = 0x4D42;



  rct.left = dwNumColors * sizeof(RGBQUAD);



  fileheader.bfSize = ((rct.right * rct.bottom * rct.top) >> 3) + rct.left + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);



  fileheader.bfReserved1 fileheader.bfReserved2 0;



  fileheader.bfOffBits = rct.left + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);



  infoheader.biSize = sizeof(BITMAPINFOHEADER);



  infoheader.biWidth = rct.right;



  infoheader.biHeight = rct.bottom;



  infoheader.biPlanes = 1;



  infoheader.biBitCount = (WORD) rct.top;



  infoheader.biCompression = BI_RGB;



  infoheader.biSizeImage infoheader.biClrImportant 0;



  infoheader.biXPelsPerMeter infoheader.biYPelsPerMeter 0;



  infoheader.biClrUsed = dwNumColors;


 



  hfl = CreateFile(pszflname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);


 



  if (hfl == INVALID_HANDLE_VALUE) {DeleteObject(hbmp); goto errato;}


 



  WriteFile(hfl, &amp;fileheader, sizeof(BITMAPFILEHEADER), &amp;dwBytes, 0);



  WriteFile(hfl, &amp;infoheader, sizeof(BITMAPINFOHEADER), &amp;dwBytes, 0);



  if(!dwNumColors)



    WriteFile(hfl, colors, rct.left, &amp;dwBytes, 0);



  WriteFile(hfl, pBits, (rct.right * rct.bottom * rct.top) >> 3, &amp;dwBytes, 0);



  CloseHandle(hfl);



  DeleteObject(hbmp);



  DeleteDC(memdc);



  return 1;


 



  errato:



  DeleteDC(memdc);



  relHwndDc:



  ReleaseDC(hwnd, hdc); return 0;


}





 


je pense que le probleme vient des lignes suivante :


 




rct.top = GetDeviceCaps(hdc, BITSPIXEL);


.....


bmpinfo.bmiHeader.biBitCount = (WORD) rct.top;





 


Je pense qu'il faudrait changer rct.top, puisque dans le code il prend la config de l'image avec GetDeviceCaps et y mettre la bonne valeur pour avoir un bmp en 24 bits.


 


Merci d'avance pour votre aide!!

1 réponse

SAKingdom Messages postés 3212 Date d'inscription lundi 7 novembre 2005 Statut Membre Dernière intervention 16 février 2009 15
16 mai 2007 à 20:09
Quesque ça donne si tu met biBitCount à 24 ?
bmpinfo.bmiHeader.biBitCount = 24;

C++ (@++)<!--
0
Rejoignez-nous