2017-06-13 108 views
0

我正在使用C#,並需要處理Jpeg-XR圖像。但是,這些圖像以base64字符串的形式呈現,需要直接轉換爲Bitmap對象。我可以將它寫入文件並進行轉換,但這會顯着影響我的運行時間。JPEG XR到位圖C#

我想知道是否有人可以幫我一個示例代碼或提示? (我已經嘗試了Magick.Net,但這對我沒有用,而且似乎也無法直接加載JXR圖像)。

非常感謝

+0

我相信大多數,如果不是全部,圖像庫可以在輸入使用的是流。將base64轉換爲字節數組後,將該數組放入MemoryStream中,然後將其送入庫 –

回答

0

JPEG XR以前被稱爲HD Photo和Windows Media Photo。

您可以使用WPF庫中System.Windows.Media.Imaging中的類WmpBitmapDecoder來處理.jxr圖像。

此類定義用於Microsoft Windows Media照片編碼圖像的解碼器。 下面的代碼轉換JXR文件,BMP文件:

 using System.IO; 
     using System.Windows.Media.Imaging; 

     public class JXrLib 
     { 
      public static void JxrToBmp(string source, string target) 
      { 
       Stream imageStreamSource = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); 
       WmpBitmapDecoder decoder = new WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
       BitmapSource bitmapSource = decoder.Frames[0]; 

       var encoder = new BmpBitmapEncoder(); ; 
       encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); 
       using (var stream = new FileStream(target, FileMode.Create)) 
       { 
        encoder.Save(stream); 
       } 

      } 
     } 

的代碼進行測試並運行良好。

備選方案2:

如果您有興趣使用Magick.Net的,你可以使用jxrlib庫https://jxrlib.codeplex.com

將文件複製JXRDecApp.exe和JXREncApp.exe到你的bin目錄,閱讀從具有.jxr擴展名的磁盤上的文件。 (你必須編譯jxrlib使用的Visual Studio)

代碼示例:

 // Read first frame of jxr image 
     //JXRDecApp.exe ,JXREncApp.exe should be located in the path of binaries 
     using (MagickImage image = new MagickImage(@"images\myimage1.jxr")) 
     { 
      // Save frame as bmp 
      image.Write("myimage2.bmp"); 

      // even , Save frame as jxr 
      image.Write("myimage2.jxr"); 
     } 
+0

謝謝。我實際上找到了一種將圖像的字節數組讀入BitmapImage對象的方法,然後將其轉換爲Bitmap。這些的組合(我的輸入是base64,並不想把它從文件中讀取): https://stackoverflow.com/questions/9564174/convert-byte-array-to-image-in-wpf 和 https://stackoverflow.com/questions/6484357/converting-bitmapimage-to-bitmap-and-vice-versa – am17