Bind Dynamically Created Bitmap object to Image in WPF

33,094

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)

Share:
33,094
Seth
Author by

Seth

Software Engineer currently working on C++ and C# projects.

Updated on September 27, 2020

Comments

  • Seth
    Seth over 3 years

    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?