forked from JimBobSquarePants/ImageProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (150 loc) · 6.96 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (150 loc) · 6.96 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// The program.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.PlayGround
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using ImageProcessor;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Imaging.Formats;
/// <summary>
/// The program.
/// </summary>
public class Program
{
/// <summary>
/// The main routine.
/// </summary>
/// <param name="args">
/// The args.
/// </param>
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute", Justification = "It works ok?")]
public static void Main(string[] args)
{
const string InputImages = @"..\..\..\ImageProcessor.UnitTests\Images\";
const string OutputImages = @"..\..\images\output";
string root = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
string inPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(root), InputImages));
string outPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(root), OutputImages));
DirectoryInfo di = new DirectoryInfo(inPath);
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jpg", ".jpeg", ".jfif", ".gif", ".bmp", ".png", ".tif");
//IEnumerable<FileInfo> files = GetFilesByName(di, "Test.jpg");
foreach (FileInfo fileInfo in files)
{
// Start timing.
byte[] photoBytes = File.ReadAllBytes(fileInfo.FullName);
Console.WriteLine("Processing: " + fileInfo.Name);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (MemoryStream inStream = new MemoryStream(photoBytes))
using (ImageFactory imageFactory = new ImageFactory() { AnimationProcessMode = AnimationProcessMode.All })
{
try
{
imageFactory.Load(inStream)
//.Watermark(new TextLayer
//{
// Text = "Vertical",
// Vertical = true,
// FontColor = Color.Black,
// Opacity = 35,
// FontSize = imageFactory.Image.Height / 20,
// DropShadow = true,
// Position = new Point(30, 30)
//})
.Resize(new Size(imageFactory.Image.Width / 2, imageFactory.Image.Height / 2))
.Save(Path.GetFullPath(Path.Combine(outPath, "Vertical" + fileInfo.Name)));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// stopwatch.Stop();
}
using (MemoryStream inStream = new MemoryStream(photoBytes))
using (ImageFactory imageFactory = new ImageFactory() { AnimationProcessMode = AnimationProcessMode.All })
{
try
{
imageFactory.Load(inStream)
.Watermark(
new TextLayer
{
Text = "NoVertical",
Vertical = false,
FontColor = Color.Black,
Opacity = 35,
FontSize = imageFactory.Image.Height / 20,
DropShadow = true,
Position = new Point(30, 30)
})
.Save(Path.GetFullPath(Path.Combine(outPath, "NoVertical" + fileInfo.Name)));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
stopwatch.Stop();
}
// Report back.
long peakWorkingSet64 = Process.GetCurrentProcess().PeakWorkingSet64;
float mB = peakWorkingSet64 / (float)1024 / 1024;
Console.WriteLine(@"Completed {0} in {1:s\.fff} secs {2}Peak memory usage was {3} bytes or {4} Mb.", fileInfo.Name, stopwatch.Elapsed, Environment.NewLine, peakWorkingSet64.ToString("#,#"), mB);
}
Console.ReadLine();
}
/// <summary>
/// Gets all files in a given directory by extension.
/// </summary>
/// <param name="dir">The directory to search within.</param>
/// <param name="extensions">The file extensions.</param>
/// <returns>
/// The <see cref="IEnumerable{FileInfo}"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown if no extensions are given.
/// </exception>
public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
if (extensions == null)
{
throw new ArgumentNullException(nameof(extensions));
}
IEnumerable<FileInfo> files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories);
return files.Where(f => extensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase));
}
/// <summary>
/// Gets all files in a given directory by name.
/// </summary>
/// <param name="dir">The directory to search within.</param>
/// <param name="name">The file extensions.</param>
/// <returns>
/// The <see cref="IEnumerable{FileInfo}"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown if no extensions are given.
/// </exception>
public static IEnumerable<FileInfo> GetFilesByName(DirectoryInfo dir, params string[] name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
IEnumerable<FileInfo> files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories);
return files.Where(f => name.Contains(f.Name, StringComparer.OrdinalIgnoreCase));
}
}
}