Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: Verwirrung um TImage und den DPI Wert

  Alt 19. Okt 2017, 09:50
Probiere es wie folgt, anstatt der 300.0 musst du natürlich deine gewünschten Werte übergeben:

Delphi-Quellcode:
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 = '.BMPthen
    begin
      Result := GetEncoderClsid('image/bmp', pClsid)
    end
    else if AFileExt = '.PNGthen
    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.
  Mit Zitat antworten Zitat