2013-05-07 53 views
0

好吧,從字節到字符串等有無數,但我根本找不到任何關於如何將資源文件轉換爲可視化工作室很好地轉換成一個byte []無論我做什麼,進入一個正常的圖像,所以我可以在我的C#代碼中實際使用它?不能隱式地將類型'byte []'轉換爲'System.Windows.Controls.Image

+0

顯示您codezzzzz .. – 2013-05-07 08:46:05

+1

https://www.google.com/search?q=byte+array+to+image+c%23 – ken2k 2013-05-07 08:47:56

+1

這基本上* *涉及到這個類似的問題http://stackoverflow.com/questions/8897210/byte-to-bitmapimage - 只需將'Image'上的'Source'設置爲'BitmapImage'一次 – 2013-05-07 08:51:31

回答

1

可以使用valueconverter做到這一點 -

public class InMemoryImageValueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var image = new BitmapImage(); 
     var memoryStream = new MemoryStream((byte[])value); 
     image.SetSource(memoryStream); 
     return image; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
     return null; 
    } 
} 

改編自https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.WindowsPhone/MvxInMemoryImageValueConverter.cs#L17

,那麼你可以使用圖像控件作爲

<Image Source="{Binding TheBytes, Converter={StaticResource InMemoryImage}}" /> 
0

如果您的圖像資源應用程序,然後你可以做如下

Uri uriR = new Uri("/WP7SampleProject3;component/images/appbar.feature.email.rest.png", UriKind.Relative); 
BitmapImage imgSourceR = new BitmapImage(uriR); 
this.imageR.Source = imgSourceR; 
相關問題