←Select platform

AutoFixImageResolutionOptions Structure

Summary

Provides the information needed to automatically convert the resolution of digital photos in LEADTOOLS toolkits.

Syntax
C#
VB
C++
public struct AutoFixImageResolutionOptions 
Public Structure AutoFixImageResolutionOptions 
public: 
   value class AutoFixImageResolutionOptions sealed 

Remarks

Automatic resolution conversion is particularly useful in OCR or Document Writer operations.

Typically, a picture taken with a digital camera does not have good resolution information (DPI). (Images originating from scanners have good resolution information, but images from digital cameras do not.) This is because digital cameras usually set the resolution regardless of the width and height of the image captured. The resolution set is an arbitrary number, often 72 DPI.

An example will illustrate the consequences of using an arbitrary DPI. Suppose you take a 12 Megapixels (3000 x 4000) photo with a digital camera. Keeping the original resolution at 72 DPI, the image would be 41.6 x 55.5 inches. If you then convert this image to PDF without adjusting the resolution, it will generate a PDF file that is 41.6 x 55.5 inches. Loaded using a LEADTOOLS document viewer, the image will be loaded onto many pages (5x5 = 25 pages). Most people do not want or expect that. Therefore, it is preferable to adjust the resolution so the image fits inside a single page.

The following methods allow getting and setting the automatic resolution conversion options:

The settings in AutoFixImageResolutionOptions are used under the following conditions:

  • Directly if RasterImage.AutoFixResolution is called.

  • Indirectly when using RasterCodecs with any load method (RasterCodecs.Load, RasterCodecs.LoadAync, etc.) if the value of CodecsLoadOptions is true. The load methods use the settings in AutoFixImageResolutionOptions to determine whether the resolution in a file should be automatically updated to fit inside a rectangle of PageWidth x PageHeight in size.

  • Indirectly if a RasterCodecs.GetInformation (or an equivalent method that retrieves a CodecsImageInfo object for a file whose resolution should be automatically converted) is called. The XResolution and YResolution members will be set to the converted resolution. In addition, CodecsImageInfo will have the IsCorrectedResolution value set to true.

The fit calculation is a smart algorithm that measures the rectangle area, which means that if an image resolution needs to be updated, it will either be adjusted to fit inside a PageWidth x PageHeight rectangle, or in a PageHeight x PageWidth rectangle, whichever fits best.

The following examples assume the following conditions:

  • AutoFixImageResolutionOptions has been set
  • PageWidth = 8.5
  • PageHeight = 11
  • Unit = AutoFixImageResolutionUnit.Inch (the settings for the US letter paper size)
  • MinResolution = 96

If a Load method or RasterImage.AutoFixResolution is called:

  1. If the image is 3000 x 4000 and 72 DPI -> The image will be converted to fit inside an 8.5" x 11" rectangle, so the resolution will be updated to 352 DPI.
  2. If the image is 4000 x 3000 and 72 DPI -> The image will be converted to fit inside an 11" x 8.5" rectangle, so the resolution will be updated to 352 DPI.
  3. If the image is 4000 x 3000 and 150 DPI -> The image will be left unchanged, so the resulting resolution will still be 150 DPI.
  4. If the image is 3000 x 4000 and 96 DPI -> The image will be left unchanged, so the resulting resolution will still be 96 DPI.

Example

The following example shows how to instruct LEADTOOLS to automatically convert the resolution in digital photos (images with a resolution less than 96):

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using LeadtoolsExamples.Common; 
 
