发票助手
This commit is contained in:
1 parent
acd07e6854
commit
f2d31c4a42
10 files changed
+1850
-304
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
@@ -14,27 +15,38 @@ 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
|
||||
{
|
||||
// 直接加载绑定如果遇到有问题的网络图片会直接崩溃。
|
||||
//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);
|
||||
BitmapImage bitMap = AllBase.ImageBuild.LoadBitmipImageFromUrl(url);
|
||||
if (bitMap != null)
|
||||
{
|
||||
if (_imageCache.Count >= MaxCacheCount)
|
||||
{
|
||||
_imageCache.Clear();
|
||||
}
|
||||
_imageCache[url] = bitMap;
|
||||
}
|
||||
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