16 3月 2012

[Win32] Save Bitmap

Save Gdiplus::Bitmap
HRESULT GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
    /*
    format support : 
        "image/png"
        "image/bmp"
        "image/gif"
        "image/jpeg"
        "image/tiff"
    */
    UINT  num  = 0;  // number of image encoders
    UINT  size = 0;  // size of the image encoder array in bytes

    GetImageEncodersSize(&num, &size);
    if(size == 0)
        return E_FAIL;  // Failure

    ImageCodecInfo* pImageCodecInfo = NULL;
    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
        return E_FAIL;  // 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 S_OK;  // Success
        }
    }
    free(pImageCodecInfo);
    return E_FAIL;
}

void SaveBitmap(Bitmap* bitmap)
{
    // Get the CLSID of the JPEG encoder.
    CLSID clsid;
    GetEncoderClsid(L"image/jpeg", &clsid);
    
    bitmap.Save(L"C:\\test.jpg", &clsid, NULL);
}

沒有留言: