-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathToImageSourceConverter.cs
45 lines (37 loc) · 1.25 KB
/
PathToImageSourceConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#region
using System;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Data;
using System.Windows.Media.Imaging;
#endregion
// With that you can use string path for controls that needed imagesource
public class PathToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string path = value as string;
if (String.IsNullOrEmpty(path))
return null;
BitmapImage bitmap;
if (path.StartsWith("isostore:"))
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var sourceFile = storage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
{
bitmap = new BitmapImage();
bitmap.SetSource(sourceFile);
return bitmap;
}
}
}
bitmap = new BitmapImage(new Uri(path, UriKind.Relative));
return bitmap;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}