WPF에서 이미지를 표시하는 가장 좋은 방법
-
05-07-2019 - |
문제
현재 나는 초음파 스캐닝 프로젝트를 진행하고 있는데, 이는 프로브에서 Aquired a Aquired를 표시하여 다음 코드를 작성하고 있습니다.
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
?
또한 첫 번째 코드가 얼마나 자주 실행됩니까? 당신은 그것을 변경하거나 필요할 때 변화하는 것을 고려할 수 있습니다.
제휴하지 않습니다 StackOverflow