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?
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.
In case anyone else out there runs into the same issue, here is the super simple way I ended up fixing this annoying issue.
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.
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.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+.