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
.
- Add
Graphics
,Dialogs
, andCADImage
to theuses
section. Add theTOpenPictureDialog
component and name itFOpenPic
.
uses
... Graphics, Dialogs, CADImage;
- Create a procedure to export CAD files to BMP. Declare the local variables:
- Declare
vPicture
and specifyTPicture
as its type. TheTPicture
object works with CAD files whenCADImage
is in theuses
section. - Declare
vBitmap
and specifyTBitmap
as its type.
- Declare
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
- Create an instance of
TPicture
, and then call theLoadFromFile
method as follows. Create an instance ofTBitmap
. Remember to use thetry...finally
construct to avoid memory leaks.
begin
vPicture := TPicture.Create;
try
if FOpenPic.Execute then
begin
vPicture.LoadFromFile(FOpenPic.FileName);
vBitmap := TBitmap.Create;
- Set the
Width
andHeight
properties of thevBitmap
object. Don't forget to check the value of thevBitmap.Height
property for exceeding the permissible limits. Finally, use theSaveToFile
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');
We recommend using such kind of the Height
and Width
properties checks to avoid exceptions.
- 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;