IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Discussion :

format image bmp 24 bits


Sujet :

Windows

  1. #1
    Membre du Club
    Inscrit en
    Juillet 2006
    Messages
    79
    Détails du profil
    Informations forums :
    Inscription : Juillet 2006
    Messages : 79
    Points : 46
    Points
    46
    Par défaut format image bmp 24 bits
    Bonjour à tous,

    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 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    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, &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, &bmpinfo, DIB_PAL_COLORS, &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, &fileheader, sizeof(BITMAPFILEHEADER), &dwBytes, 0);
      WriteFile(hfl, &infoheader, sizeof(BITMAPINFOHEADER), &dwBytes, 0);
      if(!dwNumColors)
        WriteFile(hfl, colors, rct.left, &dwBytes, 0);
      WriteFile(hfl, pBits, (rct.right * rct.bottom * rct.top) >> 3, &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 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    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!!

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Déjà, je ne vois pas ce que cette valeur (résolution en bits/pixel) vient faire dans un RECT.

    Ensuite, puisque tu utilises BitBlt(), je pense qu'il te suffit de créer directement une DIB 24 bits sans te soucier du device lui-même : Tu as juste besoin des dimensions, et c'est le BitBlt() qui fera la conversion.

    Quant à la taille qui utilise le produit, je te conseille de la calculer à la place en récupérant les informations du DIB avec GetObject(BITMAP), pour utiliser les membres bmWidthBytes et bmHeight.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

Discussions similaires

  1. Réponses: 3
    Dernier message: 04/10/2012, 15h12
  2. convertir une image bmp de 24 bits en 8 bits
    Par nissame143 dans le forum Images
    Réponses: 3
    Dernier message: 18/04/2009, 08h38
  3. Chargement d'une image BMP sur 24 bits
    Par kj_83 dans le forum C++Builder
    Réponses: 3
    Dernier message: 26/12/2006, 19h32
  4. Convertir une image BMP 8 bits en BMP 24 bits
    Par SauCisS dans le forum Bibliothèques
    Réponses: 17
    Dernier message: 23/03/2005, 09h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo