unit GdiPlus.Save;
interface
uses
System.SysUtils,
Winapi.GDIPOBJ,
Winapi.GDIPUTIL,
Winapi.GDIPAPI,
Vcl.Graphics;
function SaveImage(
const vstrImagePath:
String;
const vpntImage: TPicture): Boolean;
implementation
function SaveImage(
const vstrImagePath:
String;
const vpntImage: TPicture): Boolean;
var
strDateiendung:
string;
pntBitmap: TBitmap;
Bitmap: TGPBitmap;
clsidEncoder: TGUID;
EncoderResult: Integer;
function GetEncoder(
const AFileExt:
string;
out pClsid: TGUID): Integer;
begin
Result := -1;
if AFileExt = '
.BMP'
then
begin
Result := GetEncoderClsid('
image/bmp', pClsid)
end
else if AFileExt = '
.PNG'
then
begin
Result := GetEncoderClsid('
image/png', pClsid)
end
else if (AFileExt = '
.JPG')
or (AFileExt = '
.JPEG')
then
begin
Result := GetEncoderClsid('
image/jpeg', pClsid)
end;
end;
begin
Result := False;
pntBitmap := TBitmap.Create;
try
try
pntBitmap.Assign(vpntImage);
Bitmap := TGPBitmap.Create(pntBitmap.Handle, pntBitmap.Palette);
try
Bitmap.SetResolution(300.0, 300.0);
strDateiendung := UpperCase(ExtractFileExt(vstrImagePath));
EncoderResult := GetEncoder(strDateiendung, clsidEncoder);
if EncoderResult > 0
then
Result := Bitmap.Save(vstrImagePath, clsidEncoder) = TStatus.Ok;
finally
Bitmap.Free;
end;
except
on E:
Exception do
begin
raise Exception.Create('
Fehler beim Speichern der Grafikdatei ' + vstrImagePath + '
: ' + E.ClassName + '
: ' + E.
Message);
end;
end;
finally
pntBitmap.Free;
end;
end;
end.