C#:如何通过MemoryStream将多页TIFF转换为一个长图像? [英] C#: How do I convert a multi-page TIFF via MemoryStream into one long image?

查看:409
本文介绍了C#:如何通过MemoryStream将多页TIFF转换为一个长图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经能够拍摄多页TIFF文件并将其转换为单个jpeg图像,但它会使TIFF变平。通过展平它,我的意思是它只返回第一页。目标是检索TIFF(通过内存流),打开TIFF的每一页并将其附加到新的jpeg(或任何Web图像)。因此,无需插件即可在Web上创建一个长图像。我确实安装了MODI.dll,但我不知道如何在这个实例中使用它,但它是一个选项。

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.


  • 源代码(使用FileHandler):

  • 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


推荐答案

如果您在使用 SelectActiveFrame 方法时遇到可怕的GDI +中发生一般错误错误(可以说是所有错误的Rickroll)在其他答案中建议,我强烈建议使用 System.Windows.Media.Imaging.TiffBitmapDecoder 类(您需要添加参考 PresentationCore.dll 框架库。)

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 the System.Windows.Media.Imaging.TiffBitmapDecoder class instead (you will need to add a Reference to the PresentationCore.dll framework library).

这是一个示例代码,它就是这样做的将TIFF帧转换为标准位图列表):

Here's an example code that does just that (it puts all the TIFF frames into a list of standard Bitmaps):

List<System.Drawing.Bitmap> bmpLst = new List<System.Drawing.Bitmap>();

using (var msTemp = new MemoryStream(data))
{
    TiffBitmapDecoder decoder = new TiffBitmapDecoder(msTemp, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    int totFrames = decoder.Frames.Count;

    for (int i = 0; i < totFrames; ++i)
    {
        // Create bitmap to hold the single frame
        System.Drawing.Bitmap bmpSingleFrame = BitmapFromSource(decoder.Frames[i]);
        // add the frame (as a bitmap) to the bitmap list
        bmpLst.Add(bmpSingleFrame);
    }
}

这里是 BitmapFromSource 帮助方法:

public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
    Bitmap bitmap;
    using (var outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new Bitmap(outStream);
    }
    return bitmap;
}

有关此解决方法的更多信息,我还建议阅读这篇文章

For further info regarding this workaround, I also suggest to read this post.

这篇关于C#:如何通过MemoryStream将多页TIFF转换为一个长图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