优化图片显示

This commit is contained in:
wule committed 2026-10-19 14:22:25 +08:00
1 parent 1d4867ebf2
commit 17728c074c
3 files changed
+101 -15

No files matched your search

@@ -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;
// 待采购不显示所有采购信息状态(否则会显示作废订单状态)
+1
View File
@@ -46,6 +46,7 @@
<!-- Framework assemblies not already implied by UseWindowsForms/UseWPF -->
<Reference Include="System.Data" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Web.Services" />
</ItemGroup>
+83 -1
View File
@@ -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<BitmapImage> 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)
//{