How to export CAD files to BMP?
- Add a new button. Name it
ExportBMP. Then create theExportBMP_Clickfunction to export a CAD file to BMP by click.
private void ExportBMP_Click(object sender, EventArgs e){
- Use the
ShowDialogmethod to load a CAD file from the dialog window. Create a new instance of theCADImageclass.
if ((openFileDialog1.ShowDialog() != DialogResult.OK)) return;
CADImage vDrawing = CADImage.CreateImageByExtension(openFileDialog1.FileName);
vDrawing.LoadFromFile(openFileDialog1.FileName);
We recommend creating a new drawing object using CreateImageByExtension in case of importing from an existing file or stream.
- Declare the local variables
vHeightandvWidthofDouble. Don't forget to check thevHeightvalue for exceeding the permissible limits. Finally, create an instance of theBitmapclass and assign the valuesvWidthandvHeightto theWidthandHeightproperties.
Double vHeight = 1;
Double vWidth = 1000;
if (vDrawing.AbsWidth != 0)
{
vHeight = vWidth * (vDrawing.AbsHeight / vDrawing.AbsWidth);
if (vHeight > 4096)
{
vHeight = 4096;
}
vHeight = Math.Round(vHeight);
}
Bitmap vBitmap = new Bitmap((int)(vWidth), (int)(vHeight));
We recommend using such kind of the Height and Width properties checks to avoid exceptions.
- Create an instance of the
Graphicsclass.
Graphics vGr = Graphics.FromImage(vBitmap);
- Render the CAD file to
vGrwith theDrawmethod.
RectangleF vRect = new RectangleF(0, 0, (float)1000, (float)(vBitmap.Width * vDrawing.AbsHeight / vDrawing.AbsWidth));
vDrawing.Draw(vGr, vRect);
- Finally, export the CAD file to BMP with the
Savemethod.
vBitmap.Save(openFileDialog1.FileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
You have created the function to export CAD files to BMP.
The full code listing.
...
using CADImport;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ExportBMP_Click(object sender, EventArgs e)
{
if ((openFileDialog1.ShowDialog() != DialogResult.OK)) return;
CADImage vDrawing = CADImage.CreateImageByExtension(openFileDialog1.FileName);
vDrawing.LoadFromFile(openFileDialog1.FileName);
Double vHeight = 1;
Double vWidth = 1000;
if (vDrawing.AbsWidth != 0)
{
vHeight = vWidth * (vDrawing.AbsHeight / vDrawing.AbsWidth);
if (vHeight > 4096)
{
vHeight = 4096;
}
vHeight = Math.Round(vHeight);
}
Bitmap vBitmap = new Bitmap((int)(vWidth), (int)(vHeight));
Graphics vGr = Graphics.FromImage(vBitmap);
RectangleF vRect = new RectangleF(0, 0, (float)(vWidth), (float)(vHeight));
vDrawing.Draw(vGr, vRect);
vBitmap.Save(openFileDialog1.FileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}