Partage
  • Partager sur Facebook
  • Partager sur Twitter

Transformer image

jpg bmp

Sujet résolu
    13 mars 2024 à 14:13:13

    Bonjour. Dans mon projet j'ai réussi a telelcharger une image avec urldownloadtofile  . Maintenant je voudrais transformer cette  JPEG en format BMP  (pour après  extraire seulement une partie de la photo). auriez vous un exemple ?
    • Partager sur Facebook
    • Partager sur Twitter
      13 mars 2024 à 14:52:30

      Bonjour ! Un exemple de quoi, d'image ? de programme ? Le format JPG ne permet pas d'extraire une partie de la photo (ben si) ? Quel rapport avec le langage C ? En fait, tu veux écrire en C (à titre d'exercice) un programme de conversion JPG-->BMP, c'est bien ça ? Est-ce que tu cherches la doc du format JPG ? Pose des questions précises !

      -
      Edité par robun 13 mars 2024 à 14:53:24

      • Partager sur Facebook
      • Partager sur Twitter
        13 mars 2024 à 15:23:18

        Calme-toi robun !!

        Je voudrais effectivement convertir un ficher jpg vers bmp.

        La fonction LoadImage ne prend pas en charge ce format.

        • Partager sur Facebook
        • Partager sur Twitter
          13 mars 2024 à 15:52:28

          Ah, donc tu cherches une fonction (ou une bibliothèque) qui remplacerait LoadImage ?Je ne connais pas.

          -
          Edité par robun 13 mars 2024 à 15:55:31

          • Partager sur Facebook
          • Partager sur Twitter
            13 mars 2024 à 16:52:24

            Ton lien LoadImage est HS

            Je suppose que tu veux parler de ce LoadImage  (GDI - API Win32) ?

            Tu devrais plutôt passer par GDI+ qui lui gère le jpg entre autre.

            • Partager sur Facebook
            • Partager sur Twitter
            ...
              16 mars 2024 à 9:31:36

              Voici un exemple simple (sans contrôle d'erreur) qui charge une image JPG pour en extraire la partie supérieure gauche et sauvegarde le résultat au format BMP :

              // cropimage.c
              // win10 - mingw/gcc
              
              #include <windows.h>
              #include <gdiplus.h> //-lgdiplus
              
              #define IMAGE_SRC L"image_src.jpg"
              #define IMAGE_DST L"image_dst.bmp"
              
              int main(void)
              {
                // Init Gdiplus
                ULONG_PTR gdiplusToken;
                GdiplusStartupInput gdiplusStartupInput = {1, 0, 0, 0};
                GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
              
                // Chargement
                GpBitmap *image_src;
                GdipLoadImageFromFile(IMAGE_SRC, &image_src);
              
                // 'Cropping'
                GpBitmap *image_dst;
                GdipCloneBitmapAreaI(0, 0, 100, 100, PixelFormat24bppRGB, image_src, &image_dst);
              
                // Sauvegarde
                CLSID bmp = {0x557cf400, 0x1a04, 0x11d3, {0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e}};
                GdipSaveImageToFile(image_dst, IMAGE_DST, &bmp, 0);
              
                //Nettoyage
                GdipDisposeImage(image_dst);
                GdipDisposeImage(image_src);
                GdiplusShutdown(gdiplusToken);
              }

              Bonne continuation ...

              -
              Edité par magma 16 mars 2024 à 9:44:13

              • Partager sur Facebook
              • Partager sur Twitter
                22 mars 2024 à 11:01:56

                Merci pour vos retour et l'exemple. je vais pouvoir charger le BMP avec LoadImage et avoir en retour un HBITMAP pour continuer. Je fais ça à titrede loisir et je suis pas pressé.

                • Partager sur Facebook
                • Partager sur Twitter
                  23 mars 2024 à 13:17:20

                  Tu peux obtenir un 'HBITMAP' (GDI handle) directement à partir d'une image JPG sans avoir besoin de convertir l'image au format BMP au préalable.

                  Voici un autre exemple différent du premier qui permet de copier la partie centrale d'une image ...

                  // cropimage2.c
                  // win10 - mingw/gcc
                  // Pas de controle d'erreur ...
                  
                  #include <windows.h>
                  #include <gdiplus.h> //-lgdiplus
                  
                  #define IMAGE_SRC L"image_src.jpg"
                  #define IMAGE_DST L"image_dst.png"
                  
                  #define CX 200 // largeur et hauteur de l'image destination
                  #define CY 100
                  
                  int main(void)
                  {
                    // Init Gdiplus
                    ULONG_PTR gdiplusToken;
                    GdiplusStartupInput gdiplusStartupInput = {1, 0, 0, 0};
                    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
                  
                    // On charge l'image source
                    GpBitmap *image_src;
                    GdipCreateBitmapFromFile(IMAGE_SRC, &image_src);
                  
                    // On recupere le 'HBITMAP' de l'image
                    HBITMAP hbitmap_src;
                    GdipCreateHBITMAPFromBitmap(image_src, &hbitmap_src, 0);
                  
                    // On recupere les caracteristiques du 'HBITMAP'
                    BITMAP bm;
                    GetObject(hbitmap_src, sizeof(BITMAP), &bm);
                  
                    // 'Cropping' On extrait, par exemple, la partie centrale de l'image(Largeur = CX, Hauteur = CY)
                    HDC hdc = GetDC(0);
                    HDC hdc_src = CreateCompatibleDC(hdc);
                    HDC hdc_dst = CreateCompatibleDC(hdc);
                    HBITMAP hbitmap_dst = CreateCompatibleBitmap(hdc, CX, CY);
                    SelectObject(hdc_src, hbitmap_src);
                    SelectObject(hdc_dst, hbitmap_dst);
                    int x = (bm.bmWidth - CX) / 2;
                    int y = (bm.bmHeight - CY) / 2;
                    BitBlt(hdc_dst, 0, 0, CX, CY, hdc_src, x, y, SRCCOPY);
                  
                    // On sauvegarde le resultat au format PNG
                    GpBitmap *bitmap_dst;
                    GdipCreateBitmapFromHBITMAP(hbitmap_dst, 0, &bitmap_dst);
                    CLSID png = {0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e}};
                    GdipSaveImageToFile(bitmap_dst, IMAGE_DST, &png, 0);
                  
                    // On fait le menage
                    DeleteObject(hbitmap_dst);
                    DeleteObject(hbitmap_src);
                    DeleteDC(hdc_dst);
                    DeleteDC(hdc_src);
                    ReleaseDC(0, hdc);
                    GdipDisposeImage(bitmap_dst);
                    GdipDisposeImage(image_src);
                    GdiplusShutdown(gdiplusToken);
                  }



                  • Partager sur Facebook
                  • Partager sur Twitter
                    28 mars 2024 à 14:29:20

                    j'ai réussi a modifier le programme (merci magma) pour me permettre d'ecrire la date et l'heure sur l'image avant d'enregistrer, je poste le code avec la modification que j'ai ajouté:

                    #include <windows.h>
                    #include <gdiplus.h> //-lgdiplus
                    
                    #include <time.h>
                    
                    #define IMAGE_SRC L"image_src.jpg"
                    #define IMAGE_DST L"image_dst.png"
                    
                    #define CX 800 // largeur et hauteur de l'image destination
                    #define CY 600
                    
                    int main(void)
                    {
                      // Init Gdiplus
                      ULONG_PTR gdiplusToken;
                      GdiplusStartupInput gdiplusStartupInput = {1, 0, 0, 0};
                      GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
                    
                      // On charge l'image source
                      GpBitmap *image_src;
                      GdipCreateBitmapFromFile(IMAGE_SRC, &image_src);
                    
                      // On recupere le 'HBITMAP' de l'image
                      HBITMAP hbitmap_src;
                      GdipCreateHBITMAPFromBitmap(image_src, &hbitmap_src, 0);
                    
                      // On recupere les caracteristiques du 'HBITMAP'
                      BITMAP bm;
                      GetObject(hbitmap_src, sizeof(BITMAP), &bm);
                    
                      // 'Cropping'
                      HDC hdc = GetDC(0);
                      HDC hdc_src = CreateCompatibleDC(hdc);
                      HDC hdc_dst = CreateCompatibleDC(hdc);
                      HBITMAP hbitmap_dst = CreateCompatibleBitmap(hdc, CX, CY);
                      SelectObject(hdc_src, hbitmap_src);
                      SelectObject(hdc_dst, hbitmap_dst);
                      int x = (bm.bmWidth - CX) / 2;
                      //int y = (bm.bmHeight - CY) / 2;
                      BitBlt(hdc_dst, 0, 0, CX, CY, hdc_src, x, 0, SRCCOPY);
                    
                    // ecrire date+heure sur image
                    time_t timegmt = time(NULL);
                    TextOut(hdc_dst, 0, 0, ctime(&timegmt), 26);
                    
                      // On sauvegarde le resultat au format PNG
                      GpBitmap *bitmap_dst;
                      GdipCreateBitmapFromHBITMAP(hbitmap_dst, 0, &bitmap_dst);
                      CLSID png = {0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e}};
                      GdipSaveImageToFile(bitmap_dst, IMAGE_DST, &png, 0);
                    
                      // On fait le menage
                      DeleteObject(hbitmap_dst);
                      DeleteObject(hbitmap_src);
                      DeleteDC(hdc_dst);
                      DeleteDC(hdc_src);
                      ReleaseDC(0, hdc);
                      GdipDisposeImage(bitmap_dst);
                      GdipDisposeImage(image_src);
                      GdiplusShutdown(gdiplusToken);
                    }

                    je met le sujet en résolu pour l'instant.

                    -
                    Edité par L4M4 28 mars 2024 à 15:03:14

                    • Partager sur Facebook
                    • Partager sur Twitter
                      28 mars 2024 à 19:10:01

                      @L4M4 Bonsoir, je sors votre dernier message des spam, si cela arrive encore vous pouvez poster dans ce sujet Si votre message est considéré comme spam

                      -
                      Edité par AbcAbc6 28 mars 2024 à 19:10:23

                      • Partager sur Facebook
                      • Partager sur Twitter

                      Transformer image

                      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                      • Editeur
                      • Markdown