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 :

Problème d'impression d'un Bitmap reconstitué


Sujet :

Windows

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut Problème d'impression d'un Bitmap reconstitué
    Bonjour,

    A partir d'un bitmap existant et d'un bitmap créé par "CreateCompatibleBitmap()", j'obtiens par "BitBlt()" un Bitmap résultant.
    Je passe ensuite par 2 fonctions trouvées sur MSDN, une qui recharge le Header, PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp), la seconde qui crée le fichier xxxxx.bmp
    void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)

    Lors du passage dans cette derniere il ne trouve rien sur l'appel de "GetDIBits()"
    Y aurait il un moyen d'imprimer le Bitmap directement, sans passer par la création d'un fichier ?
    Merci de votre aide

    Voilà les deux foncrions :
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
    { 
        BITMAP bmp; 
        PBITMAPINFO pbmi; 
        WORD    cClrBits; 
    char tmp[10];
    
        // Retrieve the bitmap color format, width, and height. 
        if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
    		MessageBox (hwnd, "Erreur Ouverure GetObject", "CreateBitmapInfoStruct", MB_OK | MB_ICONEXCLAMATION);
    //        errhandler("GetObject", hwnd); 
    
        // Convert the color format to a count of bits. 
        cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
        if (cClrBits == 1) 
            cClrBits = 1; 
        else if (cClrBits <= 4) 
            cClrBits = 4; 
        else if (cClrBits <= 8) 
            cClrBits = 8; 
        else if (cClrBits <= 16) 
            cClrBits = 16; 
        else if (cClrBits <= 24) 
            cClrBits = 24; 
        else cClrBits = 32; 
    
        // Allocate memory for the BITMAPINFO structure. (This structure 
        // contains a BITMAPINFOHEADER structure and an array of RGBQUAD 
        // data structures.) 
    
         if (cClrBits != 24) 
             pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                        sizeof(BITMAPINFOHEADER) + 
                        sizeof(RGBQUAD) * (1<< cClrBits)); 
    
         // There is no RGBQUAD array for the 24-bit-per-pixel format. 
    
         else 
             pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                        sizeof(BITMAPINFOHEADER)); 
    
        // Initialize the fields in the BITMAPINFO structure. 
    
        pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
        pbmi->bmiHeader.biWidth = bmp.bmWidth; 
        pbmi->bmiHeader.biHeight = bmp.bmHeight; 
        pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
        pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
        if (cClrBits < 24) 
            pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 
    
        // If the bitmap is not compressed, set the BI_RGB flag. 
        pbmi->bmiHeader.biCompression = BI_RGB; 
    
        // Compute the number of bytes in the array of color 
        // indices and store the result in biSizeImage. 
        // For Windows NT, the width must be DWORD aligned unless 
        // the bitmap is RLE compressed. This example shows this. 
        // For Windows 95/98/Me, the width must be WORD aligned unless the 
        // bitmap is RLE compressed.
        pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                      * pbmi->bmiHeader.biHeight; 
        // Set biClrImportant to 0, indicating that all of the 
        // device colors are important. 
         pbmi->bmiHeader.biClrImportant = 0; 
         return pbmi; 
     }
    
    BOOL CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) 
     { 
         HANDLE hf;                 // file handle 
        BITMAPFILEHEADER hdr;       // bitmap file-header 
        PBITMAPINFOHEADER pbih;     // bitmap info-header 
        LPBYTE lpBits;              // memory pointer 
        DWORD dwTotal;              // total count of bytes 
        DWORD cb;                   // incremental count of bytes 
        BYTE *hp;                   // byte pointer 
        DWORD dwTmp; 
    
        pbih = (PBITMAPINFOHEADER) pbi; 
        lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
    
        if (!lpBits) {
    		MessageBox (hwnd, "Erreur GlobalAlloc", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
    	}
    
    // Retrieve the color table (RGBQUAD array) and the bits 
    // (array of palette indices) from the DIB. 
        if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)) 
        {
            MessageBox (hwnd, "Erreur GetDIBits", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
        }
    
    // Create the .BMP file. 
        hf = CreateFile(pszFile, 
                       GENERIC_READ | GENERIC_WRITE, 
                       (DWORD) 0, 
                        NULL, 
                       CREATE_ALWAYS, 
                       FILE_ATTRIBUTE_NORMAL, 
                       (HANDLE) NULL); 
        if (hf == INVALID_HANDLE_VALUE) {
    		MessageBox (hwnd, "Erreur CreateFile", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
    	}
        hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M" 
    // Compute the size of the entire file. 
        hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                     pbih->biSize + pbih->biClrUsed 
                     * sizeof(RGBQUAD) + pbih->biSizeImage); 
        hdr.bfReserved1 = 0; 
        hdr.bfReserved2 = 0; 
    
    // Compute the offset to the array of color indices. 
        hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                        pbih->biSize + pbih->biClrUsed 
                        * sizeof (RGBQUAD); 
    
    // Copy the BITMAPFILEHEADER into the .BMP file. 
        if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
            (LPDWORD) &dwTmp,  NULL)) 
        {
           MessageBox (hwnd, "Erreur WriteFile", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
           return FALSE;
        }
    
    // Copy the BITMAPINFOHEADER and RGBQUAD array into the file. 
        if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                      + pbih->biClrUsed * sizeof (RGBQUAD), 
                      (LPDWORD) &dwTmp, ( NULL))) {
              MessageBox (hwnd, "Erreur WriteFile", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
              return FALSE;
    		  }
    
    // Copy the array of color indices into the .BMP file. 
        dwTotal = cb = pbih->biSizeImage; 
        hp = lpBits; 
        if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) {
    		   MessageBox (hwnd, "Erreur WriteFile", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
               return FALSE;
    	}
    
    // Close the .BMP file. 
    	if (!CloseHandle(hf)) {
    		   MessageBox (hwnd, "Erreur CloseHandle", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
               return FALSE;
    	}
    
    // Free memory. 
        GlobalFree((HGLOBAL)lpBits);
    	return TRUE;
    }

  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 518
    Points
    41 518
    Par défaut
    Qu'appelles-tu "Ne trouve rien" ? Est-ce une erreur ? Dans ce cas, que retourne GetLastError() ? (voir aussi FormatMessage()).
    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.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    Qu'appelles-tu "Ne trouve rien" ? Est-ce une erreur ? Dans ce cas, que retourne GetLastError() ? (voir aussi FormatMessage()).
    Bonjour,

    C'est pas mieux, je n'ai que des caractères illisibles en retour ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    LPVOID lpMsgBuf;
    
        // Retrieve the bitmap color format, width, and height. 
        if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
                             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_STRING,
    						 NULL, GetLastError(),
    						 0, (LPTSTR) &lpMsgBuf, 0, NULL);
    // Display the string.
    MessageBox( NULL, (LPCTSTR)&lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
    // Free the buffer.
    LocalFree( lpMsgBuf );

  4. #4
    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 518
    Points
    41 518
    Par défaut
    Supprime le & dans ton appel à MessageBox.
    Code C : 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
     
    	// Retrieve the bitmap color format, width, and height. 
    	if (!GetObject(hBmp, sizeof(bmp), &bmp))
    	{
    		/* Get the message */
    		LPTSTR lpMsgBuf = NULL;
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
    		 | FORMAT_MESSAGE_IGNORE_INSERTS,
    		 NULL, GetLastError(),
    		 0, (LPTSTR)(&lpMsgBuf), 0, NULL
    		 );
    		// Display the string.
    		MessageBox( NULL, lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION );
    		// Free the buffer.
    		LocalFree( lpMsgBuf );
    	}
    Code C++ : 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
     
    	// Retrieve the bitmap color format, width, and height. 
    	if (!GetObject(hBmp, sizeof(bmp), &bmp))
    	{
    		/* Get the message */
    		LPTSTR lpMsgBuf = 0;
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
    		 | FORMAT_MESSAGE_IGNORE_INSERTS,
    		 NULL, GetLastError(),
    		 0, reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, NULL
    		 );
    		// Display the string.
    		MessageBox( NULL, lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION );
    		// Free the buffer.
    		LocalFree( lpMsgBuf );
    	}
    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.

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    Supprime le & dans ton appel à MessageBox.
    Code C : 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
     
    	// Retrieve the bitmap color format, width, and height. 
    	if (!GetObject(hBmp, sizeof(bmp), &bmp))
    	{
    		/* Get the message */
    		LPTSTR lpMsgBuf = NULL;
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
    		 | FORMAT_MESSAGE_IGNORE_INSERTS,
    		 NULL, GetLastError(),
    		 0, (LPTSTR)(&lpMsgBuf), 0, NULL
    		 );
    		// Display the string.
    		MessageBox( NULL, lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION );
    		// Free the buffer.
    		LocalFree( lpMsgBuf );
    	}
    Code C++ : 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
     
    	// Retrieve the bitmap color format, width, and height. 
    	if (!GetObject(hBmp, sizeof(bmp), &bmp))
    	{
    		/* Get the message */
    		LPTSTR lpMsgBuf = 0;
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
    		 | FORMAT_MESSAGE_IGNORE_INSERTS,
    		 NULL, GetLastError(),
    		 0, reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, NULL
    		 );
    		// Display the string.
    		MessageBox( NULL, lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION );
    		// Free the buffer.
    		LocalFree( lpMsgBuf );
    	}
    ça plante carrement sans le &

  6. #6
    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 518
    Points
    41 518
    Par défaut
    Avec FORMAT_MESSAGE_ALLOCATE_BUFFER et si lpMsgBuf n'est pas NULL, ça ne devrait pas plante.
    Vérifier peut-être la valeur de retour de FormatMessage()...
    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.

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    Avec FORMAT_MESSAGE_ALLOCATE_BUFFER et si lpMsgBuf n'est pas NULL, ça ne devrait pas plante.
    Vérifier peut-être la valeur de retour de FormatMessage()...
    Je dois avoir "0" dans le retour de FormatMessage() puisque je n'affiche pas le MessageBox() ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) {
    		if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
                             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_STRING,
    						 NULL, GetLastError(),
    						 0, (LPTSTR) &lpMsgBuf, 0, NULL)) {
              MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
              LocalFree( lpMsgBuf );
    		}
            MessageBox (hwnd, "Erreur Ouverure GetObject", "CreateBitmapInfoStruct", MB_OK | MB_ICONEXCLAMATION);
    }

  8. #8
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut
    Il y a eu une erreur sur le copier/coller, ce n'est pas sur l'appel de "GetObject()" mais bien sur l'appel de GetDIBits() !
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)) 
        {
           if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
                             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_STRING,
    						 NULL, GetLastError(),
    						 0, (LPTSTR) &lpMsgBuf, 0, NULL)) {
              MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
              LocalFree( lpMsgBuf );
    		}
            MessageBox (hwnd, "Erreur GetDIBits", "CreateBMPFile", MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
        }

  9. #9
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    525
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 525
    Points : 212
    Points
    212
    Par défaut
    Autant pour moi, l'appel de ma fonction n'etait pas correct !
    Il y avait un "&" sur un parametre ...
    La fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
    L'appel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    CreateBMPFile(hwnd, "tm.bmp", &Slpbmi,  hbmpOut2, hdc);
    Merci quand même,
    et toutes mes excuses ....

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Encore Problème d'impression d'un Bitmap
    Par dede92 dans le forum Windows
    Réponses: 0
    Dernier message: 18/04/2008, 19h35
  2. Problème d'impression de Bitmap
    Par geocad dans le forum Langage
    Réponses: 20
    Dernier message: 26/10/2007, 21h54
  3. Réponses: 2
    Dernier message: 16/01/2005, 23h18
  4. [Rave] problème d'impression
    Par romstarr dans le forum Rave
    Réponses: 2
    Dernier message: 09/06/2004, 15h48
  5. Problème d'impression
    Par IngBen dans le forum C++Builder
    Réponses: 7
    Dernier message: 22/05/2002, 11h37

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