Skip to content

Instantly share code, notes, and snippets.

@chitoku-k
Last active August 29, 2015 14:02
Show Gist options
  • Save chitoku-k/bf2368ed640fecfdab9e to your computer and use it in GitHub Desktop.
Save chitoku-k/bf2368ed640fecfdab9e to your computer and use it in GitHub Desktop.
LazyImageBehavior
using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
public static class LazyBitmapImage
{
public static Task<BitmapImage> GetImage(Uri uri)
{
return Task.Run(() =>
{
var wc = new WebClient { CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable) };
try
{
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(wc.DownloadData(uri));
image.EndInit();
image.Freeze();
return image;
}
catch (WebException) { }
catch (IOException) { }
catch (InvalidOperationException) { }
finally
{
wc.Dispose();
}
return null;
});
}
public static Task<BitmapImage> GetImage(string uri)
{
return GetImage(new Uri(uri));
}
}
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public class LazyImageBehavior
{
#region LazySource 添付プロパティ
[AttachedPropertyBrowsableForType(typeof(Image))]
public static Uri GetLazySource(Image element)
{
return (Uri)element.GetValue(LazySourceProperty);
}
[AttachedPropertyBrowsableForType(typeof(Image))]
public static void SetLazySource(Image element, Uri value)
{
element.SetValue(LazySourceProperty, value);
}
public static readonly DependencyProperty LazySourceProperty =
DependencyProperty.RegisterAttached("LazySource", typeof(Uri), typeof(LazyImageBehavior), new PropertyMetadata(null, LazySource_Changed));
#endregion
#region LazyImageSource 添付プロパティ
[AttachedPropertyBrowsableForType(typeof(ImageBrush))]
public static Uri GetLazyImageSource(ImageBrush element)
{
return (Uri)element.GetValue(LazyImageSourceProperty);
}
[AttachedPropertyBrowsableForType(typeof(ImageBrush))]
public static void SetLazyImageSource(ImageBrush element, Uri value)
{
element.SetValue(LazyImageSourceProperty, value);
}
public static readonly DependencyProperty LazyImageSourceProperty =
DependencyProperty.RegisterAttached("LazyImageSource", typeof(Uri), typeof(LazyImageBehavior), new PropertyMetadata(null, LazyImageSource_Changed));
#endregion
private static async void LazySource_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var element = sender as Image;
if (element == null)
{
return;
}
var image = await LazyBitmapImage.GetImage(e.NewValue as Uri);
if (image != null)
{
element.Source = image;
}
}
private static async void LazyImageSource_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var element = sender as ImageBrush;
if (element == null)
{
return;
}
var image = await LazyBitmapImage.GetImage(e.NewValue as Uri);
if (image != null)
{
element.ImageSource = image;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment