How to export CAD files to JPEG with a specified DPI?
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 using the TsgJPEGImage class.
- Add Graphics,Dialogs,CADImage, andsgJpegto theusessection. Add theTOpenPictureDialogcomponent and name itFOpenPic.
uses
... Graphics, Dialogs, CADImage, sgJpeg;  
- Create a procedure to export CAD files to JPEG. Declare the local variables:- Declare vPictureand specifyTPictureas its type. TheTPictureobject works with CAD files whenCADImageis in theusessection.
- Declare vBitmapand specifyTBitmapas its type.
- Declare vJPEGImageand specifyTsgJPEGImageas its type.
 
- Declare 
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
  vPicture: TPicture;
  vBitmap: TBitmap;
  vJPEGImage: TsgJPEGImage;
- Create instances of the TPictureandTBitmapobjects, and then call theLoadFromFilemethod as follows. Remember to use thetry...finallyconstruct to avoid memory leaks.
begin
  vPicture := TPicture.Create;
  try
    if FOpenPic.Execute then
    begin
      vPicture.LoadFromFile(FOpenPic.FileName);
      vBitmap := TBitmap.Create;
- Set the WidthandHeightproperties of thevBitmapobject. Don't forget to check the value of thevBitmap.Heightproperty for exceeding the permissible limits.
try
  vBitmap := TBitmap.Create;
  vBitmap.Height := 1;
  vBitmap.Width := 1000;
  if vPicture.Graphic.Width <> 0 then
    vBitmap.Height := Round(vBitmap.Width * (vPicture.Graphic.Height / vPicture.Graphic.Width));
  if vBitmap.Height > 4096 then
    vBitmap.Height := Round(4096);
  vBitmap.Canvas.StretchDraw(Rect(0, 0, vBitmap.Width, vBitmap.Height), vPicture.Graphic);
We recommend to use such kinds of the Height and Width properties checks to avoid exceptions. 
- Finally, create an instance of the TsgJPEGImageobject. Set theDPUX,DPUY, andCompressionQualityproperties. Use theAssignandSaveToFilemethods.
vJPEGImage := TsgJPEGImage.Create;
  try
    vJPEGImage.Assign(vBitmap);
    vJPEGImage.DPUX := 300;
    vJPEGImage.DPUY := 300;
    vJPEGImage.CompressionQuality := 100;
    vJPEGImage.SaveToFile(FOpenPic.FileName + '.jpg');
    ShowMessage('File is saved to BMP: ' + FOpenPic.FileName + '.jpg');
- Don't forget to free the objects.
        finally
          vJPEGImage.Free;
        end;
      finally
        vBitmap.Free;
      end;
    end;
  finally
    vPicture.Free;
  end;
end;
You have created the procedure to export CAD files to JPEG.
The full code listing.
procedure TForm1.ExportToJPEG(ASender: TObject);
var
  vPicture: TPicture;
  vBitmap: TBitmap;
  vJPEGImage: TsgJPEGImage;
begin
  vPicture := TPicture.Create;
  try
    if FOpenPic.Execute then
    begin
      vPicture.LoadFromFile(FOpenPic.FileName);
      vBitmap := TBitmap.Create;
      try
        vBitmap := TBitmap.Create;
        vBitmap.Height := 1;
        vBitmap.Width := 1000;
        if vPicture.Graphic.Width <> 0 then
          vBitmap.Height :=
            Round(vBitmap.Width * (vPicture.Graphic.Height /
            vPicture.Graphic.Width));
        if vBitmap.Height > 4096 then
          vBitmap.Height := Round(4096);
        vBitmap.Canvas.StretchDraw(Rect(0, 0, vBitmap.Width, vBitmap.Height),
          vPicture.Graphic);
        vJPEGImage := TsgJPEGImage.Create;
        try
          vJPEGImage.Assign(vBitmap);
          vJPEGImage.DPUX := 300;
          vJPEGImage.DPUY := 300;
          vJPEGImage.CompressionQuality := 100;
          vJPEGImage.SaveToFile(FOpenPic.FileName + '.jpg');
          ShowMessage('File is saved to BMP: ' + FOpenPic.FileName + '.jpg');
        finally
          vJPEGImage.Free;
        end;
      finally
        vBitmap.Free;
      end;
    end;
  finally
    vPicture.Free;
  end;
end;