2010-08-26 15 views
5

पर एकल टिफ के मल्टी-पेज टिफ़ को विभाजित करना मैंने हाल ही में विंडोज एक्सपी 32 बिट से विंडोज 7 64 बिट तक एक नए विकास बॉक्स में स्थानांतरित किया। दोनों मशीनें विजुअल स्टूडियो 2010 के साथ विकास के लिए नेट फ्रेमवर्क संस्करण 4.0 चला रही हैं।विंडोज 7/.NET 4.0

विंडोज 7 64 बिट में अपग्रेड करने के बाद, एक बहु पृष्ठ टिफ छवि को अलग-अलग छवियों में विभाजित करने के लिए मेरा कोड अब टूटा हुआ है (XP पर पहले ठीक काम किया गया है एमएस भरने के आदेश बग को छोड़कर 32 बिट)। डिबगिंग के बाद, बिटमैप मेटाडाटा को नेट फ्रेमवर्क द्वारा सही ढंग से पढ़ा जाता प्रतीत होता है, हालांकि, स्टैक में कुछ घटक गलत तरीके से कुछ टिफ़ टैग (273, 278, और 279) को जारी रखते हैं। मैंने जीडीआई +, और फ्री इमेज लाइब्रेरी समेत विभाजन को पूरा करने के कई तरीकों की कोशिश की है, लेकिन सभी नेट के भीतर विफल हो गए हैं। मैं छवि टैग और वैध टैग मानों के साथ एक और तृतीय पक्ष टूल का उपयोग कर टिफ को सफलतापूर्वक विभाजित करने में सक्षम था।

विशेष रूप से, टिफ़ टैग 273, 278 (257 से मेल खाना चाहिए लेकिन नहीं), और 279 में गलत मान हैं।

क्या यह ज्ञात माइक्रोसॉफ्ट मुद्दा है? क्या आसपास कोई काम है? क्या मैं यह काम गलत कर रहा हूं? बहुत निराश क्योंकि यह XP 32 पर ठीक काम कर रहा था और यह कि ओएस तैनाती विकल्प नहीं है।

// Copy bytes into our memory 
using (MemoryStream ms = new MemoryStream()) 
{ 
    using (BinaryWriter bw = new BinaryWriter(ms)) 
    { 
     // Get the multi page tiff image's size, and allocate a place to put it. 
     int size = RdmInstance.ImageSize; 
     byte[] imgContents = new byte[size + 1]; 

     // Create the pointer and assign the Rdm image address to it 
     IntPtr pointerToImage = new IntPtr(RdmInstance.ImageData); 

     // Copy the bytes from unmanaged memory to managed memory 
     Marshal.Copy(pointerToImage, imgContents, 0, size); 

     // Now, write that contents into our memory stream 
     bw.Write(imgContents); 


     // Check for multiple tiff pages, split them out and write them out correctly for the Fed 
     TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 

     if (decoder.Frames.Count > 0) 
     { 

      // check for multi page tiff 
      for (int i = 0; i < decoder.Frames.Count; i++) 
      { 
       log.InfoFormat("Loading Multi Page Tiff Frame [{0}]... to bitmap", i); 

       // First is front, second is back 
       // TODO - it would be better to get this out of tiff tag RDM sets with the page info 
       string fileName = (i == 0) ? frontFileName : backFileName; 
       BitmapSource bmSrc = decoder.Frames[i]; 
       TiffBitmapEncoder encoder = new TiffBitmapEncoder(); 

       encoder.Compression = TiffCompressOption.Ccitt4; 
       encoder.Frames.Add(BitmapFrame.Create(bmSrc)); 

       log.InfoFormat("Saving Multi Page Tiff Frame [{0}]... to file {1}.", i, fileName); 
       using (var fs = new FileStream(fileName, FileMode.Create)) 
       { 
        encoder.Save(fs); 
       } 

       /* 
       * jknipp - 6/4/2010 
       * Microsoft has a bug in their TiffBitmapEncoder where 
       * they incorrectly set tag 266 (Fill Order) to 0, where the TIFF 
       * spec says it should be 1 or 2. We fix this here. 
       * Reopen the stupid file and fix the fill order 
       */ 
       using (var file = new FileStream(fileName, FileMode.Open)) 
       { 
        TiffBitmapDecoder output = new TiffBitmapDecoder(file, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
        InPlaceBitmapMetadataWriter metadata = output.Frames[0].CreateInPlaceBitmapMetadataWriter(); 

        var fillOrder = metadata.GetQuery("/ifd/{ushort=266}"); 
        log.DebugFormat("Read Fill Order Metadata tag as {0}", fillOrder); 

        // If .Net added a bogus fill order, correct it 
        if (fillOrder != null && (ushort)fillOrder == 0) 
        { 
         log.InfoFormat("Correcting FILL ORDER in file {0}", fileName); 
         metadata.SetQuery("/ifd/{ushort=266}", (ushort)1); 

         // Try to save new metadata 
         if (metadata.TrySave()) 
         { 
          fillOrder = metadata.GetQuery("/ifd/{ushort=266}"); 
          log.Info("Fill order correction successful!"); 
          log.DebugFormat("Read New Fill Order Metadata tag as {0}", fillOrder); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

उत्तर

3

आप इसके लिए LibTiff.Net लाइब्रेरी का प्रयास कर सकते हैं। यह मुफ़्त और ओपन-सोर्स (बीएसडी लाइसेंस) है।

लाइब्रेरी tiffcp उपयोगिता के साथ आता है जिसका उपयोग आपके कोड से TIFF छवियों को विभाजित/विलय करने के लिए किया जा सकता है। या आप नमूना के रूप में उस उपयोगिता के स्रोत कोड का उपयोग कर सकते हैं।

LibTiff.Net में आपके आवेदन से टिफसीपी का उपयोग करके टीआईएफएफ छवियों को विलय और विभाजित करने के नमूने भी शामिल हैं।

अस्वीकरण: मैं पुस्तकालय के रखरखाव में से एक हूं।

+0

मैं LibTiff.Net कल का उपयोग कर को देखा था, लेकिन करने का निर्णय लिया आलसी रहो। मेरे आवेदन में टिफसीपी कोड हैकिंग के बाद, यह फिर से काम कर रहा है! –

+0

बढ़िया है कि LibTiff.Net ने आपकी मदद की :-) – Bobrovsky

3

अपने प्रारंभिक कोड से परखने के बाद, मेरा उत्तर यहाँ अपने विशिष्ट जरूरतों के लिए सम्मान के साथ अनुभवहीन हो सकता है, लेकिन फिर भी, यह मेरे लिए काम करता है:

public static MyImage New(string filePath, ImageFormat imageFormat, 
    int imageSequence = 0) 
{ 
    byte[] imageBytes; 

    using (FileStream imageStream = File.OpenRead(filePath)) 
    { 
     Image tmpImg = Image.FromStream(imageStream); 
     if (imageSequence > 0) 
     { 
      tmpImg.SelectActiveFrame(FrameDimension.Page, imageSequence); 
     } 
     imageBytes = ToByteArray(tmpImg, imageFormat); 
     //FileStream must be open for entire lifetime of the Image 
     imageStream.Close(); 
    } 
    return new MyImage(filePath, imageBytes); 
} 

public static byte[] ToByteArray(Image image, ImageFormat imageFormat) 
{ 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     image.Save(ms, imageFormat); 
     byte[] bytes = ms.ToArray(); 
     ms.Close(); 
     return bytes; 
    } 
}