Page 1 of 1

How to save CAD drawing to a JPEG image with a specified DPI

Posted: 05 May 2016, 20:53
by support
Hello everyone,

Sometimes you need to save a drawing loaded to TsgCADImage as a JPEG file with a specified DPI and compression quality. Such JPEG export can be implemented with using a TsgJPEGImage class. To be able to use this class in your program, add sgJpeg unit to the uses clause in the interface section of a unit which is a part of your Delphi project:

Code: Select all

interface

uses

...
sgJpeg;
Once you have sgJpeg under the uses clause, you can add the following code in the implementation section of the unit:

Code: Select all

implementation

{$R *.dfm}

function DrawToBitmap(AGraphic: TGraphic): TBitmap;
begin
  Result := TBitmap.Create;
  Result.Width := AGraphic.Width;
  Result.Height := AGraphic.Height;
  Result.Canvas.StretchDraw(Rect(0, 0, AGraphic.Width, AGraphic.Height), AGraphic);
end;

procedure ExportToJPEG(ACADImage: TsgCADImage; AFileName: string; ADPI, AQuality: Word);
var
  vBitmap: TBitmap;
  vJPEGImage: TsgJPEGImage;
begin
  if (AFileName = '') or not Assigned(ACADImage) then
    Exit;
  try
    vBitmap := DrawToBitmap(ACADImage);
    vJPEGImage := TsgJPEGImage.Create;
    vJPEGImage.Assign(vBitmap);
    with vJPEGImage do
    begin
      DPUX := ADPI;
      DPUY := ADPI;
      CompressionQuality := AQuality;
      SaveToFile(AFileName);
    end;
  finally
    vBitmap.Free;
    vJPEGImage.Free;
  end;
end;
Mikhail