How to specify a DPI when saving to a BMP file
Posted: 11 Aug 2016, 20:53
Hello everyone,
As you may know, CADImage.SaveToFile method allows to export a drawing to the raster formats, but doesn't allow to specify the resolution in dots per inch (DPI) for the output bitmap file. The code snippet below explains how to set a DPI for the output BMP file by using the CADImage.SaveToStream and Bitmap.SetResolution methods:
Mikhail
As you may know, CADImage.SaveToFile method allows to export a drawing to the raster formats, but doesn't allow to specify the resolution in dots per inch (DPI) for the output bitmap file. The code snippet below explains how to set a DPI for the output BMP file by using the CADImage.SaveToStream and Bitmap.SetResolution methods:
Code: Select all
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using CADImport;
...
private void SaveToBMP(CADImage cadImage, string filename, double scale, float dpi)
{
if ((cadImage != null) & (Path.GetExtension(filename) == ".bmp"))
{
DRect imageSize = new DRect(0, 0, cadImage.AbsWidth * scale, cadImage.AbsHeight * scale);
MemoryStream stream = new MemoryStream();
cadImage.SaveToStream(stream, ImageFormat.Bmp, imageSize, PixelFormat.Format24bppRgb);
Bitmap bmp = new Bitmap(stream);
bmp.SetResolution(dpi, dpi);
bmp.Save(filename);
}
}