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
|
using System;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png
namespace CSharpTutorials
{
enum MergeDirection
{
Horizontal = 0,
Vertical = 1
}
class Program
{
static void Main(string[] args)
{
if(args.Lenght<3)
{
Console.WriteLine("Please specify inpur folder with images to merge and ");
Console.WriteLine("wildcard mask of the input images (i.e. *.png or *.jpg) and");
Console.WriteLine("output folder for merge image(s)");
return;
}
License license = new License();
license.SetLicense("Aspose.Imaging.Merge.NET.lic");
// This code merges input images into output one
var images = new List<Image>();
int maxWidth = 0;
int maxHeight = 0;
int totalWidth = 0;
int totalHeight = 0;
string InputDirectory = args[0];
string Mask = args[1];
foreach (var fileName in Directory.GetFiles(InputDirectory, Mask))
{
var image = Image.Load(fileName);
totalWidth += image.Width;
if (image.Width > maxWidth)
{
maxWidth = image.Width;
}
totalHeight += image.Height;
if (image.Height > maxHeight)
{
maxHeight = image.Height;
}
images.Add(image);
}
string OutputDirectory = args[2];
if (!Directory.Exists(OutputDirectory))
{
Directory.CreateDirectory(OutputDirectory);
}
try
{
var outputPath = Path.Combine(OutputDirectory, "merge_horizontal.jpg");
var outputOptions = new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha };
MergeImages(images, MergeDirection.Horizontal, totalWidth, maxHeight, outputPath, outputOptions);
outputPath = Path.Combine(OutputDirectory, "merge_vertical.jpg");
MergeImages(images, MergeDirection.Vertical, totalHeight, maxWidth, outputPath, outputOptions);
}
finally
{
images.ForEach(image => image.Dispose());
}
}
void MergeImages(List<Image> images, MergeDirection direction, int totalSize, int maxSize, string outputPath, ImageOptionsBase outputImageOptions)
{
int targetWidth, targetHeight;
switch (direction)
{
case MergeDirection.Horizontal:
{
targetWidth = totalSize;
targetHeight = maxSize;
break;
}
case MergeDirection.Vertical:
{
targetWidth = maxSize;
targetHeight = totalSize;
break;
}
default:
throw new ArgumentException("Unexpected merge direction");
}
using (Stream stream = new MemoryStream())
{
outputOptions.Source = new StreamSource(stream);
using (var image = Image.Create(outputOptions, targetWidth, targetHeight))
{
image.BackgroundColor = Color.White;
var graphics = new Graphics(image);
float x = 0, y = 0;
images.ForEach(image =>
{
graphics.DrawImage(image, new RectangleF(x, y, image.Width, image.Height));
if (direction == MergeDirection.Horizontal)
{
x += image.Width;
}
if (direction == MergeDirection.Vertical)
{
y += image.Height;
}
});
image.Save(outputPath);
}
}
}
}
}
|