WPF 位图图像和 TIFF 与 CMYK +阿尔法

发布于 2024-09-29 22:46:51 字数 1432 浏览 14 评论 0原文

我使用此代码片段加载各种图像文件:

BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();

这对于存储为 RGB、RGB+Alpha 和 CMYK 的 TIFF 文件效果很好。 但是,如果我尝试使用 CMYK 颜色和 Alpha 通道加载 TIFF 文件,则会出现异常(解码器无法将文件格式识别为有效)。

我之前使用的是 <一个 href="http://freeimage.sourceforge.net/" rel="nofollow">FreeImage 库和其上的一个瘦 C# 包装器。 FreeImage 3.x 对这种图像格式有部分支持,即我必须加载 TIFF 两次,一次加载为不透明的 CMYK,一次加载为 RGB+Alpha;由于 FreeImage 最多只能访问 4 个同时颜色通道,因此需要使用此技巧。

我想知道是否有支持的方式来加载 CMYK+Alpha 位图?直接使用 C# 或通过一些互操作代码,但最好不必使用第三方 DLL(.NET 4 框架库除外)。

可以找到此类 TIFF 文件的示例 此处

编辑:我无法再重现该问题,以下代码工作得很好:

BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
byte[] pixels = new byte[bitmap.PixelHeight*bitmap.PixelWidth*5];
bitmap.CopyPixels (pixels, bitmap.PixelWidth * 5, 0);

但我仍然卡住了:如何才能发现源图像被编码为 CMYK 加 Alpha 通道?查看 Format 属性时,我仅得到图像每像素 40 位的信息。所有有趣的东西都存储在以下非公共属性中:

bitmap.Format.FormatFlags == IsCMYK | NChannelAlpha;
bitmap.Format.HasAlpha == true;

是否有任何官方方法可以在不诉诸反射的情况下获取它们?

I am using this code snippet to load various image files:

BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();

This works fine for TIFF files stored as RGB, RGB+Alpha and CMYK. However, if I try to load a TIFF file using CMYK colors and an alpha channel, I get an exception (the file format is not recognized as being valid by the decoder).

I was previously using the FreeImage library and a thin C# wrapper on top of it. FreeImage 3.x has partial support for this kind of image format, i.e. I had to load the TIFF twice, once as CMYK without transparency and once as RGB+Alpha; this trick is needed since FreeImage only gives access to at most 4 simultaneous color channels.

I'd like to know if there is a supported way to load CMYK+Alpha bitmaps? Either directly in C# or by going through some interop code, but preferably without having to use a third-party DLL (other than the .NET 4 framework libraries).

An example of such a TIFF file can be found here.

EDIT : I can no longer reproduce the problem, the following code works just fine:

BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
byte[] pixels = new byte[bitmap.PixelHeight*bitmap.PixelWidth*5];
bitmap.CopyPixels (pixels, bitmap.PixelWidth * 5, 0);

But I am still stuck: how can I find out that the source image was encoded as CMYK plus Alpha channel? When looking at the Format property, I get only the information that the image has 40 bits per pixel. All the interesting stuff is stored in the following non-public properties:

bitmap.Format.FormatFlags == IsCMYK | NChannelAlpha;
bitmap.Format.HasAlpha == true;

Is there any official way of getting to them, without resorting to reflection?

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

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

发布评论

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

评论(3

末が日狂欢 2024-10-06 22:46:51

我只能这么说,因为我遇到了一些文件的问题:首先将 tiff 转换为 png24 然后加载它可能是更好的方法。

如果用户尝试保存 CMYK Tiff 文件并勾选“透明度”,甚至 Photoshop 也会发出警告:“许多程序不支持 TIFF 透明度。保存透明度信息吗?”

因此,在开业之前进行转换可能是安全的方法。

也许 http://msdn.microsoft.com/en-us/ Library/system.drawing.imageconverter.aspx 会做到这一点,但我对此表示怀疑,您可能需要一些额外的管道。

HTH。

I can only say this because I've had the issues with some files: it might be a better way to convert the tiff to png24 first and then load it up.

Even Photoshop puts up a warning if a user tries to save a CMYK Tiff file and ticks 'Transparency': "Many programs do not support transparency in TIFF. Save transparency information?"

So converting prior to opening might be the safe way to go.

Maybe http://msdn.microsoft.com/en-us/library/system.drawing.imageconverter.aspx would do it but I doubt it, you probably need some extra piping.

HTH.

安穩 2024-10-06 22:46:51

这只是猜测,但 GDI+ 可能能够加载此类文件。

System.Drawing.Image等。

有一个互操作类可以在WPF中渲染GDI+图像。

It's only a guess, but GDI+ might be able to load such files.

System.Drawing.Image etc.

There is an interop class which can render GDI+ images in WPF.

罗罗贝儿 2024-10-06 22:46:51

我在链接到这个库的SO上发现了另一个问题:

http://freeimage.sourceforge.net/

< a href="https://stackoverflow.com/questions/294391">适用于 .NET 的良好 Tiff 库

我希望这可能有所帮助。

I found another question on SO which linked to this library:

http://freeimage.sourceforge.net/

Good Tiff library for .NET

I hope this might help.

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