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.
23 using System.Collections.Generic;
25 using System.Runtime.InteropServices;
27 using System.Windows.Forms;
28 using System.ComponentModel;
30 using System.Threading.Tasks;
31 using System.Threading;
33 using System.Net.Http;
35 using OpenTween.Thumbnail;
39 public class OTPictureBox : PictureBox
42 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
43 public new MemoryImage Image
45 get { return this.memoryImage; }
48 this.memoryImage = value;
49 base.Image = value != null ? value.Image : null;
51 this.RestoreSizeMode();
54 private MemoryImage memoryImage;
57 [DefaultValue(PictureBoxSizeMode.Normal)]
58 public new PictureBoxSizeMode SizeMode
60 get { return this.currentSizeMode; }
63 this.currentSizeMode = value;
65 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
67 base.SizeMode = value;
73 /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
74 /// 現在の SizeMode を記憶しておくためのフィールド
76 private PictureBoxSizeMode currentSizeMode;
78 public void ShowInitialImage()
80 base.Image = base.InitialImage;
82 // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
83 base.SizeMode = PictureBoxSizeMode.CenterImage;
86 public void ShowErrorImage()
88 base.Image = base.ErrorImage;
90 // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
91 base.SizeMode = PictureBoxSizeMode.CenterImage;
94 private void RestoreSizeMode()
96 base.SizeMode = this.currentSizeMode;
99 public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
103 this.ShowInitialImage();
104 this.Image = await imageTask();
108 this.ShowErrorImage();
113 catch (HttpRequestException) { }
114 catch (InvalidImageException) { }
115 catch (OperationCanceledException) { }
116 catch (WebException) { }
120 protected override void OnPaint(PaintEventArgs pe)
127 DrawPlayableMark(pe);
129 catch (ExternalException)
131 // アニメーション GIF 再生中に発生するエラーの対策
132 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
133 this.ShowErrorImage();
137 private void DrawPlayableMark(PaintEventArgs pe)
139 var thumb = this.Tag as ThumbnailInfo;
140 if (thumb == null || !thumb.IsPlayable) return;
141 if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
143 var overlayImage = Properties.Resources.PlayableOverlayImage;
145 var overlaySize = Math.Min(this.Width, this.Height) / 4;
146 var destRect = new Rectangle(
147 (this.Width - overlaySize) / 2,
148 (this.Height - overlaySize) / 2,
152 pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
158 public new string ImageLocation
160 get { throw new NotSupportedException(); }
161 set { throw new NotSupportedException(); }
164 [EditorBrowsable(EditorBrowsableState.Never)]
165 public new void Load(string url)
167 throw new NotSupportedException();