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.ImageInternal;
50 this.ImageInternal = value;
52 // Image を直接更新した場合も SetImageFromTask による更新をキャンセルした扱いにする
53 Interlocked.Increment(ref this.currentImageTaskId);
57 private MemoryImage? ImageInternal
59 get => this.memoryImage;
62 this.memoryImage = value;
63 base.Image = value?.Image;
65 this.RestoreSizeMode();
69 private MemoryImage? memoryImage;
71 public bool PlayableMark
73 get => this.playableMark;
76 if (value == this.playableMark)
79 this.playableMark = value;
84 private bool playableMark = false;
87 [DefaultValue(PictureBoxSizeMode.Normal)]
88 public new PictureBoxSizeMode SizeMode
90 get => this.currentSizeMode;
93 this.currentSizeMode = value;
95 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
97 base.SizeMode = value;
103 /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
104 /// 現在の SizeMode を記憶しておくためのフィールド
106 private PictureBoxSizeMode currentSizeMode;
108 public void ShowInitialImage()
110 base.Image = base.InitialImage;
112 // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
113 base.SizeMode = PictureBoxSizeMode.CenterImage;
116 public void ShowErrorImage()
118 base.Image = base.ErrorImage;
120 // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
121 base.SizeMode = PictureBoxSizeMode.CenterImage;
124 private void RestoreSizeMode()
125 => base.SizeMode = this.currentSizeMode;
128 /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
129 /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
131 private int currentImageTaskId = 0;
133 public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask, bool useStatusImage = true)
135 var id = Interlocked.Increment(ref this.currentImageTaskId);
140 this.ShowInitialImage();
142 var image = await Task.Run(imageTask);
144 if (id == this.currentImageTaskId)
145 this.ImageInternal = image;
149 if (id == this.currentImageTaskId && useStatusImage)
150 this.ShowErrorImage();
155 catch (HttpRequestException)
158 catch (InvalidImageException)
161 catch (OperationCanceledException)
164 catch (ObjectDisposedException)
176 protected override void OnPaint(PaintEventArgs pe)
183 this.DrawPlayableMark(pe);
185 catch (ExternalException)
187 // アニメーション GIF 再生中に発生するエラーの対策
188 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
189 this.ShowErrorImage();
193 private void DrawPlayableMark(PaintEventArgs pe)
195 if (!this.PlayableMark)
198 if (this.Image == null)
201 if (base.Image == base.InitialImage || base.Image == base.ErrorImage)
204 var overlayImage = Properties.Resources.PlayableOverlayImage;
206 var overlaySize = Math.Min(this.Width, this.Height) / 4;
207 var destRect = new Rectangle(
208 (this.Width - overlaySize) / 2,
209 (this.Height - overlaySize) / 2,
213 pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
219 public new string? ImageLocation
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 public new void Load(string url)
227 => throw new NotSupportedException();