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 :

[Débutant] Copie d'écran vers BMP


Sujet :

Windows

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2003
    Messages : 12
    Points : 13
    Points
    13
    Par défaut [Débutant] Copie d'écran vers BMP
    Bonjour,
    Je souhaite réaliser une copie d'écran depuis du code C++ et la sauvegarder dans un BMP. J'ai récupéré un bout de code sur developpez.com mais je n'arrive pas à le faire fonctionner , sans doute parce qu'il me manque les bons "#include" et je n'arrive pas à les trouver dans la doc MSDN.
    Voici le code récupéré (auteur : Geronimo (je crois)) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <windows.h>
    
    Graphics&#58;&#58;TBitmap *Bitmap = new Graphics&#58;&#58;TBitmap;
    Bitmap->Height = Screen->Height;
    Bitmap->Width = Screen->Width;
    HDC ScreenSrc = GetWindowDC&#40;0&#41;;
    BitBlt&#40;Bitmap->Canvas->Handle, 0, 0, Screen->Width,
           Screen->Height, ScreenSrc, 0, 0, SRCCOPY&#41;;
    Bitmap->SaveToFile&#40;ExtractFilePath&#40;Application->ExeName&#41;+"Bitmap.bmp"&#41;;
    ReleaseDC&#40;GetDesktopWindow&#40;&#41;, ScreenSrc&#41;;
    delete Bitmap;
    La première ligne (déclaration de "Bitmap") ne compile pas. Je n'utilise pas les librairies MFC dans mon code actuel, "TBitmap" fait-il partie de ces librairies ? Je suis familier du C++ mais je ne connais pas MFC.
    Si quelqu'un pouvait me compléter ce code pour qu'il compile et puisse s'exécuter (directement dans un main par exemple), ce serait super cool . Merci.
    J'utilise Visual C++ 6.0.

  2. #2
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    TBitmap, ça ressemble à du Borland.

    Tu as des choses équivalentes avec les MFC, si tu souhaites essayer de les utiliser je déplacerai ton post dans le forum correspondant.

    Sinon, tu peux te taper le tout à la main (avec l'API Windows), mais ce sera sans doute un peu plus long (et là aussi, faudra aller sur le forum correspondant).

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2003
    Messages : 12
    Points : 13
    Points
    13
    Par défaut
    Merci Loulou pour ces précisions. Depuis ta réponse, j'ai essayé d'utiliser les API Windows pour faire ma copie d'écran. En fait, l'objectif est d'imprimer cette copie d'écran sur l'imprimante par défaut. En piochant des exemples à droite à gauche (developpez.com, MSDN) je pensais avoir trouvé mon bonheur ... mais ça ne marche toujours pas.
    Voici le code qui doit imprimer l'écran et mettre un petit titre au-dessus :
    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
      // Recherche de l'imprimante par defaut
      PRINTDLG pd;
      memset&#40; &pd, 0, sizeof&#40; pd &#41; &#41;;
      pd.lStructSize = sizeof&#40; pd &#41;;
      pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
      if&#40;! PrintDlg&#40; &pd &#41;&#41;
      &#123;
        cout << "Erreur recherche imprimante par defaut" << endl;
        return;
      &#125;
      HDC hPrinter = pd.hDC;
    
       // Begin a print job by calling the StartDoc function. 
      DOCINFO di;
        di.cbSize = sizeof&#40;DOCINFO&#41;; 
        di.lpszDocName = "Bitmap Printing Test"; 
        di.lpszOutput = &#40;LPTSTR&#41; NULL; 
        di.lpszDatatype = &#40;LPTSTR&#41; NULL; 
        di.fwType = 0; 
       if &#40;StartDoc&#40;pd.hDC, &di&#41; == SP_ERROR&#41;
      &#123;
    	cout << "Erreur StardDoc" << endl;
    	return;
      &#125;
        // Inform the driver that the application is about to begin 
       // sending data. 
       if &#40;StartPage&#40;pd.hDC&#41; <= 0&#41; 
       &#123; 
          cout << "Erreur StartPage" << endl;
    	return;
       &#125; 
    
       HDC hdcScreen = CreateDC&#40;"DISPLAY", NULL, NULL, NULL&#41;; 
     
        int screenWidth   = GetDeviceCaps&#40;hdcScreen, HORZRES&#41;;
        int screenHeight  = GetDeviceCaps&#40;hdcScreen, VERTRES&#41;;
        int printerWidth  = GetDeviceCaps&#40;pd.hDC,    HORZRES&#41;;
        int printerHeight = GetDeviceCaps&#40;pd.hDC,    VERTRES&#41;;
    
      // Calcul du fctaur de transformation Ecran -> Imprimante
      float fScaleX = 0.0;
      float fScaleY = 0.0; 
        float quota = 0.9f;
    	float fscale = 0.0;
    	fScaleX = printerWidth / screenWidth;
     	fScaleY = &#40;printerHeight * quota&#41; / screenHeight;
        if &#40;fScaleX <= fScaleY&#41; fscale = fScaleX;
    	else fscale = fScaleY;
    	float dx = &#40;printerWidth - screenWidth * fscale&#41; / 2.0f;
    	float dy = printerWidth * &#40;1.0 - quota&#41;;
    	xLeft = &#40;int&#41; dx;
    	yTop = &#40;int&#41; dy;
    
        HDC hdcMem = CreateCompatibleDC&#40;hdcScreen&#41;; 
     
    
        // Create a compatible bitmap for hdcScreen. 
        HBITMAP hbmScreen = CreateCompatibleBitmap&#40;hdcScreen, 
                             GetDeviceCaps&#40;hdcScreen, HORZRES&#41;, 
                             GetDeviceCaps&#40;hdcScreen, VERTRES&#41;&#41;; 
    
        if &#40;!SelectObject&#40;hdcMem, hbmScreen&#41;&#41; 
    	&#123;
    		cout << "Erreur SelectObject" << endl;
    		return;
    	&#125;
      
        // Use the StretchBlt function to scale the bitmap and maintain 
        // its original proportions &#40;that is, if the bitmap was square 
        // when it appeared in the application's client area, it should 
        // also appear square on the page&#41;. 
        if&#40;!StretchBlt&#40;hPrinter, xLeft, yTop, &#40;int&#41; &#40;screenWidth * 
            fscale&#41;, &#40;int&#41; &#40;screenHeight * fscale&#41;, 
            hdcMem, 0, 0, &#40;int&#41; screenWidth, &#40;int&#41; screenHeight, SRCCOPY&#41;&#41; 
        &#123;
    		int err = GetLastError &#40;&#41;;
    		cout << "Erreur StretchBlt = " << err << endl;
    		return;
        &#125;
        DeleteDC&#40;hdcMem&#41;; 
     
        // Impression d'un titre
        // =====================
        // Retrieve the width of the string that specifies the full path 
        // and filename for the file that contains the bitmap. 
    	SIZE szMetric;
    	szMetric.cx = &#40;long&#41; 12;
    	szMetric.cy = &#40;long&#41; 5;
        GetTextExtentPoint32&#40;pd.hDC, "Ma copie d'écran", 
            titre.length &#40;&#41;, &szMetric&#41;; 
     
        // Compute the starting point for the text-output operation. The 
        // string will be centered horizontally and positioned three lines 
        // down from the top of the page. 
        xLeft = &#40;&#40;cWidthPels / 2&#41; - &#40;szMetric.cx / 2&#41;&#41;; 
        yTop = &#40;szMetric.cy * 3&#41;; 
     
        // Print the path and filename for the bitmap, centered at the top 
        // of the page. 
        TextOut&#40;pd.hDC, xLeft, yTop, titre.chars &#40;&#41;, 
            titre.length &#40;&#41;&#41;; 
     
        // Fin de l'impression &#58;
        // Determine whether the user has pressed the Cancel button in the 
        // AbortPrintJob dialog box; if the button has been pressed, call 
        // the AbortDoc function. Otherwise, inform the spooler that the 
        // page is complete. 
        if &#40;EndPage&#40;pd.hDC&#41; <= 0&#41; 
        &#123;
    		cout << "Erreur EndPage" << endl;
    		return;
        &#125;
      
        // Inform the driver that document has ended. 
        if &#40;EndDoc&#40;pd.hDC&#41; <= 0&#41;
        &#123;
    		cout << "Erreur EndDoc" << endl;
    		return;
        &#125;
    A l'exécution, la fonction StretchBlt renvoie le code d'erreur 203 et je ne sais pas trop pourquoi. L'impression du titre fonctionne si je mets le StretchBlt en commentaires.
    Loulou, si tu penses que c'est un problème d'API tu peux déplacer mon post dans le bon forum. Merci à toi et aux autres qui essaierons de m'aider.

Discussions similaires

  1. AS400ToExcel, copie d'écran AS400 vers Excel
    Par mutsum1 dans le forum AS/400
    Réponses: 9
    Dernier message: 31/01/2014, 10h03
  2. Copie écran vers une image
    Par celeborn56 dans le forum Flash/Flex
    Réponses: 3
    Dernier message: 29/05/2011, 12h33
  3. Copie de Textbox vers Excel
    Par Flateric dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 02/05/2005, 09h50
  4. Copie de backup vers ftp (journalier)
    Par osmoze dans le forum Administration système
    Réponses: 3
    Dernier message: 25/10/2004, 14h27
  5. [Débutant][copie object]modifications
    Par pacha1 dans le forum Langage
    Réponses: 11
    Dernier message: 15/07/2004, 12h17

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