-
Notifications
You must be signed in to change notification settings - Fork 0
/
WBVerticalBannerImage.cs
182 lines (152 loc) · 5.56 KB
/
WBVerticalBannerImage.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using DDS;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WBBannerConverter
{
/// <summary>
/// banners_a.dds, banners_d.dds, banners_e.dds, banners_f.dds and banners_g.dds are same pattern
/// but banners_b.dds and banners_c.dds are very messed pattern which are very different from the banners_a.dds, so we need to handle them directly
/// or you need to adjust your banners file manually to suit the banners_b.dds and banners_c.dds
/// </summary>
public class WBVerticalBannerImage
{
private int[] BANNER_MODE_B_X_STRIDE_ARR = new int[] { 2, 4, 1, 3, 1, 2, };
private int BANNER_MOD_B_Y_STRIDE = 4;
private int[] BANNER_MODE_C_STRIDE_ARR = new int[] { 3, 4, };
protected Bitmap image;
protected Bitmap verticalBannerImage;
protected List<Bitmap> veriticalBannerImages;
protected const int SINGLE_VERTICAL_BANNER_WIDTH = 140;
protected const int SINGLE_VERTICAL_BANNER_HEIGHT = 341;
private const int SINGLE_VERTICAL_BANNER_B_WIDTH = 135;
private const int SINGLE_VERTICAL_BANNER_B_HEIGHT = 348;
private const int SINGLE_VERTICAL_BANNER_C_WIDTH = 134;
private const int SINGLE_VERTICAL_BANNER_C_HEIGHT = 349;
public int BannerMode { get; set; }
private DDSImage ddsImage;
public WBVerticalBannerImage(string bannerImageFileName)
{
image = new Bitmap(bannerImageFileName);
veriticalBannerImages = new List<Bitmap>();
splitImageIntoSingleBanner();
}
public WBVerticalBannerImage(DDSImage ddsImage)
{
this.ddsImage = ddsImage;
image = new Bitmap(ddsImage.Images[0]);
veriticalBannerImages = new List<Bitmap>();
splitImageIntoSingleBanner();
}
protected virtual void splitImageIntoSingleBanner()
{
int index = 0;
Rectangle drawRect = new Rectangle(0, 0, SINGLE_VERTICAL_BANNER_WIDTH, SINGLE_VERTICAL_BANNER_HEIGHT);
for (int i = 0; i < 3; i++) //Warband banner_*.dds has 21 banners in one dds image
{
for (int j = 0; j < 7; j++)
{
var rect = new Rectangle(j * SINGLE_VERTICAL_BANNER_WIDTH, i * SINGLE_VERTICAL_BANNER_HEIGHT, SINGLE_VERTICAL_BANNER_WIDTH, SINGLE_VERTICAL_BANNER_HEIGHT);
Bitmap newImage = new Bitmap(SINGLE_VERTICAL_BANNER_WIDTH, SINGLE_VERTICAL_BANNER_HEIGHT, image.PixelFormat);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(image, drawRect, rect, GraphicsUnit.Pixel);
}
index++;
veriticalBannerImages.Add(newImage);
}
}
}
public void ConvertToStandardHoritonzalBanner(string outputFilePath)
{
WBStandardHorizontalBannerImage standardizedBannerImage =
new WBStandardHorizontalBannerImage(veriticalBannerImages, ddsImage);
standardizedBannerImage.Save(outputFilePath);
}
#region Std To WB
public WBVerticalBannerImage(List<Bitmap> standardHorizontalBannerImages, DDSImage ddsImage)
{
this.ddsImage = ddsImage;
veriticalBannerImages = new List<Bitmap>();
rescaleToVerticalBannerImages(standardHorizontalBannerImages);
}
private void rescaleToVerticalBannerImages(List<Bitmap> standardHorizontalBannerImages)
{
for (int i = 0; i < standardHorizontalBannerImages.Count; i++)
{
Bitmap standardizedSingleBannerImage = null;
using (Bitmap bitmap = new Bitmap(
SINGLE_VERTICAL_BANNER_WIDTH,
SINGLE_VERTICAL_BANNER_HEIGHT,
standardHorizontalBannerImages[i].PixelFormat))
{
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(standardHorizontalBannerImages[i], new Rectangle(0, 0, SINGLE_VERTICAL_BANNER_WIDTH, SINGLE_VERTICAL_BANNER_HEIGHT));
}
standardizedSingleBannerImage = new Bitmap(bitmap);
}
veriticalBannerImages.Add(standardizedSingleBannerImage);
}
}
protected virtual Bitmap generateVerticalBannerImage()
{
string bannerTemplateFile = Environment.CurrentDirectory + "//Template//wb_banners_template.png";
Bitmap wbBannerImage = new Bitmap(Image.FromFile(bannerTemplateFile));
int col = 0;
int row = 0;
int x = 0;
int y = 0;
using (var g = Graphics.FromImage(wbBannerImage))
{
Rectangle rect = new Rectangle(x, y,
SINGLE_VERTICAL_BANNER_WIDTH,
SINGLE_VERTICAL_BANNER_HEIGHT);
for (int i = 0; i < veriticalBannerImages.Count; i++)
{
if (i == 0)
{
g.DrawImage(veriticalBannerImages[i], rect);
}
else
{
int ret = i % 7;
if (ret != 0)
{
x = col * SINGLE_VERTICAL_BANNER_WIDTH;
y = row * SINGLE_VERTICAL_BANNER_HEIGHT;
}
else
{
row++;
col = 0;
x = 0;
y = row * SINGLE_VERTICAL_BANNER_HEIGHT;
}
rect.X = x;
rect.Y = y;
g.DrawImage(veriticalBannerImages[i], rect);
}
col++;
}
}
return wbBannerImage;
}
public void SaveToDDS(string outputFilePath)
{
verticalBannerImage = generateVerticalBannerImage();
DDSImage.Save(verticalBannerImage, outputFilePath, ddsImage.Format);
}
public void Save(string outputFilePath)
{
verticalBannerImage = generateVerticalBannerImage();
verticalBannerImage.Save(outputFilePath);
image.Dispose();
}
#endregion
}
}