Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind Dynamically Created Bitmap object to Image in WPF [duplicate]

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?

like image 357
Seth Avatar asked Sep 27 '11 02:09

Seth


1 Answers

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)

like image 199
H.B. Avatar answered Oct 21 '22 00:10

H.B.