2016-09-27 75 views
0

我有一個BMP格式的圖像,我希望C++程序使用GDI +將它保存爲JPG,閱讀了一些GDI +文檔後,我想出了這個程序:使用GDI +保存JPG格式的JPG DPI

#include <windows.h> 
#include <objidl.h> 
#include <gdiplus.h> 
#include "GdiplusHelperFunctions.h" 


#pragma comment (lib,"Gdiplus.lib") 

VOID SaveFile() 
{ 
    // Initialize GDI+. 
    Gdiplus::GdiplusStartupInput gdiplusStartupInput; 
    ULONG_PTR gdiplusToken; 
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 

    CLSID encoderClsid; 
    Status stat; 
    EncoderParameters encoderParameters; 
    ULONG quality; 
    Image* image = new Gdiplus::Image(L"plot.bmp"); 

    // Get the CLSID of the PNG encoder. 
    GetEncoderClsid(L"image/jpeg", &encoderClsid); 

    encoderParameters.Count = 1; 
    encoderParameters.Parameter[0].Guid = EncoderQuality; 
    encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong; 
    encoderParameters.Parameter[0].NumberOfValues = 1; 

    quality = 100; 
    encoderParameters.Parameter[0].Value = &quality; 

    stat = image->Save(L"plot100.jpg", &encoderClsid, &encoderParameters); 

    if (stat == Ok) 
     printf("plot.jpg was saved successfully\n"); 
    else 
     printf("Failure: stat = %d\n", stat); 

    delete image; 
    GdiplusShutdown(gdiplusToken); 
    return; 
} 

int main() 
{ 
    SaveFile(); 
    return 0; 
} 

不管「quelity」的值是多少,我都需要用96dpi保存jpg,我該怎麼設置?

預先感謝您。

回答

0

功能SAVEFILE()的修改版本解決了這個問題:

VOID SaveFile() 
{ 
// Initialize GDI+. 
Gdiplus::GdiplusStartupInput gdiplusStartupInput; 
ULONG_PTR gdiplusToken; 
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 

CLSID encoderClsid; 
Status stat; 
EncoderParameters encoderParameters; 
ULONG quality; 

Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(L"plot.bmp"); 
Gdiplus::REAL dpi = 72; 
bitmap->SetResolution(dpi,dpi); 


// Get the CLSID of the PNG encoder. 
GetEncoderClsid(L"image/jpeg", &encoderClsid); 

encoderParameters.Count = 1; 
encoderParameters.Parameter[0].Guid = EncoderQuality; 
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong; 
encoderParameters.Parameter[0].NumberOfValues = 1; 

quality = 100; 
encoderParameters.Parameter[0].Value = &quality; 

stat = bitmap->Save(L"plot.jpg", &encoderClsid, &encoderParameters); 


if (stat == Ok) 
    printf("plot.jpg was saved successfully\n"); 
else 
    printf("Failure: stat = %d\n", stat); 

delete bitmap; 
GdiplusShutdown(gdiplusToken); 
return; 
}