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