-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageFilesHelper.cs
78 lines (66 loc) · 2.42 KB
/
ImageFilesHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Utilities.Images
{
public static class ImageFilesHelper
{
public static List<ImageFormat> ImageFormats =>
typeof(ImageFormat).GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(p => (ImageFormat)p.GetValue(null, null)).ToList();
public static ImageFormat ImageFormatFromRawFormat(ImageFormat raw) =>
ImageFormats.FirstOrDefault(f => raw.Equals(f)) ?? ImageFormat.Bmp;
public static ImageFormat ImageFormatFromGuid(Guid id) =>
ImageFormats.FirstOrDefault(f => id.Equals(f.Guid)) ?? ImageFormat.Bmp;
public static List<string> ImageExtensions =>
ImageCodecInfo.GetImageDecoders()
.SelectMany(d => d.FilenameExtension.Split(';').Select(x => x.Substring(1).Trim().ToLower())).ToList();
public static List<string> GetImageFiles(string path, SearchOption searchOption = SearchOption.TopDirectoryOnly, params string[] additionalExtensions) =>
Directory.GetFiles(path, "*.*", searchOption)
.Join(ImageExtensions.Concat(additionalExtensions), f => Path.GetExtension(f), e => e, (f, e) => f, StringComparer.OrdinalIgnoreCase).ToList();
public static Image CropImage(Image image, Rectangle cropArea)
{
using (var bmp = new Bitmap(image))
return bmp.Clone(cropArea, image.PixelFormat);
}
public static Image ResizeImage(Image original, decimal? percent) =>
ResizeImage(original, percent, default(Size), false, false);
public static Image ResizeImage(Image original, Size size, bool absolute = false, bool lesser = false) =>
ResizeImage(original, null, size, absolute, lesser);
private static Image ResizeImage(Image original, decimal? percent, Size size, bool absolute, bool lesser)
{
using (var bmpOriginal = new Bitmap(original))
{
int h = bmpOriginal.Height;
int w = bmpOriginal.Width;
if (percent.HasValue)
{
decimal p = percent.Value / 100m;
h = (int)(h * p);
w = (int)(w * p);
}
else
{
double wr = size.Width / (double)w;
double hr = size.Height / (double)h;
if (wr == hr || absolute)
{
h = size.Height;
w = size.Width;
}
else
{
var r = lesser ? Math.Min(wr, hr) : Math.Max(wr, hr);
h = (int)(h * r);
w = (int)(w * r);
}
}
return new Bitmap(bmpOriginal, new Size(w, h));
}
}
}
}