using System;using System.IO;using System.IO.IsolatedStorage;using System.Windows.Data;using System.Windows.Media.Imaging;using Microsoft.Phone;namespace XXXX.Common{public class FilePathToImageConverter : IValueConverter{private static IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){string path = value as string;if (String.IsNullOrEmpty(path))return null;if ((path.Length > 9) && (path.ToLower().Substring(0, 9).Equals("isostore:"))){using (IsolatedStorageFileStream isfs = isf.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read)){WriteableBitmap wb = PictureDecoder.DecodeJpeg(isfs, 80, 50);return wb;}}else{BitmapImage bi = new BitmapImage(new Uri(path, UriKind.Relative));return bi;}}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){throw new NotImplementedException();}}}
可以這樣使用
<common:FilePathToImageConverter x:Key="FilePathToImageConverter" /><Image x:Name="Image1" Source="{Binding ImageSource,Converter={StaticResource FilePathToImageConverter}}" Width="80" Height="50" ></Image>
沒有留言:
張貼留言