Generally when working with digital images and it is necessary to work with coordinates or dimensions, pixels are used as units. However it is not always convenient. Sometimes it would be preferable to work with physical units such as inches, centimeters, etc.
Let's assume that you need to generate a business card, which has size 3.5x2 inches. How much pixels will it take? How to specify how image should be scaled during printing? To solve these questions, images have parameters which is called resolution - a number of pixels per some physical unit. As usual resolution is measured in DPI (dots per inch) - amount of pixels per one inch. Generally horizontal and vertical resolutions are equal, but sometimes they may differ.
Let's assume you need to create image with size 3.5x2 inches. First of all you should decide what resolution you need. The higher resolution, the better quality (more detailed image, less pixelization) but the high-resolution image is noticeable larger. As usual screen resolution is 72 or 96 DPI, scanned documents have 300 DPI. Note, printers have limited resolution (some printer cannot print more than 600 DPI, some professional models has capacity up to several thousands DPI). So it does not make sense to choose resolution which will not be reproduced by your printing device.
For definiteness let's use resolution 300 DPI. It means that 3.5x2 inches image will have width 3.5*300 = 1050 pixels and height 2*300 = 600 pixels. If we choosen 96 DPI, the width would be 3.5*96 = 336 pixels and height would be 2*96 = 192 pixels.
Not only image size can be measured in inches or other device-independent units. You can also measure all coordinates, sizes and other spatial parameters. All of them will depend on the resolution.
Fortunately you never need to convert units to pixels and vica versa, Graphics Mill for .NET does it automatically. All you need is to specify the resolution for the image (or it can be read from the file along with other data) using HorizontalResolution and VerticalResolution properties of the Bitmap , and specify the unit to measure using Unit property. If you set other unit than Pixel, resolution will be used to interpret all the spatial arguments you pass to the bitmap methods. Also, class GdiGraphics has the same functionality, so you can draw lines, add text and so on using various metrics.
Here is a code example which creates new image with size 3.5x2 inches and adds text and graphics to it:
Dim bitmap As New Aurigma.GraphicsMill.Bitmap 'Set unit to inches bitmap.Unit = Aurigma.GraphicsMill.Unit.Inch 'Set resolution to 300 Dpi bitmap.HorizontalResolution = 300 bitmap.VerticalResolution = 300 'Create new bitmap with size 3.5x2 inches bitmap.Create(Aurigma.GraphicsMill.RgbColor.White, 3.5, 2, _ Aurigma.GraphicsMill.PixelFormat.Format24bppRgb) 'Get graphics Dim graphics As Aurigma.GraphicsMill.Drawing.GdiGraphics = _ bitmap.GetGdiGraphics() 'Create brush object Dim brush As New Aurigma.GraphicsMill.Drawing.SolidBrush( _ Aurigma.GraphicsMill.RgbColor.Black) 'Create font object Dim font As New Aurigma.GraphicsMill.Drawing.Font font.Unit = bitmap.Unit font.HorizontalResolution = bitmap.HorizontalResolution font.VerticalResolution = bitmap.VerticalResolution font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center 'Set font size to 0.4 inches font.Size = 0.4 'Put text at coordinates 1.75, 0.1 in inches graphics.DrawString("Aurigma", font, brush, 1.75F, 0.1F) 'Set font size to 0.3 inches font.Size = 0.5 'Put text at coordinates 1.75, 0.7 in inches graphics.DrawString("Graphics Mill", font, brush, 1.75F, 0.7F) 'Draw line 0.05 inches width Dim pen As New Aurigma.GraphicsMill.Drawing.Pen( _ Aurigma.GraphicsMill.RgbColor.Black, 0.05F) graphics.DrawLine(pen, 0.0F, 1.5F, 3.5F, 1.5F)
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(); //Set unit to inches bitmap.Unit = Aurigma.GraphicsMill.Unit.Inch; //Set resolution to 300 Dpi bitmap.HorizontalResolution = 300; bitmap.VerticalResolution = 300; //Create new bitmap with size 3.5x2 inches bitmap.Create(Aurigma.GraphicsMill.RgbColor.White, 3.5f, 2f, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb); //Get graphics Aurigma.GraphicsMill.Drawing.GdiGraphics graphics = bitmap.GetGdiGraphics(); //Create brush object Aurigma.GraphicsMill.Drawing.SolidBrush brush = new Aurigma.GraphicsMill.Drawing.SolidBrush( Aurigma.GraphicsMill.RgbColor.Black); //Create font object Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font(); font.Unit = bitmap.Unit; font.VerticalResolution = bitmap.VerticalResolution; font.HorizontalResolution = bitmap.HorizontalResolution; font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center; //Set font size to 0.4 inches font.Size = 0.4f; //Put text at coordinates 1.75, 0.1 in inches graphics.DrawString("Aurigma", font, brush, 1.75F, 0.1F); //Set font size to 0.3 inches font.Size = 0.5f; //Put text at coordinates 1.75, 0.7 in inches graphics.DrawString("Graphics Mill", font, brush, 1.75F, 0.7F); //Draw line 0.05 inches width Aurigma.GraphicsMill.Drawing.Pen pen = new Aurigma.GraphicsMill.Drawing.Pen( Aurigma.GraphicsMill.RgbColor.Black, 0.05F); graphics.DrawLine(pen, 0.0F, 1.5F, 3.5F, 1.5F);
You can use not only inches as device-independent unit. Graphics Mill for .NET supports the following units: