开发者

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

开发者 https://www.devze.com 2023-04-07 14:38 出处:网络
This question already has answers here: 开发者_StackOverflow中文版Image UriSource and Data Binding
This question already has answers here: 开发者_StackOverflow中文版 Image UriSource and Data Binding (6 answers) Closed 3 years ago.

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?


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消