【问题标题】:Programmatically set the Source of an Image (XAML)以编程方式设置图像的源 (XAML)
【发布时间】:2012-06-29 18:40:31
【问题描述】:

我正在开发一个 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;

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

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

【问题讨论】:

    标签: c# xaml windows-runtime


    【解决方案1】:

    我刚试过

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

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

    【讨论】:

    • 我也遇到了异常?
    【解决方案2】:

    这是我用的:

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

    【讨论】:

    • placeHolder.png 的构建操作是什么。我已将其设置为“内容”,但无法正确加载图像。
    • 我收到一个错误,RandomAccessStreamReference 无法转换为 ImageSource。
    • Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/placeHolder.png", UriKind.Absolute));
    【解决方案3】:

    嗯,Windows.Foundation.Uri 是这样记录的:

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

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

    【讨论】:

      【解决方案4】:

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

      Logo 是图像控件的名称。

      参考以下代码:

          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;
              }
          }
      

      【讨论】:

        【解决方案5】:

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

        但这应该也可以

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

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

        【讨论】:

          【解决方案6】:

          试试这个格式:

          ms-appx:/Images/800x600/BackgroundTile.bmp
          

          The given System.Uri cannot be converted into a Windows.Foundation.Uri

          【讨论】:

            【解决方案7】:
            <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;
            }
            

            【讨论】:

              猜你喜欢
              • 2011-09-24
              • 1970-01-01
              • 1970-01-01
              • 2017-02-26
              • 2013-09-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多