How to specify a DPI when saving to a BMP file

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

How to specify a DPI when saving to a BMP file

Post by support » 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:

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);
    }
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

sugi
Posts: 4
Joined: 10 Aug 2016, 20:33

Re: How to specify a DPI when saving to a BMP file

Post by sugi » 12 Aug 2016, 11:21

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:

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);
	...
}
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

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to specify a DPI when saving to a BMP file

Post by support » 12 Aug 2016, 16:54

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
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

sugi
Posts: 4
Joined: 10 Aug 2016, 20:33

Re: How to specify a DPI when saving to a BMP file

Post by sugi » 12 Aug 2016, 17:17

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?

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to specify a DPI when saving to a BMP file

Post by support » 12 Aug 2016, 19:29

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:

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();
    }
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

sugi
Posts: 4
Joined: 10 Aug 2016, 20:33

Re: How to specify a DPI when saving to a BMP file

Post by sugi » 12 Aug 2016, 20:43

Thank you. That's exactly what I needed.

Post Reply