How to specify a DPI when saving to a BMP file
Moderators: SDS, support, admin
How to specify a DPI when saving to a BMP file
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);
}
}
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support
Chat support on Skype: cadsofttools.support
Re: How to specify a DPI when saving to a BMP file
Hi Mikhail,
I'm afraid your code snippet doesn't really answer my question.
The Bitmap.SetResolution method only sets the DPI parameter in the bitmap header. It doesn't change the physical size of the image, as you can find out in the Remarks section on msdn.com.
What determines the physical size of the bitmap is your "scale" parameter. I'm interested in calculating the scale based on the required dpi.
Something like the following:
Does the .NET library have something like the MMWidth/Height property to get the real size of the HPGLImage in millimeters?
The VCL version of CADSoftTools has such property: http://cadsofttools.com/help/cadvcl/cad ... mwidth.htm
I'm afraid your code snippet doesn't really answer my question.
The Bitmap.SetResolution method only sets the DPI parameter in the bitmap header. It doesn't change the physical size of the image, as you can find out in the Remarks section on msdn.com.
What determines the physical size of the bitmap is your "scale" parameter. I'm interested in calculating the scale based on the required dpi.
Something like the following:
Code: Select all
private void SaveToBMP(HPGLImage image, string filename, float dpi)
{
DRect imageSize = new DRect(0, 0, image.MMWidth / 25.4 * dpi, image.MMHeight / 25.4 * dpi);
...
}
The VCL version of CADSoftTools has such property: http://cadsofttools.com/help/cadvcl/cad ... mwidth.htm
Re: How to specify a DPI when saving to a BMP file
Hello,
Your initial question wasn't "How to calculate the physical size of the bitmap based on the required DPI?" You was interested how to specify a DPI when saving a bitmap. My code snippet answers the given question. Please be more precise further to avoid misunderstanding.
Unfortunately, in CAD .NET library the HPGLImage class doesn't have MMWidth and MMHeight properties.
Mikhail
Your initial question wasn't "How to calculate the physical size of the bitmap based on the required DPI?" You was interested how to specify a DPI when saving a bitmap. My code snippet answers the given question. Please be more precise further to avoid misunderstanding.
Unfortunately, in CAD .NET library the HPGLImage class doesn't have MMWidth and MMHeight properties.
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support
Chat support on Skype: cadsofttools.support
Re: How to specify a DPI when saving to a BMP file
I apologize for not being more clear when asking my initial question.
I'm interested in saving the bitmap with height and width (in pixels) according to a given DPI value.
Can you suggest an alternate CADSoftTools library, which has the required properties and can be used in a .NET or VC++ project?
I'm interested in saving the bitmap with height and width (in pixels) according to a given DPI value.
Can you suggest an alternate CADSoftTools library, which has the required properties and can be used in a .NET or VC++ project?
Re: How to specify a DPI when saving to a BMP file
Hello,
I found that CADImage.AbsWidth and CADImage.AbsHeight properties return the physical size in millimeters for HPGL drawings, therefore, you can use the following code to calculate the bitmap size in pixels based on the required DPI:
Mikhail
I found that CADImage.AbsWidth and CADImage.AbsHeight properties return the physical size in millimeters for HPGL drawings, therefore, you can use the following code to calculate the bitmap size in pixels based on the required DPI:
Code: Select all
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using CADImport;
using CADImport.HPGL2;
...
private void SaveToBMP(HPGLImage image, string filename, float dpi)
{
if ((image != null) & (Path.GetExtension(filename) == ".bmp"))
{
DRect imageSize = new DRect(0, 0, image.AbsWidth / 25.4 * dpi, image.AbsHeight / 25.4 * dpi);
MemoryStream stream = new MemoryStream();
cadImage.SaveToStream(stream, ImageFormat.Bmp, imageSize, PixelFormat.Format24bppRgb);
Bitmap bmp = new Bitmap(stream);
bmp.SetResolution(dpi, dpi);
bmp.Save(filename);
bmp.Dispose();
stream.Dispose();
}
}
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support
Chat support on Skype: cadsofttools.support
Re: How to specify a DPI when saving to a BMP file
Thank you. That's exactly what I needed.