So I have been able to take a multi-page TIFF file and convert it to a single jpeg image but it flattens the TIFF. By flatten it, I mean it only returns the first page. The goal is to retrieve the TIFF (via memory stream), open each page of the TIFF and append it to a new jpeg (or any web image). Thus creating one long image to view on the web without the aid of a plugin. I do have the MODI.dll installed but I am not sure how to use it in this instance but it is an option.
Source Code (using a FileHandler):
#region multi-page tiff to single page jpeg var byteFiles = dfSelectedDocument.File.FileBytes; // <-- FileBytes is a byte[] or byte array source. byte[] jpegBytes; using( var inStream = new MemoryStream( byteFiles ) ) using( var outStream = new MemoryStream() ) { System.Drawing.Image.FromStream( inStream ).Save( outStream, ImageFormat.Jpeg ); jpegBytes = outStream.ToArray(); } context.Response.ContentType = "image/JPEG"; context.Response.AddHeader( "content-disposition", string.Format( "attachment;filename=\"{0}\"", dfSelectedDocument.File.FileName.Replace( ".tiff", ".jpg" ) ) ); context.Response.Buffer = true; context.Response.BinaryWrite( jpegBytes ); #endregion
In case you get the dreadful "A generic error occurred in GDI+" error (which is arguably the Rickroll of all errors) when using the
SelectActiveFrame
method suggested in the other answers, I strongly suggest to use theSystem.Windows.Media.Imaging.TiffBitmapDecoder
class instead (you will need to add a Reference to thePresentationCore.dll
framework library).Here's an example code that does just that (it puts all the TIFF frames into a list of standard Bitmaps):
And here's the
BitmapFromSource
helper method:For further info regarding this workaround, I also suggest to read this blog post I wrote.
have you compressed the jpeg? https://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx
I'm guessing that you'll have to loop over each frame in the TIFF.
Here's an excerpt from Split multi page tiff file:
You should be able to adapt the logic above to append onto your JPG rather than create separate files.