Skip to main content

How to export CAD files to BMP?

To export CAD files to BMP, create a new TBitmap instance and render CAD file graphics on TBitmap.Canvas.

  1. Add Graphics, Dialogs, and CADImage to the uses section. Add the TOpenPictureDialog component and name it FOpenPic.
uses
... Graphics, Dialogs, CADImage;
  1. Create a procedure to export CAD files to BMP. 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.
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
  1. Create an instance of TPicture, and then call the LoadFromFile method as follows. Create an instance of TBitmap. 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. Finally, use the SaveToFile method.
try
vBitmap.Width := 1000;
if vPicture.Graphic.Width <> 0 then
vBitmap.Height :=
Round(vBitmap.Width * (vPicture.Graphic.Height / vPicture.Graphic.Width));
vBitmap.Canvas.StretchDraw(Rect(0, 0, vBitmap.Width, vBitmap.Height), vPicture.Graphic);
vBitmap.SaveToFile(FOpenPic.FileName + '.bmp');
ShowMessage('File is saved to BMP: ' + FOpenPic.FileName + '.bmp');
Note

We recommend using such kind of the Height and Width properties checks to avoid exceptions.

  1. Don't forget to free the objects.
      finally
vBitmap.Free;
end;
end;
finally
vPicture.Free;
end;
end;

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

The full code listing.

uses
... Graphics, Dialogs, CADImage;

...

procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
begin
vPicture := TPicture.Create;
try
if FOpenPic.Execute then
begin
vPicture.LoadFromFile(FOpenPic.FileName);
vBitmap := TBitmap.Create;
try
vBitmap.Width := 1000;
if vPicture.Graphic.Width <> 0 then
vBitmap.Height :=
Round(vBitmap.Width * (vPicture.Graphic.Height /
vPicture.Graphic.Width));
vBitmap.Canvas.StretchDraw(Rect(0, 0, vBitmap.Width, vBitmap.Height),
vPicture.Graphic);
vBitmap.SaveToFile(FOpenPic.FileName + '.bmp');
ShowMessage('File is saved to BMP: ' + FOpenPic.FileName + '.bmp');
finally
vBitmap.Free;
end;
end;
finally
vPicture.Free;
end;
end;