使用具有设置 DPI 的 GDI+ 从 BMP 保存 JPG

Saving a JPG from a BMP using GDI+ with a set DPI

我有一个 BMP 格式的图像,我想要一个 C++ 程序使用 GDI+ 将它保存为 JPG,在阅读了一些 GDI+ 文档之后我想出了这个程序:

#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"的值是多少,图像都是以7dpi的水平和垂直分辨率保存的,我需要以96dpi保存jpg,我该如何设置?

提前致谢。

函数 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;
}