#region License Information (GPL v3) /* ShareX - A program that allows you to take screenshots and share any file type Copyright (C) 2008-2014 ShareX Developers This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Optionally you can also view the license at . */ #endregion License Information (GPL v3) using HelpersLib; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Threading.Tasks; namespace ScreenCaptureLib { public class HardDiskCache : ImageCache { public int Count { get { if (indexList != null) { return indexList.Count; } return 0; } } private List indexList; public HardDiskCache(AVIOptions options) { Options = options; indexList = new List(); imageQueue = new BlockingCollection(); StartConsumerThread(); } protected override void StartConsumerThread() { if (!IsWorking) { IsWorking = true; task = TaskEx.Run(() => { Helpers.CreateDirectoryIfNotExist(Options.OutputPath); using (FileStream fsCache = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { while (!imageQueue.IsCompleted) { Image img = null; try { img = imageQueue.Take(); if (img != null) { using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Bmp); long position = fsCache.Position; ms.CopyStreamTo(fsCache); indexList.Add(new LocationInfo(position, fsCache.Length - position)); } } } catch (InvalidOperationException) { } finally { if (img != null) img.Dispose(); } } } IsWorking = false; }); } } public IEnumerable GetImageEnumerator() { if (!IsWorking && File.Exists(Options.OutputPath) && indexList != null && indexList.Count > 0) { using (FileStream fsCache = new FileStream(Options.OutputPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { foreach (LocationInfo index in indexList) { using (MemoryStream ms = new MemoryStream()) { fsCache.CopyStreamTo(ms, (int)index.Location, (int)index.Length); yield return Image.FromStream(ms); } } } } } } }