public static void AutoFixImageResolutionOptions_Example() 
{ 
   // Simulate a digital camera image 
   string fileName = Path.Combine(ImagesPath.Path, "digital-camera.jpg"); 
   int pixelWidth = 3042; 
   int pixelHeight = 4032; 
   int resolution = 72; 
   using (var rasterImage = RasterImage.Create(pixelWidth, pixelHeight, 24, resolution, RasterColor.White)) 
   { 
      using (var codecs = new RasterCodecs()) 
      { 
         codecs.Save(rasterImage, fileName, RasterImageFormat.ExifJpeg411, 24); 
      } 
   } 
 
   Console.WriteLine("Default"); 
 
   // Load it using the default values, it should be the original size 
   using (var codecs = new RasterCodecs()) 
   { 
      using (var rasterImage = codecs.Load(fileName, 1)) 
      { 
         Console.WriteLine("image size {0} by {1} pixels at {2} DPI", rasterImage.Width, rasterImage.Height, rasterImage.XResolution); 
         // Show its size in inches 
         double inchesWidth = (double)rasterImage.Width / rasterImage.XResolution; 
         double inchesHeight = (double)rasterImage.Height / rasterImage.YResolution; 
         Console.WriteLine("size in inches {0} by {1}", inchesWidth, inchesHeight); 
         Debug.Assert(rasterImage.Width == pixelWidth); 
         Debug.Assert(rasterImage.Height == pixelHeight); 
         Debug.Assert(rasterImage.XResolution == resolution); 
         Debug.Assert(rasterImage.YResolution == resolution); 
      } 
   } 
 
   Console.WriteLine("Fixing the resolution"); 
 
   // Automatically fix its resolution next time we load it 
   AutoFixImageResolutionOptions options = RasterDefaults.GetAutoFixImageResolutionOptions(); 
   options.PageWidth = 8.5; 
   options.PageHeight = 11; 
   options.Unit = AutoFixImageResolutionUnit.Inch; 
   options.MinResolution = 96; 
   RasterDefaults.SetAutoFixImageResolutionOptions(options); 
 
   using (var codecs = new RasterCodecs()) 
   { 
      // Use the option with this RasterCodecs 
      codecs.Options.Load.AutoFixImageResolution = true; 
      using (var rasterImage = codecs.Load(fileName, 1)) 
      { 
         Console.WriteLine("image size {0} by {1} pixels at {2} DPI", rasterImage.Width, rasterImage.Height, rasterImage.XResolution); 
         // Show its size in inches 
         double inchesWidth = (double)rasterImage.Width / rasterImage.XResolution; 
         double inchesHeight = (double)rasterImage.Height / rasterImage.YResolution; 
         Console.WriteLine("size in inches {0} by {1}", inchesWidth, inchesHeight); 
         Debug.Assert(rasterImage.Width == pixelWidth); 
         Debug.Assert(rasterImage.Height == pixelHeight); 
         Debug.Assert((int)inchesWidth <= 8.5); 
         Debug.Assert((int)inchesHeight <= 11); 
      } 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Core 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Controls 
Imports Leadtools.Dicom 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Public Shared Sub AutoFixImageResolutionOptions_Example() 
   ' Simulate a digital camera image 
   Dim fileName As String = Path.Combine(LEAD_VARS.ImagesDir, "digital-camera.jpg") 
   Dim pixelWidth As Integer = 3042 
   Dim pixelHeight As Integer = 4032 
   Dim resolution As Integer = 72 
   Using rasterImage As RasterImage = RasterImage.Create(pixelWidth, pixelHeight, 24, resolution, RasterColor.White) 
      Using codecs As New RasterCodecs() 
         codecs.Save(rasterImage, fileName, RasterImageFormat.ExifJpeg411, 24) 
      End Using 
   End Using 
 
   Console.WriteLine("Default") 
 
   ' Load it using the default values, it should be the original size 
   Using codecs As New RasterCodecs() 
      Using rasterImage As RasterImage = codecs.Load(fileName, 1) 
         Console.WriteLine("image size {0} by {1} pixels at {2} DPI", rasterImage.Width, rasterImage.Height, rasterImage.XResolution) 
         ' Show its size in inches 
         Dim inchesWidth As Double = rasterImage.Width / rasterImage.XResolution 
         Dim inchesHeight As Double = rasterImage.Height / rasterImage.YResolution 
         Console.WriteLine("size in inches {0} by {1}", inchesWidth, inchesHeight) 
         Debug.Assert(rasterImage.Width = pixelWidth) 
         Debug.Assert(rasterImage.Height = pixelHeight) 
         Debug.Assert(rasterImage.XResolution = resolution) 
         Debug.Assert(rasterImage.YResolution = resolution) 
      End Using 
   End Using 
 
   Console.WriteLine("Fixing the resolution") 
 
   ' Automatically fix its resolution next time we load it 
   Dim options As AutoFixImageResolutionOptions = RasterDefaults.GetAutoFixImageResolutionOptions() 
   options.PageWidth = 8.5 
   options.PageHeight = 11 
   options.Unit = AutoFixImageResolutionUnit.Inch 
   options.MinResolution = 96 
   RasterDefaults.SetAutoFixImageResolutionOptions(options) 
 
   Using codecs As New RasterCodecs() 
      ' Use the option with this RasterCodecs 
      codecs.Options.Load.AutoFixImageResolution = True 
      Using rasterImage As RasterImage = codecs.Load(fileName, 1) 
         Console.WriteLine("image size {0} by {1} pixels at {2} DPI", rasterImage.Width, rasterImage.Height, rasterImage.XResolution) 
         ' Show its size in inches 
         Dim inchesWidth As Double = rasterImage.Width / rasterImage.XResolution 
         Dim inchesHeight As Double = rasterImage.Height / rasterImage.YResolution 
         Console.WriteLine("size in inches {0} by {1}", inchesWidth, inchesHeight) 
         Debug.Assert(rasterImage.Width = pixelWidth) 
         Debug.Assert(rasterImage.Height = pixelHeight) 
         Debug.Assert(CType(inchesWidth, Integer) <= 8.5) 
         Debug.Assert(CType(inchesHeight, Integer) <= 11) 
      End Using 
   End Using 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

See Also

Reference

Leadtools Namespace

Help Version 20.0.2019.9.19
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2019 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly