How to export CAD files to BMP?
- Add a new button. Name it
ExportBMP
. Then create theExportBMP_Click
function to export a CAD file to BMP by click.
private void ExportBMP_Click(object sender, EventArgs e){
- Use the
ShowDialog
method to load a CAD file from the dialog window. Create a new instance of theCADImage
class.
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
vHeight
andvWidth
ofDouble
. Don't forget to check thevHeight
value for exceeding the permissible limits. Finally, create an instance of theBitmap
class and assign the valuesvWidth
andvHeight
to theWidth
andHeight
properties.
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
Graphics
class.
Graphics vGr = Graphics.FromImage(vBitmap);
- Render the CAD file to
vGr
with theDraw
method.
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
Save
method.
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);
}
}
}