代码之家  ›  专栏  ›  技术社区  ›  red-X

如何从c_中的相对url获取图像,图像不能在项目中

  •  0
  • red-X  · 技术社区  · 16 年前

    我有一个项目,我从一个xml文件加载相对图像uri。 我正在加载这样的图像:

    if (child.Name == "photo" &&
        child.Attributes["href"] != null &&
        File.Exists(child.Attributes["href"].Value))
    {
        Image image = new Image();
        image.Source = new BitmapImage(new Uri(child.Attributes["href"].Value, UriKind.RelativeOrAbsolute));
        images.Add(image);
    }
    

    其中“child”对象是一个xmlnode,它可能如下所示

    <photo name="info" href="Resources\Content\test.png"/>
    

    在调试过程中,它看到md图像被实际的图像填充,但是当我想以任何方式看到它们时,它什么也不显示。 奇怪的是,当我在项目中包含图像时,它确实起作用,但是我不想这样做,因为我使用XML文件的目的是这样的,因为在更改之后无论如何都必须重建程序,所以它会丢失。

    2 回复  |  直到 16 年前
        1
  •  1
  •   red-X    16 年前

    虽然不是完美的解决方案,但它仍然有效,我正在将相对uri转换为绝对uri,如下所示

    if (child.Name == "photo" &&
        child.Attributes["href"] != null &&
        File.Exists(Environment.CurrentDirectory + child.Attributes["href"].Value))
    {
        Image image = new Image();
        image.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + child.Attributes["href"].Value, UriKind.RelativeOrAbsolute));
        images.Add(image);
    }
    

    只需将XML中的所有uri更改为具有前导斜杠。

        2
  •  0
  •   user131077    16 年前

    嗯可能是这个简单的当前文件夹问题。vs检查project/resources/content/folder下的资源和project/bin/debug/resources/content/folder下的程序检查资源。