Last active
October 27, 2015 03:17
-
-
Save LGM-AdrianHum/4e40e059a284250c9742 to your computer and use it in GitHub Desktop.
Allows you to bind a property in the form of a uri to be bound to an image control, and then cache the image locally on a folder/file location. You will need to ensure that you change the local cache folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.IO; | |
using System.Windows.Data; | |
using System.Windows.Media.Imaging; | |
namespace MyNamespace{ | |
public sealed class UriToCachedImageConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
var url = value as string; | |
if (url == null) | |
return null; | |
var webUri = new Uri(url, UriKind.Absolute); | |
var filename = Path.GetFileName(webUri.AbsolutePath); | |
var localFilePath = Path.Combine("C:\\MyImagesFolder\\", filename); | |
if (File.Exists(localFilePath)) | |
{ | |
return BitmapFrame.Create(new Uri(localFilePath, UriKind.Absolute)); | |
} | |
var image = new BitmapImage(); | |
image.BeginInit(); | |
image.CacheOption = BitmapCacheOption.OnLoad; | |
image.UriSource = webUri; | |
image.EndInit(); | |
SaveImage(image, localFilePath); | |
return image; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void SaveImage(BitmapImage image, string localFilePath) | |
{ | |
image.DownloadCompleted += (sender, args) => | |
{ | |
var encoder = new PngBitmapEncoder(); | |
encoder.Frames.Add(BitmapFrame.Create((BitmapImage) sender)); | |
using (var filestream = new FileStream(localFilePath, FileMode.Create)) | |
{ | |
encoder.Save(filestream); | |
} | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:u="clr-namespace:MyNamespace" | |
d:DesignHeight="500" | |
d:DesignWidth="420"> | |
<UserControl.Resources> | |
<ResourceDictionary> | |
<u:UriToCachedImageConverter x:Key="UrlToCachedImageConverter" /> | |
</ResourceDictionary> | |
</UserControl.Resources> | |
</UserControl> | |
<Image Source="{Binding URL, Mode=OneWay, Converter={StaticResource UrlToCachedImageConverter}, IsAsync=true}"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO
Need to change the location of the local cache to a parameter for the converter...