【问题标题】:c# Tiff files compression methodologyc# Tiff 文件压缩方法
【发布时间】:2014-01-27 18:22:40
【问题描述】:

我正在尝试理解并实现一段 Tiff 压缩代码。

我已经使用了 2 种不同的技术 - 使用 3rd 方 dll 的 LibTiff.NEt(第一种方法很庞大)和图像保存方法,http://msdn.microsoft.com/en-us/library/ytz20d80%28v=vs.110%29.aspx(第二种方法仅适用于 windows 7 机器,但不适用于 windows 2003 或 2008 服务器)。

现在我正在探索第三种方法。

using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;

        int width = 800;
        int height = 1000;
        int stride = width/8;
        byte[] pixels = new byte[height*stride];

        // Try creating a new image with a custom palette.
        List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
        colors.Add(System.Windows.Media.Colors.Red);
        colors.Add(System.Windows.Media.Colors.Blue);
        colors.Add(System.Windows.Media.Colors.Green);
        BitmapPalette myPalette = new BitmapPalette(colors);

        // Creates a new empty image with the pre-defined palette

        BitmapSource image = BitmapSource.Create(
            width,
            height,
            96,
            96,
            System.Windows.Media.PixelFormats.BlackWhite,
            myPalette, 
            pixels, 
            stride);


FileStream stream = new FileStream(Original_File, FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Ccitt4;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);

但我对这里发生的事情没有完全了解。

很明显,压缩技术正在应用于某种内存流。但是我有点困惑如何将其应用于我的具体案例。我有一个原始的 tiff 文件,我想使用这种方法将其压缩设置为 CCITT 并保存回来。任何人都可以帮忙吗?

我复制了上面的代码并且代码运行了。但我的最终输出文件是纯黑色背景图像。尽管从积极的方面来说,它是正确的压缩类型。

http://msdn.microsoft.com/en-us/library/ms616002%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffcompressoption%28v=vs.100%29.aspx

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-compressionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl

【问题讨论】:

  • 为什么要添加asp.net标签?你做的是asp.net项目吗?
  • 是的。我有一个网页,允许用户输入,然后将其转换为 tiff 文件。此 tiff 文件的原始压缩类型为 LZW。我需要 CCITT 压缩格式。
  • PixelFormatsSystem.Windows.Media,而不是System.Windows.Media.Imaging
  • 谢谢,现在我的代码运行了,但是我得到的输出文件没有任何图像或属性......当我打开文件时,我收到消息 windows 照片编辑器无法打开文件,因为它正在另一个应用程序中使用。

标签: asp.net c#-4.0 tiff


【解决方案1】:

LibTiff.net 有点庞大,因为它基于 LibTiff,它有自己的一系列问题。

我的公司 (Atalasoft) 能够相当轻松地做到这一点,free version of the SDK 将在一些限制条件下完成您想要的任务。重新编码文件的代码如下所示:

public bool ReencodeFile(string path)
{
    AtalaImage image = new AtalaImage(path);
    if (image.PixelFormat == PixelFormat.Pixel1bppIndexed)
    {
        TiffEncoder encoder = new TiffEncoder();
        encoder.Compression = TiffCompression.Group4FaxEncoding;
        image.Save(path, encoder, null); // destroys the original - use carefully
        return true;
    }
    return false;
}

您应该注意的事项:

  • 此代码仅适用于 1bpp 图像
  • 此代码将在多页 TIFF 上正常工作
  • 此代码在原始文件中保留元数据

我希望代码至少检查一下。如果您倾向于有一个更好地保留文件内容的解决方案,您会想要这样做:

public bool ReencodeFile(string origPath, string outputPath)
{
    if (origPath == outputPath) throw new ArgumentException("outputPath needs to be different from input path.");
    TiffDocument doc = new TiffDocuemnt(origPath);

    bool needsReencoding = false;
    for (int i=0; i < doc.Pages; i++) {
        if (doc.Pages[i].PixelFormat == PixelFormat.Pixel1bppIndexed) {
            doc.Pages[i] = new TiffPage(new AtalaImage(origPath, i, null), TiffCompression.Group4FaxEncoding);
            needsReencoding = true;
        }
    }
    if (needsReendcoding)
        doc.Save(outputPath);
    return needsReencoding;
}

此解决方案将尊重文档中的所有页面以及文档元数据。

【讨论】:

  • 谢谢。我仍在尝试使用上述方法来解决我的问题。如果你能帮忙的话。请告诉我。
  • 还有,如果图片原来不是1bpp怎么办?
  • 在 CCITT 中只能编码 1bpp。对于其他位深度,您必须选择不同的编解码器(通常是 flate 或 jpeg)。我建议您花一点时间阅读图像压缩和编解码器。此外,如果您查看 TIFF 规范并认为“我完全可以自己编写这种格式” - 再想一想,它是微妙而草率的(一个不寻常的组合)。
  • 对于第一个语句:- Atalasoft.Imaging.AtalaImage image = new Atalasoft.Imaging.AtalaImage(fileName);我收到错误无法检索此帧的安全描述符。
  • Philo - 我会打电话给我们的支持热线 - 他们是好人(以及像你这样的工程师)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多