2012-10-16 2 views
3

단일 티파니를 다중 페이지 티파니에 결합하는 코드를 작성합니다. 출력은 빈 페이지가 끝에옵니다. 입력 파일이 검은 색일 경우 코드가 올바르게 작동합니다. & 흰색이지만 색이있는 .tiff 파일은 아닙니다. 예를 들어 내가 100 개의 파일을주고 입 출력으로 하나의 tiff 파일이 47 페이지로 나오면 나머지는 비어 있습니다.다중 페이지 티프 프로그램 빈 페이지

표준 코드를 사용하여이 기능을 수행하려면 다음을 수행하십시오. 왜 그런 생각이 들지?

using (FileStream fs = new FileStream(fileNameTemp, FileMode.Append, FileAccess.Write)) 
     { 
      System.Windows.Media.Imaging.TiffBitmapEncoder tifEnc = new System.Windows.Media.Imaging.TiffBitmapEncoder(); 
      tifEnc.Compression = System.Windows.Media.Imaging.TiffCompressOption.Default; 

      foreach (string fileName1 in filePaths) 
      { 
       Console.WriteLine("FileName:::" + fileName1); 

       System.Windows.Media.Imaging.BitmapImage bmpImg = new System.Windows.Media.Imaging.BitmapImage(); 
       bmpImg.BeginInit(); 
       bmpImg.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; 
       bmpImg.UriSource = new Uri(fileName1); 
       bmpImg.EndInit(); 

       System.Windows.Media.Imaging.FormatConvertedBitmap fcb = new System.Windows.Media.Imaging.FormatConvertedBitmap(bmpImg, 
                System.Windows.Media.PixelFormats.Rgb24, 
                System.Windows.Media.Imaging.BitmapPalettes.Halftone27, 
                1.0); 
       tifEnc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(fcb)); 



      } 

      tifEnc.Save(fs); 
      fs.Dispose(); 


     } 

미리 감사드립니다.

답변

0

아래 샘플에서는 bitmapImage.EndInit()을 수행 할 때 지연로드를 수행하는 것처럼 BitmapImage가로드되지 않습니다. 메모리 스트림이 삭제되면 이미지가로드되지 않기 때문에 가져 오는 페이지도 흰색입니다. 두 번째 방법은 대상 파일 경로와 원본 목록을 사용합니다.

 public static BitmapImage LoadImage(byte[] bytes) 
     { 
      var memoryStream = new MemoryStream(bytes); 

      var bitmapImage = new BitmapImage(); 
      bitmapImage.BeginInit(); 
      bitmapImage.StreamSource = memoryStream; 
      bitmapImage.EndInit(); 
      return bitmapImage;    
     } 

     public static void CreateTiff(string destPath, params string[] filePaths) 
     { 
      using (FileStream fs = new FileStream(destPath, FileMode.Append, 
                  FileAccess.Write)) 
      { 
       var tifEnc = new TiffBitmapEncoder(); 
       tifEnc.Compression = TiffCompressOption.Default; 

       foreach (string fileName in filePaths) 
       { 
        var image = LoadImage(File.ReadAllBytes(fileName)); 

        tifEnc.Frames.Add(BitmapFrame.Create(image)); 
       } 

       tifEnc.Save(fs); 
      } 

     }