ShareX/ScreenCaptureLib/HardDiskCache.cs

126 lines
4.2 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2014-01-12 20:25:51 +13:00
Copyright (C) 2008-2014 ShareX Developers
2013-11-03 23:53:49 +13:00
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 <http://www.gnu.org/licenses/>.
*/
#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
2013-11-03 23:53:49 +13:00
{
public class HardDiskCache : ImageCache
2013-11-03 23:53:49 +13:00
{
public int Count
{
get
{
if (indexList != null)
{
return indexList.Count;
}
return 0;
}
}
private List<LocationInfo> indexList;
public HardDiskCache(AVIOptions options)
2013-11-03 23:53:49 +13:00
{
Options = options;
2013-11-03 23:53:49 +13:00
indexList = new List<LocationInfo>();
imageQueue = new BlockingCollection<Image>();
StartConsumerThread();
}
protected override void StartConsumerThread()
2013-11-03 23:53:49 +13:00
{
if (!IsWorking)
2013-11-03 23:53:49 +13:00
{
IsWorking = true;
2013-11-03 23:53:49 +13:00
task = TaskEx.Run(() =>
{
Helpers.CreateDirectoryIfNotExist(Options.OutputPath);
2013-11-03 23:53:49 +13:00
using (FileStream fsCache = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
2013-11-03 23:53:49 +13:00
{
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;
2013-11-03 23:53:49 +13:00
});
}
}
public IEnumerable<Image> GetImageEnumerator()
{
if (!IsWorking && File.Exists(Options.OutputPath) && indexList != null && indexList.Count > 0)
2013-11-03 23:53:49 +13:00
{
using (FileStream fsCache = new FileStream(Options.OutputPath, FileMode.Open, FileAccess.Read, FileShare.Read))
2013-11-03 23:53:49 +13:00
{
foreach (LocationInfo index in indexList)
{
using (MemoryStream ms = new MemoryStream())
{
fsCache.CopyStreamTo(ms, (int)index.Location, (int)index.Length);
yield return Image.FromStream(ms);
}
}
}
}
}
}
}