将tiff像素长宽比更改为平方

Dav*_*cia 2 c# tiff barcode aspect-ratio

我正在尝试在多页tiff文件上执行条形码识别.但是tiff文件是从传真服务器(我无法控制)来找我的,它以非方形像素长宽比保存tiff.这导致图像由于纵横比而被严重压扁.我需要将tiff转换为正方形像素宽高比,但不知道如何在C#中执行此操作.我还需要拉伸图像,以便改变宽高比仍然使图像清晰可辨.

有没有人用C#做过这个?或者有没有人使用过将执行此类程序的图像库?

Dav*_*cia 6

如果其他人遇到同样的问题,这是我最终修复这个恼人问题的超级简单方法.

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)));
    }
}
Run Code Online (Sandbox Code Playgroud)