private async Task SetUserImageAsync(string imageUri, CancellationToken cancellationToken) { var oldImage = this.UserPicture.Image; this.UserPicture.Image = null; oldImage?.Dispose(); // ProfileImageUrlHttps が null になる場合があるらしい // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=33871 if (imageUri == null) { return; } await this.UserPicture.SetImageFromTask(async() => { var uri = imageUri.Replace("_normal", "_bigger"); using (var imageStream = await Networking.Http.GetStreamAsync(uri).ConfigureAwait(false)) { var image = await MemoryImage.CopyFromStreamAsync(imageStream) .ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { image.Dispose(); throw new OperationCanceledException(cancellationToken); } return(image); } }); }
private async Task LoadUserIconAsync(Uri imageUri, long userId) { var oldImage = this.UserIcon.Image; this.UserIcon.Image = null; oldImage?.Dispose(); await this.UserIcon.SetImageFromTask(async() => { var uri = imageUri.AbsoluteUri.Replace("_normal", "_bigger"); using (var imageStream = await Networking.Http.GetStreamAsync(uri)) { var image = await MemoryImage.CopyFromStreamAsync(imageStream); // 画像の読み込み中に選択中のユーザーが変化していたらキャンセルとして扱う var selectedUser = (UserInfo)this.UserList.SelectedItem; if (selectedUser.Id != userId) { image.Dispose(); throw new OperationCanceledException(); } return(image); } }); }
public async Task ImageFormat_GifTest() { using (var imgStream = File.OpenRead("Resources/re.gif")) using (var image = await MemoryImage.CopyFromStreamAsync(imgStream).ConfigureAwait(false)) { Assert.Equal(ImageFormat.Gif, image.ImageFormat); Assert.Equal(".gif", image.ImageFormatExt); } }
private async Task <MemoryImage> FetchImageAsync(string uri, CancellationToken cancelToken) { using (var response = await Networking.Http.GetAsync(uri, cancelToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); using (var imageStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { return(await MemoryImage.CopyFromStreamAsync(imageStream) .ConfigureAwait(false)); } } }
public async Task Equals_Test() { using (var imgStream1 = File.OpenRead("Resources/re.gif")) using (var image1 = await MemoryImage.CopyFromStreamAsync(imgStream1).ConfigureAwait(false)) { using (var imgStream2 = File.OpenRead("Resources/re.gif")) using (var image2 = await MemoryImage.CopyFromStreamAsync(imgStream2).ConfigureAwait(false)) { Assert.True(image1.Equals(image2)); Assert.True(image2.Equals(image1)); } using (var image3 = TestUtils.CreateDummyImage()) { Assert.False(image1.Equals(image3)); Assert.False(image3.Equals(image1)); } } }
/// <summary> /// MemoryImage インスタンスを非同期に複製します /// </summary> /// <remarks> /// メソッド実行中にストリームのシークが行われないよう注意して下さい。 /// 特に PictureBox で Gif アニメーションを表示している場合は Enabled に false をセットするなどして更新を止めて下さい。 /// </remarks> /// <returns>複製された MemoryImage を返すタスク</returns> public Task <MemoryImage> CloneAsync() { this.Stream.Seek(0, SeekOrigin.Begin); return(MemoryImage.CopyFromStreamAsync(this.Stream)); }