2014-06-24 51 views
2

我想解碼Base64圖像並將其放置到WPF圖像源中。但是,我正在使用的代碼有一個錯誤:Base64圖像到WPF圖像源錯誤沒有適合的成像組件

找不到適合完成此操作的成像組件。

error

我有雙重檢查的Base64編碼字符串我有,事實上,通過使用在線Base64編碼解碼器正確的Base64編碼,所以我知道它不是。

我的代碼:

byte[] binaryData = Convert.FromBase64String(desc.Icon_Path); 
MemoryStream ms = new MemoryStream(binaryData, 0, binaryData.Length); 
ms.Write(binaryData, 0, binaryData.Length); 
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); 
icon.Source = ToWpfImage(image); 
ms.Dispose(); 

public BitmapImage ToWpfImage(System.Drawing.Image img) 
{ 
    MemoryStream ms = new MemoryStream(); 
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 

    BitmapImage ix = new BitmapImage(); 
    ix.BeginInit(); 
    ix.CacheOption = BitmapCacheOption.OnLoad; 
    ix.StreamSource = ms; 
    ix.EndInit(); 
    return ix; 
} 

我應該怎麼做不正確的?

+2

我不認爲你需要ms.Write行嗎?如果你這樣做,你需要在寫入之後設置ms.position = 0。這是因爲在寫入之後流的位置將會在最後。 –

+0

您是否必須將Base64字符串轉換爲System.Drawing.Image? – Jamleck

+0

作爲一個方面說明,我還發現用ImageFormat.Png替換ImageFormat.Bmp似乎解決了這個問題。 – Jamleck

回答

1

我不認爲你需要的MS .Write行是你的嗎?如果你這樣做,你需要在寫入之後設置ms.position = 0。這是因爲在寫入之後流的位置將會在最後。

-1

功能從hereConvert.FromBase64String是你所需要的。看下面的例子。

// MainWindow.xaml 
<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="400" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <!--Using data binding--> 
     <Image Grid.Row="0" Source="{Binding ImageSource}" /> 
     <!--Using Code behind--> 
     <Image Grid.Row="1" x:Name="Icon" /> 
    </Grid> 
</Window> 

以下是後面的代碼。

// MainWindow.xaml.cs 
using System; 
using System.ComponentModel; 
using System.IO; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent();  
     var mainWindowVm = new MainWindowViewModel(); 
     DataContext = mainWindowVm; 

     byte[] buffer = File.ReadAllBytes(@"C:\temp\icon.jpg"); 
     string base64String = Convert.ToBase64String(buffer, 0, buffer.Length); 

     // Option 1 
     byte[] binaryData = Convert.FromBase64String(base64String); 
     Icon.Source = MainWindowViewModel.LoadImage(binaryData);  
     // Option 2 
     mainWindowVm.SetImageSource(Convert.FromBase64String(base64String));  
     // Option 3 
     // mainWindowVm.SetImageSource(File.ReadAllBytes(@"C:\temp\icon.jpg")); 
    } 
} 

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private ImageSource _imageSource; 

    public ImageSource ImageSource 
    { 
     get { return _imageSource; } 
     set 
     { 
      _imageSource = value; 
      OnPropertyChanged("ImageSource"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void SetImageSource(byte[] imageData) 
    { 
     // You can also call LoadImage from here. 
     var image = new BitmapImage(); 
     image.BeginInit(); 
     image.StreamSource = new MemoryStream(imageData); 
     image.EndInit();  
     ImageSource = image; 
    } 

    public static BitmapImage LoadImage(byte[] imageData) 
    { 
     if (imageData == null || imageData.Length == 0) return null; 
     var image = new BitmapImage(); 
     using (var mem = new MemoryStream(imageData)) 
     { 
      mem.Position = 0; 
      image.BeginInit(); 
      image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; 
      image.CacheOption = BitmapCacheOption.OnLoad; 
      image.UriSource = null; 
      image.StreamSource = mem; 
      image.EndInit(); 
     } 
     image.Freeze(); 
     return image; 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 
3

鑑於Base64編碼字符串包含可以通過WPF的BitmapDecoders的一個解碼的編碼圖像緩衝區,你並不需要比這更多的代碼:

public static BitmapSource BitmapFromBase64(string b64string) 
{ 
    var bytes = Convert.FromBase64String(b64string); 

    using (var stream = new MemoryStream(bytes)) 
    { 
     return BitmapFrame.Create(stream, 
      BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
    } 
}