views:

521

answers:

4

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?

A: 

Oh, I forgot to mention. Bitmap.SetResolution may help with the aspect ratio issues. The below stuff is just about resizing.

Check out This page. It discusses two mechanisms for resizing. I suspect in your case bilinear filtering is actually a bad idea, since you probably want things nice and monochrome.

Below is a copy of the naive resize algorithm (written by Christian Graus, from the page linked above), which should be what you want.

public static Bitmap Resize(Bitmap b, int nWidth, int nHeight)
{
    Bitmap bTemp = (Bitmap)b.Clone();
    b = new Bitmap(nWidth, nHeight, bTemp.PixelFormat);

    double nXFactor = (double)bTemp.Width/(double)nWidth;
    double nYFactor = (double)bTemp.Height/(double)nHeight;

    for (int x = 0; x < b.Width; ++x)
        for (int y = 0; y < b.Height; ++y)
            b.SetPixel(x, y, bTemp.GetPixel((int)(Math.Floor(x * nXFactor)),
                      (int)(Math.Floor(y * nYFactor))));

    return b;
}

An alternative mechanism is to abuse the GetThumbNailImage function like this. That code maintains aspect ratio, but removing the code that does that should be simple.

Brian
A: 

I have done this with a couple of image libraries, FreeImage (open source) and Snowbound. (fairly expensive) FreeImage has a c# wrapper and Snowbound is available in a .Net assembly. Both work well.

Resizing them in code would shouldn't be impossible, but 2 color tiffs are sometimes awkward with GDI+.

R Ubben
A: 

Disclaimer: I work at Atalasoft

Our .NET imaging SDK can do this. We have written a KB article to show how using our product, but you can adapt to other SDKs. Basically you need to resample the image and adjust the DPI.

Lou Franco
A: 

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