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?
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.
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.
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.
精彩评论