1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
25 using System.Collections.Generic;
26 using System.ComponentModel;
31 using System.Net.Http;
32 using System.Runtime.InteropServices;
34 using System.Threading;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using OpenTween.Thumbnail;
41 public class OTPictureBox : PictureBox
44 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
45 public new MemoryImage? Image
47 get => this.memoryImage;
50 this.memoryImage = value;
51 base.Image = value?.Image;
53 this.RestoreSizeMode();
56 private MemoryImage? memoryImage;
59 [DefaultValue(PictureBoxSizeMode.Normal)]
60 public new PictureBoxSizeMode SizeMode
62 get => this.currentSizeMode;
65 this.currentSizeMode = value;
67 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
69 base.SizeMode = value;
75 /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
76 /// 現在の SizeMode を記憶しておくためのフィールド
78 private PictureBoxSizeMode currentSizeMode;
80 public void ShowInitialImage()
82 base.Image = base.InitialImage;
84 // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
85 base.SizeMode = PictureBoxSizeMode.CenterImage;
88 public void ShowErrorImage()
90 base.Image = base.ErrorImage;
92 // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
93 base.SizeMode = PictureBoxSizeMode.CenterImage;
96 private void RestoreSizeMode()
97 => base.SizeMode = this.currentSizeMode;
100 /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
101 /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
103 private int currentImageTaskId = 0;
105 public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
107 var id = Interlocked.Increment(ref this.currentImageTaskId);
111 this.ShowInitialImage();
113 var image = await imageTask();
115 if (id == this.currentImageTaskId)
120 if (id == this.currentImageTaskId)
121 this.ShowErrorImage();
126 catch (HttpRequestException) { }
127 catch (InvalidImageException) { }
128 catch (OperationCanceledException) { }
129 catch (ObjectDisposedException) { }
130 catch (WebException) { }
131 catch (IOException) { }
135 protected override void OnPaint(PaintEventArgs pe)
142 this.DrawPlayableMark(pe);
144 catch (ExternalException)
146 // アニメーション GIF 再生中に発生するエラーの対策
147 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
148 this.ShowErrorImage();
152 private void DrawPlayableMark(PaintEventArgs pe)
154 if (!(this.Tag is ThumbnailInfo thumb && thumb.IsPlayable)) return;
155 if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
157 var overlayImage = Properties.Resources.PlayableOverlayImage;
159 var overlaySize = Math.Min(this.Width, this.Height) / 4;
160 var destRect = new Rectangle(
161 (this.Width - overlaySize) / 2,
162 (this.Height - overlaySize) / 2,
166 pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
170 [EditorBrowsable(EditorBrowsableState.Never)]
171 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
172 public new string? ImageLocation
178 [EditorBrowsable(EditorBrowsableState.Never)]
179 public new void Load(string url)
180 => throw new NotSupportedException();