Welcome to Ask N Share, where you can ask questions and receive answers from other members of the community.

Converting JPG to BMP in C

0 votes
Here is the source code given using GDI+ to convert JPEG to BMP. Run the program enter the name of the jpg image like "test.jpg" and output BMP file name like "out.bmp" and it will convert the JPG to BMP.

Have fun....

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <gdiplus.h>
#pragma comment(lib,"GdiPlus.lib")

using namespace Gdiplus;

int GetEncoderClassid(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;
                        return j;  // Success
        return -1;

int main(void)
    char input[100]={0};
    char output[100]={0};
    printf("Enter the JPEG file name : ");
    printf("\nEnter the Output BMP file name : ");
    GdiplusStartupInput startupInput;
    ULONG_PTR token;
    GdiplusStartup(&token, &startupInput, NULL);
    const WCHAR* in;
    const WCHAR* out;
    int nChars;
    // Converting char[] to WCHAR*
    nChars = MultiByteToWideChar(CP_ACP, 0, input, -1, NULL, 0);
    in = new WCHAR[nChars];
    MultiByteToWideChar(CP_ACP, 0, input, -1, (LPWSTR)in, nChars);

    nChars = MultiByteToWideChar(CP_ACP, 0, output, -1, NULL, 0);
    out = new WCHAR[nChars];
    MultiByteToWideChar(CP_ACP, 0, output, -1,(LPWSTR)out, nChars);

   Image* testImg = new Image(in, false);
    CLSID clsid;
    int ret = -1;
    if(-1 != GetEncoderClassid(L"image/bmp", &clsid))
      ret = testImg->Save(out, &clsid);

    delete [] in;
    delete [] out;
    delete testImg;

    printf("\n\nExit with %d code",ret);

    return ret;

And as always, here is the project in Visual Studio 2008.

asked Oct 13, 2012 in Programming Source Codes by Tejashwi Expert (49 points)

Please log in or register to answer this question.