扣丁书屋

HBITMAP保存到JPEG文件

1年以前  |  阅读数:42 次  |    


int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) 
{  
    UINT num = 0;
    UINT size = 0;
    ImageCodecInfo* pImageCodecInfo = NULL;  

    GetImageEncodersSize(&num, &size);  
    if(size == 0)  return -1;

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));  
    if(pImageCodecInfo == NULL)  return -1;

    //获取所有的图像编码器信息  
    GetImageEncoders(num, size, pImageCodecInfo);  

    //查找符合的图像编码器的Clsid
    for(UINT j = 0; j < num; ++j)
    {
        OutputDebugString(pImageCodecInfo[j].MimeType);
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
        {
            if (pClsid)
            {
                *pClsid = pImageCodecInfo[j].Clsid;  
                free(pImageCodecInfo);  
                return j;  // Success  
            }
        }      
    }  

    free(pImageCodecInfo);  
    return -1;
} 

BOOL CUIHelper::SaveBitmapToJpegFile(HBITMAP hBitmap, LPCWSTR lpFileName)
{
    if (hBitmap)
    {
        CLSID sImageId;
        if (GetEncoderClsid(L"image/jpeg", &sImageId) >= 0)
        {
            Bitmap bitmap(hBitmap, NULL);
            bitmap.Save(lpFileName, &sImageId, NULL);
        }
    }

    return TRUE;
}

相关文章: