none
windows universal app中如何获取Image控件中显示图片的流? RRS feed

  • 问题

  • 我做的是windows universal app,页面上Image空间显示的图片是网络图片,图片显示后,我想获取Image控件中显示图片的流进行操作,请问应该如何获取?
    2014年10月9日 2:22

答案

  • public static async Task<bool> PhotoDownloadAsync(MyMedia media, string folderName, string fileName)
            {
                try
                {
                    Uri uri = new Uri(media.Url);
                    var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName,
                        CreationCollisionOption.OpenIfExists);
    
                    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    
                    RandomAccessStreamReference stream = RandomAccessStreamReference.CreateFromUri(uri);
    
                    using (IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        Debug.WriteLine("{0} stream open at {1}", media.Url,
                                    DateTime.Now.ToString("HH:mm:ss fff"));
                        Windows.Storage.Streams.Buffer buffer;
                                          using (IRandomAccessStream originalImageStream = await stream.OpenReadAsync())
                                                                    buffer = new Windows.Storage.Streams.Buffer((uint) originalImageStream.Size);
                            await
                                originalImageStream.ReadAsync(buffer, (uint) originalImageStream.Size,
                                    InputStreamOptions.None);
                        }
                        
                        await randomAccessStream.WriteAsync(buffer);
                        await randomAccessStream.FlushAsync();
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }


    爱编程 爱生活

    2014年10月9日 5:54