【问题标题】:Best way to display image in WPF在 WPF 中显示图像的最佳方式
【发布时间】:2010-11-15 02:38:02
【问题描述】:

目前我正在处理一个超声波扫描项目,该项目显示从探头获取的连续图像,为此我正在编写以下代码。

XAML:

<Image Name="imgScan" DataContext="{Binding}" Source="{Binding Path=prescanImage,Converter={StaticResource imgConverter}}" />

C# 作业:

Bitmap myImage = GetMeImage();
imageMem = new MemoryStream();
myImage .Save(imageMem, ImageFormat.Png);
imgScan.DataContext = new { prescanImage = imageMem.ToArray() };

转换器:

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;
}

这种方法花费了我很多(性能), 有没有更好的办法??

【问题讨论】:

  • Convert 方法本身是否会在每次运行时消耗大量性能,还是只是轮询过于频繁?我很好奇处理这个问题的自定义控件是否是更好的方法(因为它是一个恒定的图像流)。
  • 会建议任何客户/第三方控件...或者我们可以流式传输位图图像

标签: c# wpf .net-3.5 image-processing


【解决方案1】:

既然您已经在代码(不是 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(),您可能需要考虑对其进行更改以更好地适应您的应用程序 - 它真的需要返回 Bitmap 吗?

另外,您的第一段代码多久执行一次?您可能需要考虑更改它,或者允许它在需要时改变。

【讨论】:

  • 感谢您的回复...我真的无法访问 GetMeImage() 并且只返回 Bitmap :( 代码必须每 500 毫秒运行一次...
猜你喜欢
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多