1 parent
f2d31c4a42
commit
4c82d549ec
10 files changed
+304
-1850
No files matched your search
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
@@ -15,38 +14,27 @@ namespace _29WxOrderWpf.Converters
|
||||
{
|
||||
public class ImageUrlToBitmapImageConverter : IValueConverter
|
||||
{
|
||||
/// <summary> 缓存上限,防止大量缩略图重复下载导致内存膨胀 </summary>
|
||||
private const int MaxCacheCount = 200;
|
||||
|
||||
private readonly ConcurrentDictionary<string, BitmapImage> _imageCache = new ConcurrentDictionary<string, BitmapImage>();
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is string url && Uri.IsWellFormedUriString(url, UriKind.Absolute))
|
||||
{
|
||||
if (_imageCache.TryGetValue(url, out BitmapImage cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 直接加载绑定如果遇到有问题的网络图片会直接崩溃。
|
||||
BitmapImage bitMap = AllBase.ImageBuild.LoadBitmipImageFromUrl(url);
|
||||
if (bitMap != null)
|
||||
{
|
||||
if (_imageCache.Count >= MaxCacheCount)
|
||||
{
|
||||
_imageCache.Clear();
|
||||
}
|
||||
_imageCache[url] = bitMap;
|
||||
}
|
||||
//var bitMap = new BitmapImage(new Uri(url));
|
||||
BitmapImage bitMap = null;
|
||||
//bitMap = LoadImageFromUrl(url + "?imageView2/1/w/100/format/webp|imageMogr2/auto-orient=");
|
||||
bitMap = AllBase.ImageBuild.LoadBitmipImageFromUrl(url);
|
||||
return bitMap;
|
||||
//string errStr = "";
|
||||
//var bitmapImg = GetUrlImg(url, out errStr);
|
||||
//return bitmapImg;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
//return Task.Run(() => new BitmapImage(new Uri(url))); // 使用 Task.Run 来异步加载图片。
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user