0

I'm using Visual Studio 2017. I've got a WriteableBitmap that I want to display to the user. To do that, I've got this component in my MainWindow.xaml:

<Image x:Name="FrameDisplayImage" Grid.Row="1" Stretch="Uniform"/>

Now I try to assign the image to the component like so in the MainWindow.xaml.cs:

FrameDisplayImage.Source = this.colorBitmap;

This doesn't work, the error is that the name FrameDisplayImage is not available in the current context.

I'm probably missing some include or connection between the xaml and the cs - but I'm completely new to C# and can't get it to work. Can someone point me in the right direction?

Christallkeks
  • 525
  • 5
  • 18

1 Answers1

0

Create a method to convert bitmap to image source then you can display it on the wpf xaml like this.

FrameDisplayImage.Source = BitmapToImageSource(this.colorBitmap);

BitmapImage BitmapToImageSource(Bitmap bitmap)
{
    using (MemoryStream memory = new MemoryStream())
    {
        bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
        memory.Position = 0;
        BitmapImage bitmapimage = new BitmapImage();
        bitmapimage.BeginInit();
        bitmapimage.StreamSource = memory;
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapimage.EndInit();

        return bitmapimage;
    }
}
bingcheng45
  • 398
  • 4
  • 14
  • Thanks for your answer, I meanwhile found that the code actually works, I was just missing a `this.` before `FrameDisplayImage` - embarassing... Sorry for stealing your time by not deleting the question since or posting the answer myself... – Christallkeks Nov 29 '17 at 09:15
  • @Christallkeks its ok man. good that you have found a fix for it – bingcheng45 Nov 29 '17 at 09:17