1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 spx (@5px)
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.
26 using System.Threading;
30 public interface IMediaItem
45 string Extension { get; }
48 /// メディアが存在するかどうかを示す真偽値
58 /// 表示用の MemoryImage を作成する
63 MemoryImage CreateImage();
66 /// メディアの内容を読み込むための Stream を開く
74 /// メディアの内容を Stream へ書き込む
76 void CopyTo(Stream stream);
80 /// ファイル用の MediaItem クラス
82 public class FileMediaItem : IMediaItem
84 private readonly FileInfo _fileInfo;
86 public FileMediaItem(string path)
88 this._fileInfo = new FileInfo(path);
91 public FileMediaItem(FileInfo fileInfo)
92 : this(fileInfo.FullName)
98 get { return this._fileInfo.FullName; }
103 get { return this._fileInfo.Name; }
106 public string Extension
108 get { return this._fileInfo.Extension; }
113 get { return this._fileInfo.Exists; }
118 get { return this._fileInfo.Length; }
121 public MemoryImage CreateImage()
123 using (var fs = this._fileInfo.OpenRead())
125 return MemoryImage.CopyFromStream(fs);
129 public Stream OpenRead()
131 return this._fileInfo.OpenRead();
134 public void CopyTo(Stream stream)
136 using (var fs = this._fileInfo.OpenRead())
142 public FileInfo FileInfo
144 get { return this._fileInfo; }
149 /// MemoryImage 用の MediaItem クラス
152 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
154 public class MemoryImageMediaItem : IMediaItem, IDisposable
156 public const string PathPrefix = "<>MemoryImage://";
157 private static int _fileNumber = 0;
159 private bool _disposed = false;
161 private readonly string _path;
162 private readonly MemoryImage _image;
164 public MemoryImageMediaItem(Image image)
167 throw new ArgumentNullException("image");
169 // image から png 形式の MemoryImage を生成
170 this._image = MemoryImage.CopyFromImage(image);
172 var num = Interlocked.Increment(ref _fileNumber);
173 this._path = PathPrefix + num + this._image.ImageFormatExt;
178 get { return this._path; }
183 get { return this._path.Substring(PathPrefix.Length); }
186 public string Extension
188 get { return this._image.ImageFormatExt; }
193 get { return this._image != null; }
198 get { return this._image.Stream.Length; }
201 public MemoryImage CreateImage()
203 return this._image.Clone();
206 public Stream OpenRead()
209 var memstream = new MemoryStream();
211 this._image.Stream.WriteTo(memstream);
212 memstream.Seek(0, SeekOrigin.Begin);
217 public void CopyTo(Stream stream)
219 this._image.Stream.WriteTo(stream);
222 protected virtual void Dispose(bool disposing)
224 if (this._disposed) return;
228 this._image.Dispose();
231 this._disposed = true;
234 public void Dispose()
238 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
239 GC.SuppressFinalize(this);
242 ~MemoryImageMediaItem()