Programmatically set the Source of an Image (XAML)

后端 未结 7 683
孤街浪徒
孤街浪徒 2020-12-09 02:15

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.

相关标签:
7条回答
  • 2020-12-09 02:51

    This is what I use:

    string url = "ms-appx:///Assets/placeHolder.png";
    image.Source = RandomAccessStreamReference.CreateFromUri(new Uri(url));
    
    0 讨论(0)
  • 2020-12-09 02:58

    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.Uri here. Maybe you have a malformed URI or you have to use an absolute URI and use UriKind.Absolute instead?

    0 讨论(0)
  • 2020-12-09 02:58
    <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;
    }
    
    0 讨论(0)
  • 2020-12-09 03:02

    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.

    0 讨论(0)
  • 2020-12-09 03:04

    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.

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-09 03:15

    Well, Windows.Foundation.Uri is documented like this:

    .NET: This type appears as System.Uri.

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

    0 讨论(0)
提交回复
热议问题