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, andCADImageto theusessection. Add theTOpenPictureDialogcomponent and name itFOpenPic.
uses
... Graphics, Dialogs, CADImage;
- Create a procedure to export CAD files to BMP. Declare the local variables:
- Declare
vPictureand specifyTPictureas its type. TheTPictureobject works with CAD files whenCADImageis in theusessection. - Declare
vBitmapand specifyTBitmapas its type.
- Declare
procedure TForm1.ExportToBMPClick(ASender: TObject);
var
vPicture: TPicture;
vBitmap: TBitmap;
- Create an instance of
TPicture, and then call theLoadFromFilemethod as follows. Create an instance ofTBitmap. 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. Finally, use theSaveToFilemethod.
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;