Skip to main content

How to export CAD files to PNG?

To export CAD files to PNG, follow the steps bellow:

  1. Add Graphics, Dialogs, CADImage, and PNGImage to the uses section. Add the TOpenPictureDialog component and name it FOpenPic.
uses
... Graphics, Dialogs, CADImage, PNGImage;
  1. Create a procedure to export CAD files to PNG. 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 vPNGImage and specify TPNGImage as its type.
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
vPNGImage: TPNGImage;
  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.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 kind of the Height and Width properties checks to avoid exceptions.

  1. Finally, create an instance of the TPNGImage object. Use the Assign and SaveToFile methods.
vPNGImage := TPNGImage.Create;
try
vPNGImage.Assign(vBitmap);
vPNGImage.SaveToFile(FOpenPic.FileName + '.png');
ShowMessage('File is saved to PNG: ' + FOpenPic.FileName + '.png');
  1. Don't forget to free the objects.
        finally
vPNGImage.Free;
end;
finally
vBitmap.Free;
end;
end;
finally
vPicture.Free;
end;
end;

You have created the procedure to export CAD files to PNG.

The full code listing.

uses 
... Graphics, Dialogs, CADImage, PNGImage;


procedure TForm1.ExportToPng(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
vPNGImage: TPNGImage;
begin
vPicture := TPicture.Create;
try
if FOpenPic.Execute then
begin
vPicture.LoadFromFile(FOpenPic.FileName);
vBitmap := TBitmap.Create;
try
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);
vPNGImage := TPNGImage.Create;
try
vPNGImage.Assign(vBitmap);
vPNGImage.SaveToFile(FOpenPic.FileName + '.png');
ShowMessage('File is saved to PNG: ' + FOpenPic.FileName + '.png');
finally
vPNGImage.Free;
end;
finally
vBitmap.Free;
end;
end;
finally
vPicture.Free;
end;
end;