C# 在WPF中显示图像的最佳方式
目前我正在做一个超声波扫描项目,它显示从探头获得的连续图像,为此我正在编写以下代码 XAML: 转换器:C# 在WPF中显示图像的最佳方式,c#,wpf,.net-3.5,image-processing,C#,Wpf,.net 3.5,Image Processing,目前我正在做一个超声波扫描项目,它显示从探头获得的连续图像,为此我正在编写以下代码 XAML: 转换器: public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null && value is byte[]) { byte[] ByteArray = v
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] ByteArray = value as byte[];
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(ByteArray);
bmp.EndInit();
return bmp;
}
return null;
}
这种方法花费了我很多(性能),
有更好的方法吗???既然您已经在代码(而不是xaml)中设置了
DataContext
,为什么不跳过几个步骤呢
Bitmap myImage = GetMeImage();
imageMem = new MemoryStream();
myImage.Save(imageMem, ImageFormat.Png);
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(imageMem.ToArray());
bmp.EndInit();
imgScan.Source = bmp;
如果您可以访问<代码> GETMEIMAGE()/<代码>,您可能需要考虑更改它以更好地适合您的应用程序。它真的需要返回<代码>位图< /C> > /P>
另外,您的第一段代码多长时间执行一次?你可能想考虑改变它,或者允许它在需要时改变。是转换方法本身在每次运行中耗费了大量的性能,还是只是经常被轮询?我很好奇,如果一个自定义控件来处理这将是更好的方法(因为它是一个恒定的图像流)。会建议任何自定义/第三方控件。。。或者我们可以流式处理位图图像以获得响应。。。我真的没有访问GetMeImage()的权限,只返回位图:(代码必须每500毫秒运行一次。。。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] ByteArray = value as byte[];
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(ByteArray);
bmp.EndInit();
return bmp;
}
return null;
}
Bitmap myImage = GetMeImage();
imageMem = new MemoryStream();
myImage.Save(imageMem, ImageFormat.Png);
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(imageMem.ToArray());
bmp.EndInit();
imgScan.Source = bmp;