4

Currently I'm working on a Ultrasound scanning project, which displays the continues images aquired from a probe, to do that I'm writing following code.

XAML:

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

C# Assignment:

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

Converter:

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

This method is costing me lot of (performance), is there any better way to do it??

Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162
  • Is the Convert method itself costing a lot of performance on each run, or is it just being polled too often? I'm curious if a custom control to handle this would be the better approach (as it is a constant stream of images). – Ryan Versaw Jul 29 '09 at 15:24
  • Would suggest any custome/thirdparty controls... or can we stream the Bitmap images – Prashant Cholachagudda Jul 30 '09 at 04:19

1 Answers1

3

Since you're already setting the DataContext in code (not xaml), why not just skip a few steps?

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;

If you have access to GetMeImage(), you may want to consider altering it to better fit into your application - Does it really need to return a Bitmap?

Also, how often is your first piece of code being executed? You may want to consider altering that, or allowing it to vary when it needs to.

Ryan Versaw
  • 6,417
  • 3
  • 30
  • 31