C# 以编程方式设置图像源 (XAML)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11267257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:11:54  来源:igfitidea点击:

Programmatically set the Source of an Image (XAML)

c#xamlwindows-runtime

提问by Villager

I am working on a Windows 8 app. I need to know how to programmatically set the Source of an Image. I assumed that the Silverlight approach would work. However, it doesn't. Does anybody know how to do this? The following will not work:

我正在开发 Windows 8 应用程序。我需要知道如何以编程方式设置图像的来源。我认为 Silverlight 方法会奏效。然而,事实并非如此。有人知道怎么做这个吗?以下将不起作用:

string pictureUrl = GetImageUrl();
Image image = new Image();
image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(pictureUrl, UriKind.Relative));
image.Stretch = Stretch.None;
image.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
image.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;

I get an Exception that says: "The given System.Uri cannot be converted into a Windows.Foundation.Uri."

我收到一个异常,内容为:“给定的 System.Uri 无法转换为 Windows.Foundation.Uri。”

However, I can't seem to find the Windows.Foundation.Uri type.

但是,我似乎找不到 Windows.Foundation.Uri 类型。

采纳答案by Rico Suter

I just tried

我刚试过

Image.Source = new BitmapImage(
    new Uri("http://yourdomain.com/image.jpg", UriKind.Absolute));

And it works without problems... I'm using System.Urihere. Maybe you have a malformed URI or you have to use an absolute URI and use UriKind.Absoluteinstead?

它可以毫无问题地工作......我在System.Uri这里使用。也许您的 URI 格式不正确,或者您必须使用绝对 URI 并UriKind.Absolute改为使用?

回答by Jon Skeet

Well, Windows.Foundation.Uriis documented like this:

好吧,Windows.Foundation.Uri记录如下:

.NET: This type appears as System.Uri.

.NET:此类型显示为 System.Uri。

So the tricky bit isn't converting it into a Windows.Foundation.Uriyourself - it looks like WinRT does that for you. It looks like the problem is with the URI you're using. What is it relative toin this case? I suspect you really just need to find the right format for the URI.

所以棘手的一点不是将它转换成Windows.Foundation.Uri你自己 - 看起来 WinRT 为你做了。问题似乎出在您使用的 URI 上。在这种情况下,它什么有关?我怀疑您真的只需要为 URI 找到正确的格式。

回答by walther

回答by avens19

This is what I use:

这是我使用的:

string url = "ms-appx:///Assets/placeHolder.png";
image.Source = RandomAccessStreamReference.CreateFromUri(new Uri(url));

回答by LZH

check your pictureUrl since it was what resulted in the exception.

检查您的图片网址,因为它是导致异常的原因。

but this should work as well

但这也应该有效

img.Source = new BitmapImage(new Uri(pictureUrl, UriKind.Absolute));

it should have nothing to do with Windows.Foundation.Uri. since winrt will handle it for you.

它应该与 Windows.Foundation.Uri 无关。因为 winrt 会为你处理。

回答by Scott Nimrod

This example uses a FileOpenPicker object to obtain the storage file. You can use whatever method you need to access your file as a StorageFile object.

本示例使用 FileOpenPicker 对象获取存储文件。您可以使用任何您需要的方法来访问您的文件作为 StorageFile 对象。

Logo is the name of the image control.

徽标是图像控件的名称。

Reference the following code:

参考以下代码:

    var fileOpenPicker = new FileOpenPicker();
    fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
    fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    fileOpenPicker.FileTypeFilter.Add(".png");
    fileOpenPicker.FileTypeFilter.Add(".jpg");
    fileOpenPicker.FileTypeFilter.Add(".jpeg");
    fileOpenPicker.FileTypeFilter.Add(".bmp");

    var storageFile = await fileOpenPicker.PickSingleFileAsync();

    if (storageFile != null)
    {
        // Ensure the stream is disposed once the image is loaded
        using (IRandomAccessStream fileStream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = new BitmapImage();

            await bitmapImage.SetSourceAsync(fileStream);
            Logo.Source = bitmapImage;
        }
    }

回答by reza.cse08

<Image Name="Img" Stretch="UniformToFill" />

var file = await KnownFolders.PicturesLibrary.GetFileAsync("2.jpg");
using(var fileStream = (await file.OpenAsync(Windows.Storage.FileAccessMode.Read))){
     var bitImg= new BitmapImage();
     bitImg.SetSource(fileStream); 
     Img.Source = bitImg;
}