C# – Change tiff pixel aspect ratio to square

aspect-ratiobarcodec++tiff

I'm trying to perform barcode recognition on a multipage tiff file. But the tiff file is coming to me from a fax server (which I don't have control over) that saves the tiff with a non-square pixel aspect ratio. This is causing the image to be badly squashed due to the aspect ratio. I need to convert the tiff to a square pixel aspect ratio, but have no idea how to do that in C#. I'll also need to stretch the image so that changing the aspect ratio still makes the image legible.

Has anyone done this in C#? Or has anyone used an image library that will perform such a procedure?

Best Solution

In case anyone else out there runs into the same issue, here is the super simple way I ended up fixing this annoying issue.

using System.Drawing;
using System.Drawing.Imaging;

// The memoryStream contains multi-page TIFF with different
// variable pixel aspect ratios.
using (Image img = Image.FromStream(memoryStream)) {
    Guid id = img.FrameDimensionsList[0];
    FrameDimension dimension = new FrameDimension(id);
    int totalFrame = img.GetFrameCount(dimension);
    for (int i = 0; i < totalFrame; i++) {
        img.SelectActiveFrame(dimension, i);

        // Faxed documents will have an non-square pixel aspect ratio.
        // If this is the case,adjust the height so that the
        // resulting pixels are square.
        int width = img.Width;
        int height = img.Height;
        if (img.VerticalResolution < img.HorizontalResolution) {
            height = (int)(height * img.HorizontalResolution / img.VerticalResolution);
        }

        bitmaps.Add(new Bitmap(img, new Size(width, height)));
    }
}
Related Question