none
jpeg library

    Question

  • s it possible to get the jpeg compress method to output to a buffer instead of an output stream file.
    dot net devloper
    Monday, April 20, 2009 9:49 PM

Answers

  • Actually, it is accessible through GdiPlus

    View this MSDN page for specific info:
    http://msdn.microsoft.com/en-us/library/ms535407(VS.85).aspx

    I'll lift the relevant code from the page (because it is broken across two separate pages) and paste it here:

    VOID Example_SaveFile(HDC hdc)
    {
       Graphics graphics(hdc);
    
       // Create an Image object based on a PNG file.
       Image  image(L"Mosaic.png");
    
       // Draw the image.
       graphics.DrawImage(&image, 10, 10);
    
       // Construct a Graphics object based on the image.
       Graphics imageGraphics(&image);
    
       // Alter the image.
       SolidBrush brush(Color(255, 0, 0, 255));
       imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
    
       // Draw the altered image.
       graphics.DrawImage(&image, 200, 10);
    
       // Save the altered image.
       CLSID pngClsid;
       GetEncoderClsid(L"image/png", &pngClsid);
       image.Save(L"Mosaic2.png", &pngClsid, NULL);
    }
    And the helper function is:
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    {
       UINT  num = 0;          // number of image encoders
       UINT  size = 0;         // size of the image encoder array in bytes
    
       ImageCodecInfo* pImageCodecInfo = NULL;
    
       GetImageEncodersSize(&num, &size);
       if(size == 0)
          return -1;  // Failure
    
       pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
       if(pImageCodecInfo == NULL)
          return -1;  // Failure
    
       GetImageEncoders(num, size, pImageCodecInfo);
    
       for(UINT j = 0; j < num; ++j)
       {
          if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
          {
             *pClsid = pImageCodecInfo[j].Clsid;
             free(pImageCodecInfo);
             return j;  // Success
          }    
       }
    
       free(pImageCodecInfo);
       return -1;  // Failure
    }
    And the boilerplate code for gdiplus is:
    #include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    
    #include "GdiplusHelperFunctions.h"
    
    INT main()
    {
       // Initialize GDI+.
       GdiplusStartupInput gdiplusStartupInput;
       ULONG_PTR gdiplusToken;
       GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
       CLSID  encoderClsid;
       INT    result;
       WCHAR  strGuid[39];
    
       result = GetEncoderClsid(L"image/png", &encoderClsid);
    
       if(result < 0)
       {
          printf("The PNG encoder is not installed.\n");
       }
       else
       {
          StringFromGUID2(encoderClsid, strGuid, 39);
          printf("An ImageCodecInfo object representing the PNG encoder\n");
          printf("was found at position %d in the array.\n", result);
          wprintf(L"The CLSID of the PNG encoder is %s.\n", strGuid);
       }
    
       GdiplusShutdown(gdiplusToken);
       return 0;
    }
    • Marked as answer by Nancy Shao Monday, April 27, 2009 2:45 AM
    Tuesday, April 21, 2009 7:55 PM

All replies

  • Yes.  Just use the Stream overload of Image.Save (see below), and save the file to a MemoryStream instead of a file.

    For details of the appropriate overload, see:

    http://msdn.microsoft.com/en-us/library/ms142148.aspx
    Monday, April 20, 2009 10:18 PM
  • Hi,
    the link above is .net specific ...i want to do it in simple C code.
    Compress .bmp image and send it to byte buffer.
    Note:- by using "JPEGLIB"

    Thanks

    dot net devloper
    Tuesday, April 21, 2009 6:52 PM
  • Actually, it is accessible through GdiPlus

    View this MSDN page for specific info:
    http://msdn.microsoft.com/en-us/library/ms535407(VS.85).aspx

    I'll lift the relevant code from the page (because it is broken across two separate pages) and paste it here:

    VOID Example_SaveFile(HDC hdc)
    {
       Graphics graphics(hdc);
    
       // Create an Image object based on a PNG file.
       Image  image(L"Mosaic.png");
    
       // Draw the image.
       graphics.DrawImage(&image, 10, 10);
    
       // Construct a Graphics object based on the image.
       Graphics imageGraphics(&image);
    
       // Alter the image.
       SolidBrush brush(Color(255, 0, 0, 255));
       imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
    
       // Draw the altered image.
       graphics.DrawImage(&image, 200, 10);
    
       // Save the altered image.
       CLSID pngClsid;
       GetEncoderClsid(L"image/png", &pngClsid);
       image.Save(L"Mosaic2.png", &pngClsid, NULL);
    }
    And the helper function is:
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    {
       UINT  num = 0;          // number of image encoders
       UINT  size = 0;         // size of the image encoder array in bytes
    
       ImageCodecInfo* pImageCodecInfo = NULL;
    
       GetImageEncodersSize(&num, &size);
       if(size == 0)
          return -1;  // Failure
    
       pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
       if(pImageCodecInfo == NULL)
          return -1;  // Failure
    
       GetImageEncoders(num, size, pImageCodecInfo);
    
       for(UINT j = 0; j < num; ++j)
       {
          if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
          {
             *pClsid = pImageCodecInfo[j].Clsid;
             free(pImageCodecInfo);
             return j;  // Success
          }    
       }
    
       free(pImageCodecInfo);
       return -1;  // Failure
    }
    And the boilerplate code for gdiplus is:
    #include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    
    #include "GdiplusHelperFunctions.h"
    
    INT main()
    {
       // Initialize GDI+.
       GdiplusStartupInput gdiplusStartupInput;
       ULONG_PTR gdiplusToken;
       GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
       CLSID  encoderClsid;
       INT    result;
       WCHAR  strGuid[39];
    
       result = GetEncoderClsid(L"image/png", &encoderClsid);
    
       if(result < 0)
       {
          printf("The PNG encoder is not installed.\n");
       }
       else
       {
          StringFromGUID2(encoderClsid, strGuid, 39);
          printf("An ImageCodecInfo object representing the PNG encoder\n");
          printf("was found at position %d in the array.\n", result);
          wprintf(L"The CLSID of the PNG encoder is %s.\n", strGuid);
       }
    
       GdiplusShutdown(gdiplusToken);
       return 0;
    }
    • Marked as answer by Nancy Shao Monday, April 27, 2009 2:45 AM
    Tuesday, April 21, 2009 7:55 PM