简体   繁体   中英

Show the image by string in WPF C# apps

I have images as Resources in Resources Folder

I want to show image by string value in ImageController

Here is the code What I am going to use.

<UserControl x:Class="WinIPTVApp.Pages.Content.Views.MenuItemUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WinIPTVApp.Pages.Content.Views"
         mc:Ignorable="d">
    <Grid>
        <Image Source="" x:Name="Icon"/>
        <ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15"
                 Foreground="White" />
    </Grid>
</UserControl>

This is the codes for xaml.cs.

Image Icon;
string user_icon = "user.png";
Icon = new Bitmap(new Uri(user_icon));

Please help me how to solve this problem. Thank you in advance.

I just found answer and this is working well.

Icon.Source = new BitmapImage(new Uri($"pack://application:,,,/Resources/{user_icon}"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM