2015-11-17 125 views
1

因此,我已经能够拍摄多页TIFF文件并将其转换为单个jpeg图像,但它会使TIFF变平。通过弄平它,我的意思是它只返回第一页。我们的目标是检索TIFF(通过内存流),打开TIFF的每个页面并将其附加到新的jpeg(或任何网页图像)。从而创建一个长的图像,在没有插件的帮助下在网上查看。我确实安装了MODI.dll,但我不确定如何在这种情况下使用它,但它是一个选项。 (使用的FileHandler)C#:如何通过MemoryStream将多页TIFF转换为一个长图像?

  • 源代码:

    #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 
    

回答

0

我猜你必须循环遍历TIFF中的每一帧。

下面是摘录自Split multi page tiff file

private void Split(string pstrInputFilePath, string pstrOutputPath) 
    { 
     //Get the frame dimension list from the image of the file and 
     Image tiffImage = Image.FromFile(pstrInputFilePath); 
     //get the globally unique identifier (GUID) 
     Guid objGuid = tiffImage.FrameDimensionsList[0]; 
     //create the frame dimension 
     FrameDimension dimension = new FrameDimension(objGuid); 
     //Gets the total number of frames in the .tiff file 
     int noOfPages = tiffImage.GetFrameCount(dimension); 

     ImageCodecInfo encodeInfo = null; 
     ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); 
     for (int j = 0; j < imageEncoders.Length; j++) 
     { 
      if (imageEncoders[j].MimeType == "image/tiff") 
      { 
       encodeInfo = imageEncoders[j]; 
       break; 
      } 
     } 

     // Save the tiff file in the output directory. 
     if (!Directory.Exists(pstrOutputPath)) 
      Directory.CreateDirectory(pstrOutputPath); 

     foreach (Guid guid in tiffImage.FrameDimensionsList) 
     { 
      for (int index = 0; index < noOfPages; index++) 
      { 
       FrameDimension currentFrame = new FrameDimension(guid); 
       tiffImage.SelectActiveFrame(currentFrame, index); 
       tiffImage.Save(string.Concat(pstrOutputPath, @"\", index, ".TIF"), encodeInfo, null); 
      } 
     } 
    } 

你应该能够适应上述逻辑追加到您的JPG,而不是创建单独的文件。

+0

我试过这个,但是当循环到第二个图像时,我收到了“错误:GDI +中发生了一般性错误”。它与获得第一个图像合作。我使用Graphics.FromImage来追加创建并追加图像。不知道我哪里错了。 –

+0

@TomHarlin,触及Opiadeiro上述说法,当您处理未压缩的图像时,是否遇到错误?搜索您的错误和SelectActiveFrame时发现的最常见问题主要是处理压缩图像或处理得太早的流。 –

+0

我不知道我对此有一个明确的答案,因为我正在使用API​​从Pronto创建AlphaTrust创建的Web应用程序来检索TIFF文件(或者,如果我选择的话)。目前,我已经设置了compressionType = OptionDocumentPdfCompressionTypes.None和Dpi = 72.除此之外,我还没有添加任何压缩文件,因为他们进来。如果有更好的方式来转换和合并为一个图像的PDF文件,洗耳恭听。 –

0

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

下面是一个例子代码,做到了这一点(它把所有的TIFF帧为标准位图的列表):

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 helper方法:

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; 
} 

为了进一步有关此解决方法的信息,我也建议read this post

相关问题