Skip to main content

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.

  1. Add Graphics, Dialogs, CADImage, and sgJpeg to the uses section. Add the TOpenPictureDialog component and name it FOpenPic.
uses

... Graphics, Dialogs, CADImage, sgJpeg;
  1. Create a procedure to export CAD files to JPEG. Declare the local variables:
    • Declare vPicture and specify TPicture as its type. The TPicture object works with CAD files when CADImage is in the uses section.
    • Declare vBitmap and specify TBitmap as its type.
    • Declare vJPEGImage and specify TsgJPEGImage as its type.
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
vJPEGImage: TsgJPEGImage;
  1. Create instances of the TPicture and TBitmap objects, and then call the LoadFromFile method as follows. Remember to use the try...finally construct to avoid memory leaks.
begin
vPicture := TPicture.Create;
try
if FOpenPic.Execute then
begin
vPicture.LoadFromFile(FOpenPic.FileName);
vBitmap := TBitmap.Create;
  1. Set the Width and Height properties of the vBitmap object. Don't forget to check the value of the vBitmap.Height property 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);
Note

We recommend to use such kinds of the Height and Width properties checks to avoid exceptions.

  1. Finally, create an instance of the TsgJPEGImage object. Set the DPUX, DPUY, and CompressionQuality properties. Use the Assign and SaveToFile methods.
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');
  1. 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;