WPFで画像を表示する最良の方法
-
05-07-2019 - |
質問
現在、プローブから取得した継続画像を表示する超音波スキャンプロジェクトに取り組んでいます。そのために、次のコードを書いています。
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;
}
この方法では、多くの(パフォーマンス)がかかりますが、 より良い方法はありますか?
解決
既にコード(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
を返す必要が本当にありますか?
また、最初のコードが実行される頻度はどれくらいですか?それを変更したり、必要なときに変更したりすることを検討することもできます。
所属していません StackOverflow