2019-12-08 20:59:56 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 ShareX Team
|
2019-12-08 20:59:56 +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 System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.HelpersLib
|
|
|
|
|
{
|
|
|
|
|
public class ImageFilesCache : IDisposable
|
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
private Dictionary<string, Bitmap> images = new Dictionary<string, Bitmap>();
|
2019-12-08 20:59:56 +13:00
|
|
|
|
|
2020-03-22 12:22:43 +13:00
|
|
|
|
public Bitmap GetImage(string filePath)
|
2019-12-08 20:59:56 +13:00
|
|
|
|
{
|
2020-05-13 00:45:32 +12:00
|
|
|
|
Bitmap bmp = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
2019-12-08 20:59:56 +13:00
|
|
|
|
{
|
2020-05-13 00:45:32 +12:00
|
|
|
|
if (images.ContainsKey(filePath))
|
|
|
|
|
{
|
|
|
|
|
return images[filePath];
|
|
|
|
|
}
|
2019-12-08 20:59:56 +13:00
|
|
|
|
|
2020-05-13 00:45:32 +12:00
|
|
|
|
bmp = ImageHelpers.LoadImage(filePath);
|
2019-12-08 20:59:56 +13:00
|
|
|
|
|
2020-05-13 00:45:32 +12:00
|
|
|
|
if (bmp != null)
|
|
|
|
|
{
|
|
|
|
|
images.Add(filePath, bmp);
|
|
|
|
|
}
|
2019-12-08 20:59:56 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 12:22:43 +13:00
|
|
|
|
return bmp;
|
2019-12-08 20:59:56 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 00:45:32 +12:00
|
|
|
|
public Bitmap GetFileIconAsImage(string filePath, bool isSmallIcon = true)
|
2020-05-12 23:55:44 +12:00
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = null;
|
|
|
|
|
|
2020-05-13 00:45:32 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
2020-05-12 23:55:44 +12:00
|
|
|
|
{
|
2020-05-13 00:45:32 +12:00
|
|
|
|
if (images.ContainsKey(filePath))
|
2020-05-12 23:55:44 +12:00
|
|
|
|
{
|
2020-05-13 00:45:32 +12:00
|
|
|
|
return images[filePath];
|
|
|
|
|
}
|
2020-05-12 23:55:44 +12:00
|
|
|
|
|
2020-05-13 00:45:32 +12:00
|
|
|
|
using (Icon icon = NativeMethods.GetFileIcon(filePath, isSmallIcon))
|
|
|
|
|
{
|
|
|
|
|
if (icon != null && icon.Width > 0 && icon.Height > 0)
|
2020-05-12 23:55:44 +12:00
|
|
|
|
{
|
2020-05-13 00:45:32 +12:00
|
|
|
|
bmp = icon.ToBitmap();
|
|
|
|
|
|
|
|
|
|
if (bmp != null)
|
|
|
|
|
{
|
|
|
|
|
images.Add(filePath, bmp);
|
|
|
|
|
}
|
2020-05-12 23:55:44 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 20:59:56 +13:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
if (images != null)
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
|
|
|
|
|
images.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (images != null)
|
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
foreach (Bitmap bmp in images.Values)
|
2019-12-08 20:59:56 +13:00
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
if (bmp != null)
|
2019-12-08 20:59:56 +13:00
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
bmp.Dispose();
|
2019-12-08 20:59:56 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|