diff --git a/29WxOrderWpf/Ctrls/OrderCtrls/OrderListRowItemCtrl.xaml.cs b/29WxOrderWpf/Ctrls/OrderCtrls/OrderListRowItemCtrl.xaml.cs index 1ca9424..a43dc34 100644 --- a/29WxOrderWpf/Ctrls/OrderCtrls/OrderListRowItemCtrl.xaml.cs +++ b/29WxOrderWpf/Ctrls/OrderCtrls/OrderListRowItemCtrl.xaml.cs @@ -769,7 +769,7 @@ namespace _29WxOrderWpf.Ctrls.OrderCtrls } } - private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) + private async void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.DataContext is oms_wei_order_item_info order_item) { @@ -782,19 +782,22 @@ namespace _29WxOrderWpf.Ctrls.OrderCtrls // 绑定时即刻生效,无需再次手动调用 //order_item.UpdateOrderItemBuyButton(); - Task.Run(() => - { - // 异步加载图片防止卡顿 - // 缩略图在有的电脑上无法显示 - //BitmapImage bImg = ImageUrlToBitmapImageConverter.LoadImageFromUrl(order_item.thumb_img + "?imageView2/1/w/100/format/webp|imageMogr2/auto-orient="); - BitmapImage bImg = AllBase.ImageBuild.LoadBitmipImageFromUrl(order_item.thumb_img, 100, 100); - //BitmapImage bImg = AllBase.ImageBuild.LoadBitmipImageFromUrl(order_item.thumb_img); - Thread.Sleep(Randomx.GetId(1000)); - Dispatcher.Invoke(new Action(() => - { - imgOrderItem.Source = bImg; - })); - }); + //Task.Run(() => + //{ + // // 异步加载图片防止卡顿 + // // 缩略图在有的电脑上无法显示 + // //BitmapImage bImg = ImageUrlToBitmapImageConverter.LoadImageFromUrl(order_item.thumb_img + "?imageView2/1/w/100/format/webp|imageMogr2/auto-orient="); + // BitmapImage bImg = AllBase.ImageBuild.LoadBitmipImageFromUrl(order_item.thumb_img, 100, 100); + // //BitmapImage bImg = AllBase.ImageBuild.LoadBitmipImageFromUrl(order_item.thumb_img); + // Thread.Sleep(Randomx.GetId(1000)); + // Dispatcher.Invoke(new Action(() => + // { + // imgOrderItem.Source = bImg; + // })); + //}); + + var bImg = await AllBase.ImageBuild.LoadBitmipImageFromUrlAsync(order_item.thumb_img + "?imageView2/1/w/100/format/webp|imageMogr2/auto-orient="); + imgOrderItem.Source = bImg; // 待采购不显示所有采购信息状态(否则会显示作废订单状态) diff --git a/AllBase/29WxOrderAllBase.csproj b/AllBase/29WxOrderAllBase.csproj index 86a136a..3ae967c 100644 --- a/AllBase/29WxOrderAllBase.csproj +++ b/AllBase/29WxOrderAllBase.csproj @@ -46,6 +46,7 @@ + diff --git a/AllBase/ImageBuild.cs b/AllBase/ImageBuild.cs index d511dd4..08673fd 100644 --- a/AllBase/ImageBuild.cs +++ b/AllBase/ImageBuild.cs @@ -1,11 +1,14 @@ -using System; +using CefSharp.DevTools.IO; +using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Net; +using System.Net.Http; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Media.Imaging; @@ -423,6 +426,85 @@ namespace AllBase return bitmap; } + public static async Task LoadBitmipImageFromUrlAsync(string url, int? width = null, int? height = null) + { + BitmapImage bitmap = new BitmapImage(); + + try + { + //using (WebClient webClient = new WebClient()) + //{ + //webClient.DownloadDataCompleted += (sender, e) => + //{ + // if (e.Cancelled) + // { + // //Console.WriteLine("下载已取消"); + // bitmap = null; + // } + // else if (e.Error != null) + // { + // //Console.WriteLine("下载出错: " + e.Error.Message); + // bitmap = null; + // } + // else + // { + // byte[] data = (byte[])e.Result; + // //string text = System.Text.Encoding.UTF8.GetString(data); + // //Console.WriteLine(text); + + // // 下载图片数据到内存流 + // using (MemoryStream stream = new MemoryStream(data)) + // { + // stream.Position = 0; // 重置流的位置到开始处 + // bitmap.BeginInit(); + // if (width != null && height != null) + // { + // bitmap.DecodePixelWidth = width.Value; + // bitmap.DecodePixelHeight = height.Value; + // } + // bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat; + // bitmap.CacheOption = BitmapCacheOption.OnLoad; // 或者使用 BitmapCacheOption.OnLoad 以减少内存使用 + // bitmap.UriSource = null; // 清除可能的旧UriSource,防止缓存问题 + // bitmap.StreamSource = stream; + // bitmap.EndInit(); + // bitmap.Freeze(); // 防止后续更改,提高性能 + // } + // } + //}; + //await webClient.DownloadDataAsync(new Uri(url)); + //} + + using (HttpClient client = new HttpClient()) + { + // 下载图片数据到内存流 + using (MemoryStream stream = new MemoryStream(await client.GetByteArrayAsync(url))) + { + stream.Position = 0; // 重置流的位置到开始处 + bitmap.BeginInit(); + if (width != null && height != null) + { + bitmap.DecodePixelWidth = width.Value; + bitmap.DecodePixelHeight = height.Value; + } + bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat; + bitmap.CacheOption = BitmapCacheOption.OnLoad; // 或者使用 BitmapCacheOption.OnLoad 以减少内存使用 + bitmap.UriSource = null; // 清除可能的旧UriSource,防止缓存问题 + bitmap.StreamSource = stream; + bitmap.EndInit(); + bitmap.Freeze(); // 防止后续更改,提高性能 + } + } + } + catch (Exception ex) + { + // 处理异常,例如打印错误信息或设置一个默认图片等 + //Console.WriteLine("Error loading image: " + ex.Message); + return null; + } + + return bitmap; + } + //public string BitmapImageToBase64(BitmapImage bitmapImage) //{