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 を作成する
60 MemoryImage CreateImage();
63 /// メディアの内容を Stream へ書き込む
65 void CopyTo(Stream stream);
69 /// ファイル用の MediaItem クラス
71 public class FileMediaItem : IMediaItem
73 private readonly FileInfo _fileInfo;
75 public FileMediaItem(string path)
77 this._fileInfo = new FileInfo(path);
80 public FileMediaItem(FileInfo fileInfo)
81 : this(fileInfo.FullName)
87 get { return this._fileInfo.FullName; }
92 get { return this._fileInfo.Name; }
95 public string Extension
97 get { return this._fileInfo.Extension; }
102 get { return this._fileInfo.Exists; }
107 get { return this._fileInfo.Length; }
110 public MemoryImage CreateImage()
112 using (var fs = this._fileInfo.OpenRead())
114 return MemoryImage.CopyFromStream(fs);
118 public void CopyTo(Stream stream)
120 using (var fs = this._fileInfo.OpenRead())
126 public FileInfo FileInfo
128 get { return this._fileInfo; }
133 /// MemoryImage 用の MediaItem クラス
136 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
138 public class MemoryImageMediaItem : IMediaItem, IDisposable
140 public const string PathPrefix = "<>MemoryImage://";
141 private static int _fileNumber = 0;
143 private bool _disposed = false;
145 private readonly string _path;
146 private readonly MemoryImage _image;
148 public MemoryImageMediaItem(Image image)
151 throw new ArgumentNullException("image");
153 // image から png 形式の MemoryImage を生成
154 using (var bitmap = new Bitmap(image))
156 this._image = MemoryImage.CopyFromBitmap(bitmap);
159 var num = Interlocked.Increment(ref _fileNumber);
160 this._path = PathPrefix + num + this._image.ImageFormatExt;
165 get { return this._path; }
170 get { return this._path.Substring(PathPrefix.Length); }
173 public string Extension
175 get { return this._image.ImageFormatExt; }
180 get { return this._image != null; }
185 get { return this._image.Stream.Length; }
188 public MemoryImage CreateImage()
190 return this._image.Clone();
193 public void CopyTo(Stream stream)
195 this._image.Stream.Seek(0, SeekOrigin.Begin);
196 this._image.Stream.CopyTo(stream);
199 protected virtual void Dispose(bool disposing)
201 if (this._disposed) return;
205 this._image.Dispose();
208 this._disposed = true;
211 public void Dispose()
215 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
216 GC.SuppressFinalize(this);
219 ~MemoryImageMediaItem()