簡體   English   中英

在WPF中顯示圖像的最佳方式

[英]Best way to display image in WPF

目前我正在研究超聲波掃描項目,該項目顯示從探測器獲取的連續圖像,為此我正在編寫以下代碼。

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

這種方法耗費了我很多(性能),有沒有更好的方法呢?

由於您已經在代碼中設置了DataContext (而不是xaml),為什么不跳過幾個步驟呢?

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嗎?

此外,您的第一段代碼執行的頻率是多少? 您可能需要考慮更改,或允許它在需要時更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM