问题描述
我正在尝试编写一个例程,该例程将使用LibTiff.net将WPF BitmapSource保存为JPEG编码的TIFF.使用LibTiff提供的示例,我得出以下结论:
I'm trying to write a routine that will save a WPF BitmapSource as a JPEG encoded TIFF using LibTiff.net. Using the examples provided with LibTiff I came up with the following:
private void SaveJpegTiff(BitmapSource source, string filename)
{
if (source.Format != PixelFormats.Rgb24) source = new FormatConvertedBitmap(source, PixelFormats.Rgb24, null, 0);
using (Tiff tiff = Tiff.Open(filename, "w"))
{
tiff.SetField(TiffTag.IMAGEWIDTH, source.PixelWidth);
tiff.SetField(TiffTag.IMAGELENGTH, source.PixelHeight);
tiff.SetField(TiffTag.COMPRESSION, Compression.JPEG);
tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
tiff.SetField(TiffTag.ROWSPERSTRIP, source.PixelHeight);
tiff.SetField(TiffTag.XRESOLUTION, source.DpiX);
tiff.SetField(TiffTag.YRESOLUTION, source.DpiY);
tiff.SetField(TiffTag.BITSPERSAMPLE, 8);
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 3);
tiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int stride = source.PixelWidth * ((source.Format.BitsPerPixel + 7) / 8);
byte[] pixels = new byte[source.PixelHeight * stride];
source.CopyPixels(pixels, stride, 0);
for (int i = 0, offset = 0; i < source.PixelHeight; i++)
{
tiff.WriteScanline(pixels, offset, i, 0);
offset += stride;
}
}
MessageBox.Show("Finished");
}
这将转换图像,我可以看到JPEG图像,但是颜色混乱了.我猜我缺少TIFF的一个或两个标签,或者像光度学解释这样的错误,但是对于需要什么尚不完全清楚.
This converts the image and I can see a JPEG image but the colours are messed up. I'm guessing I'm missing a tag or two for the TIFF or something is wrong like the Photometric interpretation but am not entirely clear on what is needed.
干杯
推荐答案
目前尚不清楚您所说的颜色弄乱了"是什么意思,但可能应该将BitmapSource
的BGR样本转换为LibTiff期望的RGB样本.净
It's not clear what do you mean by saying " colours are messed up" but probably you should convert BGR samples of BitmapSource
to RGB ones expected by LibTiff.Net.
我的意思是,在将像素提供给WriteScanline
方法之前,请确保颜色通道的顺序是RGB(很可能不是).
I mean, make sure the order of color channels is RGB (most probably, it's not) before feeding pixels to WriteScanline
method.
这篇关于使用Libtiff.net将BitmapSource保存为Tiff编码的JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!