You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
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();
|
|
}
|
|
}
|
|
}
|
|
|