9

I have a bitmap object that is not a static resource. Normally, with image binding in WPF you can bind to a string path. However, I have a dynamically created bitmap object that I would like to bind to. Is it possible to do something like:

<WrapPanel x:Name="imageWrapPanel" HorizontalAlignment="Center">
    <Image Source="{Binding Material1}" Margin="10" />
    <Image Source="/NightVision;component/Images/concrete_texture.tif" Margin="10" />
</WrapPanel>

And in the code behind file I have a public accessor:

public Bitmap Material1 { 
    get 
    { 
         return new Bitmap(/* assume created somewhere else*/) 
    } 
}

The above is obviously not working however, is there a way to do something similar?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Seth
  • 8,213
  • 14
  • 71
  • 103

1 Answers1

5

The only thing you need to do is convert the Bitmap to an ImageSource which can be used in the Image control. So in your binding you can add a Converter which accomplishes that. The implementation of the conversion is likely to be found in the answers to this question.

(If you have a chance to work directly with BitmapImage (WPF) instead of Bitmap (WinForms) that might be quite a good idea)

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400