using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using System.Windows.Media.Imaging; namespace AIK.Views.Converters { public class ByteArrayToImageSourceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is not byte[] bytes || bytes.Length == 0) return null; try { using var stream = new MemoryStream(bytes); var image = new BitmapImage(); image.BeginInit(); image.StreamSource = stream; image.CacheOption = BitmapCacheOption.OnLoad; // 避免流关闭后图像失效 image.EndInit(); image.Freeze(); // 允许跨线程使用(可选但推荐) return image; } catch { return null; // 或返回默认占位图 } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }