Home : Image Processing : Image Clean-up/Correction Techniques : Fix Non-square Resolution of a Single AtalaImage
Q10425 - HOWTO: Fix Non-square Resolution of a Single AtalaImage

At times, you may run into an image which has a "non-square" resolution - an image where the resolution of the X (width) axis of the image differs from the resolution of the Y (height) axis

The most common cause for such an image is that many older fax systems used to use a resolution of 204 DPI for the width but 196 DPI for the height.

Such images appear normal when viewed in the original orientation, but if rotated or opened in one of our viewers, these types of images often cause unwanted effects - distortion of an image when rotated or annotations that completely fail to line up / appear as intended.

NOTE: if you would like a means of detecting and fixing all such images in a multipage TIFF File please see
Q10153 - HOWTO: Using TiffFile to resample FAX images with unequal resolutions in a multi-page TIFF

Example Code:

/// <summary>
///
Given the incoming AtalaImage, this method will determine if the resolution is non-squire
///
/// if it is not, the original image object is returned by reference
///
/// if it is, then the original image is resampled into a new image, disposed of and a
/// totally new image representing the corrected square-resolution version of the original
/// is returned.
/// </summary>
///
<param name="img"></param>
///
<returns></returns>
private static AtalaImage FixNonSquareResolution(AtalaImage img)
{
    AtalaImage returnImg = null;
    if ((int)Math.Floor(img.Resolution.X) != (int)Math.Floor(img.Resolution.Y))
    {
       
// Do nothing:
       
return img;
    }
   
else 
   
{
       
Size newSize = Size.Empty;
       
ResampleCommand resamp = new ResampleCommand();
       
Dpi newRes;

        // non square
       
if (img.Resolution.X > img.Resolution.Y)
        {
           
// most common: width stays the same, height gets recalculated
           
newSize = new Size(img.Width, (int)((img.Height / img.Resolution.Y) * img.Resolution.X));\
            newRes =
new Dpi(img.Resolution.X, img.Resolution.X, img.Resolution.Units);
        }
       
else
       
{
           
// should almost never see this
           
newSize = new Size((int)((img.Width / img.Resolution.X) * img.Resolution.Y), img.Height);
            newRes =
new Dpi(img.Resolution.Y, img.Resolution.Y, img.Resolution.Units);
        }

        resamp.DestSize = newSize;
        returnImg = resamp.Apply(img).Image;
        returnImg.Resolution = newRes;

        // clean up - returnImg is a full new image -  NOT a reference to img -  so dispose img now that we're done
       
img.Dispose();
       
return returnImg;
    }
}

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved