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

Discuss and ask questions about CAD VCL (Delphi and C++ Builder).

Moderators: admin, support, SDS

Post Reply
support
Posts: 2930
Joined: 30 Mar 2005, 11:36
Contact:

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

Post by support » 05 May 2016, 20:53

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
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply