46

我需要动态设置图像源,请注意我的图像在网络上的某个地方,这是我的代码

BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(@"pack://application:,,,\\myserver\\folder1\\Customer Data\\sample.png");
logo.EndInit(); // Getting the exception here
ImageViewer1.Source = logo;

例外:

无法识别 URI 前缀

4

5 回答 5

77

以上解决方案都不适合我。但这确实:

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));
于 2013-12-09T20:37:30.610 回答
71

你只需要一行:

ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png"));
于 2010-09-24T13:00:45.693 回答
5

您在此处使用的包语法适用于作为资源包含在应用程序中的图像,而不适用于文件系统中的松散文件。

您只需要将实际路径传递给 UriSource:

logo.UriSource = new Uri(@"\\myserver\folder1\Customer Data\sample.png");
于 2010-09-24T12:50:30.660 回答
4

没有一种方法对我有用,因为我需要从文件夹中提取图像而不是将其添加到应用程序中。以下代码有效:

TestImage.Source = GetImage("/Content/Images/test.png")

private static BitmapImage GetImage(string imageUri)
{
    var bitmapImage = new BitmapImage();
    
    bitmapImage.BeginInit();
    bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri,             UriKind.RelativeOrAbsolute);
    bitmapImage.EndInit();
    
    return bitmapImage;
} 
于 2014-11-12T04:09:00.053 回答
-3

你们都错了!为什么?因为您只需要以下代码即可工作:

(图像视图)/ C# Img 是:你的图像框

保持原样,无需更改 ("ms-appx:///) 这是代码而不是您的应用名称 图片是您项目中的文件夹,您可以更改它。dog.png 是您文件夹中的文件,以及我做我的文件夹'Images'和文件'dog.png'所以uri是:“ms-appx:///Images/dog.png”和我的代码:


private void Button_Click(object sender, RoutedEventArgs e)
    {
         img.Source = new BitmapImage(new Uri("ms-appx:///Images/dog.png"));
    }
于 2013-01-30T18:51:49.273 回答