c# - 在运行时添加图像

标签 c# wpf image

我正在使用 C# 进行 WPF 应用。 我的文件夹“数据”中有三种图像。 我有 Iamge abd textblock 和一个按钮。 当我按下按钮时,它会在文本 block 中显示文本并且取决于文本,图像可能会有所不同。我如何在运行时添加图像。

 public void Adddata(string lData)
        {          
            Text1.Text = lData; 
            Img1.Source = "data\vista_flag.png";
        }

我知道我编码错误。但我不知道我能做些什么。 Img1.Source = ????????

最佳答案

XAML:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas Name="myCanvas">
    <StackPanel Name="stkPanel">
        <Button Name="btnLoadImage" Click="btnLoadImage_Click" >Load Image</Button>
    </StackPanel>
</Canvas>

C# 按钮点击代码:

 private void btnLoadImage_Click(object sender, RoutedEventArgs e)
    {
        string src = @"C:\Documents and Settings\pdeoghare\My Documents\My Pictures\YourImage.jpg";

        Image img = new Image();

        img.Source = new ImageSourceConverter().ConvertFromString(src) as ImageSource;

        stkPanel.Children.Add(img);
    }

关于c# - 在运行时添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2242329/

相关文章:

c# - 将图像转换为字节数组

Javascript - 将 ISO8601 UTC 时间转换为客户端的本地时间

c# - 无法比较字符串 > 字符串

c# - 依赖注入(inject)(使用 SimpleInjector)和 OAuthAuthorizationServerProvider

c# - 重定向到 Razor 宏错误的自定义错误页面

c# - 通过 SmtpClient 发送的电子邮件不显示图像

c# - WPF 控件中的 SlimDX 视口(viewport)

wpf - 从单独的线程访问 ViewModel 属性

c# - 在 WPF 数据网格中选择项目时如何显示按钮?

html - 如何在 Messenger Chat 中添加图片?