【问题标题】:WPF Adding images to canvas without them being added as resourcesWPF 将图像添加到画布而不将它们添加为资源
【发布时间】:2014-10-06 20:22:09
【问题描述】:

我正在尝试将图像添加到我的 WPF 应用程序画布中。 据我了解,它们需要在 VS 解决方案中作为资源引用。 但是,我需要能够将图像复制到文件夹中,并从 XML 文件中解析图像的相对 Uri,并将图像加载到画布中:

    Image image = new Image();
    var pic = new BitmapImage();
    pic.BeginInit();
    pic.UriSource = new Uri(url, UriKind.Relative); // url is from the xml
    pic.EndInit();
    image.Source = pic;
    LayoutRoot.Children.Add(image); //since the image is not in VS marked as Resource,
    // nothing shows up

感谢您的善意建议

【问题讨论】:

    标签: c# .net wpf canvas


    【解决方案1】:

    如果您指定 URI 的完整路径而不是使用 UriKind.Relative uri,它将正常工作:

    pic.BeginInit();
    pic.UriSource = new Uri(@"C:\Path\To\File.jpg");
    pic.EndInit();
    

    【讨论】:

    • 那部分呢:“从 XML 文件中解析图像的 relative Uri”?
    • @alterfox 只需使用Path.Combine 来构建完整的 URL - OP 没有显示 URL 生成,所以我不能包含它...
    • 感谢您的解决方案
    猜你喜欢
    • 2017-10-10
    • 2015-05-19
    • 2020-07-02
    • 1970-01-01
    • 2018-07-16
    • 2011-10-20
    • 2020-04-30
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多