NO

Author Topic: A drop in wraper to provide easy access to GDI+ image encoders for saving images  (Read 3457 times)

Offline DMac

  • Member
  • *
  • Posts: 272
[UPDATE:] Changed Encoder parameter GUIDs to local constants.

In order to easily access the image encoding features of GDI+; I wrote this drop-in module which contains a limited subset of GDI+ to support saving files in various graphic formats.  The module loads and initializes GDI+ on the fly, consequently there is no need to work with the GDI+ headers or libraries if all you want to do is save a bitmap to JPEG, PNG, GIF, or TIFF.  The module takes care of all of the housekeeping with a simple constructor and destructor.

The following is an example of usage:

Code: [Select]
int main(int argc, char *argv[])
{
    HINSTANCE hInstance = GetModuleHandle(NULL);

    if(NULL == hInstance)
        return 0;

    //Create ImageFile object;
    LPIMAGEFILE lpi = New_ImageFile();
    if(NULL == lpi) return 0;

    HBITMAP hbmp = LoadImage(hInstance, _T("Ball.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    if(NULL != hbmp){
        ImageFile_SaveJPEG(lpi, hbmp, 100, _T("Ball.jpg"));
        ImageFile_SaveTIFF(lpi, hbmp, TRUE, _T("Ball.tif"));
        ImageFile_SaveBMP(lpi, hbmp, _T("Ball-1.bmp"));
        ImageFile_SavePNG(lpi, hbmp, _T("Ball.png"));
        ImageFile_SaveGIF(lpi, hbmp, _T("Ball.gif"));
    }

    //Clean up and release ImageFile object
    ImageFile_Destroy(lpi);

    return 0;
}
Pure elegance and simplicity if I must say so myself. ;D

The module is fully documented and it should be easy to extend it to include reading images or to utilize more of the options associated with the JPEG and TIFF formats if you wanted to do so.
« Last Edit: January 18, 2015, 06:47:26 PM by DMac »
No one cares how much you know,
until they know how much you care.

czerny

  • Guest
I get the errors

POLINK: error: Unresolved external symbol '_EncoderQuality'.
POLINK: error: Unresolved external symbol '_EncoderCompression'.

You can try to avoid usage of DEFINE_GUID.

Instead of
Code: [Select]
DEFINE_GUID(EncoderCompression, 0xe09d739d,0xccd4,0x44ee,0x8e,0xba,0x3f,0xbf,0x8b,0xe4,0xfc,0x58);use
Code: [Select]
extern const GUID EncoderCompression = {0xe09d739d,0xccd4,0x44ee,{0x8e,0xba,0x3f,0xbf,0x8b,0xe4,0xfc,0x58}};and you don't need to include initguide.h

Another way is to include initguide.h prior to windows.h.
« Last Edit: January 17, 2015, 10:12:36 AM by czerny »