将 tiff 像素长宽比更改为正方形

发布于 2024-07-20 12:54:52 字数 224 浏览 11 评论 0原文

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

有人用 C# 做过这个吗? 或者有人使用过可以执行此类过程的图像库吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羅雙樹 2024-07-27 12:54:52

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

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

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)));
    }
}
后eg是否自 2024-07-27 12:54:52

哦,我忘记说了。 Bitmap.SetResolution 可能有助于解决宽高比问题。 下面的内容只是关于调整大小。

请查看此页面。 它讨论了两种调整大小的机制。 我怀疑在你的情况下双线性过滤实际上是一个坏主意,因为你可能想要漂亮和单色的东西。

下面是简单的调整大小算法的副本(由 Christian Graus 编写,来自上面链接的页面),这应该是您想要的。

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

另一种机制是滥用 GetThumbNailImage 函数,例如 this 。 该代码保持了纵横比,但删除执行此操作的代码应该很简单。

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.

风尘浪孓 2024-07-27 12:54:52

我使用了几个图像库、FreeImage(开源)和 Snowbound 来完成此操作。 (相当昂贵)FreeImage 有 ac# 包装器,Snowbound 在 .Net 程序集中可用。 两者都运作良好。

在代码中调整它们的大小应该不是不可能的,但是 2 种颜色的冲突有时对于 GDI+ 来说很尴尬。

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+.

以歌曲疗慰 2024-07-27 12:54:52

免责声明:我在 Atalasoft 工作,

我们的 .NET 成像 SDK 可以做到这一点。 我们编写了一篇知识库文章来展示如何使用我们的产品,但是可以适配其他SDK。 基本上,您需要重新采样图像并调整 DPI。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文