-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageParser.cs
100 lines (79 loc) · 2.66 KB
/
ImageParser.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
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace TPlayer {
/// <summary>
/// Summary description for ImageParser.
/// </summary>
public class ImageParser {
private Size m_imageSize;
public Size ImageSize {
get {
return m_imageSize;
}
}
private int m_imageCount;
public int ImageCount {
get {
return m_imageCount;
}
}
private Image m_image;
// private Color m_transparentColor;
// public Color TransparentColor
// {
// get
// {
// return m_transparentColor;
// }
// }
private ImageAttributes m_imageAtt;
private Rectangle m_destRect = new Rectangle(0, 0, 0, 0);
public ImageParser(Image image, int imagesCount, Size imageSize, Color transparentColor) {
// check validity
int imageWidth = imageSize.Width * imagesCount;
if ((image.Width != imageWidth) || (image.Height != imageSize.Height)) {
throw new ArgumentException("The width or height do not match the image.", "imageSize");
}
if (imagesCount < 1) {
throw new ArgumentException("The count isn't valid.", "imagesCount");
}
m_image = image;
m_imageCount = imagesCount;
m_imageSize = imageSize;
m_imageAtt = new ImageAttributes();
m_imageAtt.SetColorKey(transparentColor, transparentColor);
}
public bool Draw(Graphics grph, int dstX, int dstY, int imageIndex) {
// check index validity
if ((imageIndex < 0) || (imageIndex >= m_imageCount)) {
throw new IndexOutOfRangeException("imageIndex out of range");
}
m_destRect.X = dstX;
m_destRect.Y = dstY;
m_destRect.Width = m_imageSize.Width;
m_destRect.Height = m_imageSize.Height;
int currentImageX = imageIndex * m_imageSize.Width;
grph.DrawImage(m_image, m_destRect, currentImageX, 0, m_imageSize.Width, m_imageSize.Height, GraphicsUnit.Pixel, m_imageAtt);
return true;
}
public bool Draw(Graphics grph, int dstX, int dstY, int dstWidth, int dstHeight, int imageIndex, Color blackColor) {
// check index validity
if ((imageIndex < 0) || (imageIndex >= m_imageCount)) {
throw new IndexOutOfRangeException("imageIndex out of range");
}
m_imageAtt.ClearRemapTable();
System.Drawing.Imaging.ColorMap[] map = new ColorMap[1]{new System.Drawing.Imaging.ColorMap()};
map[0].NewColor = blackColor;
map[0].OldColor = System.Drawing.Color.Black;
m_imageAtt.SetRemapTable(map);
m_destRect.X = dstX;
m_destRect.Y = dstY;
m_destRect.Width = dstWidth;
m_destRect.Height = dstHeight;
int currentImageX = imageIndex * m_imageSize.Width;
grph.DrawImage(m_image, m_destRect, currentImageX, 0, m_imageSize.Width, m_imageSize.Height, GraphicsUnit.Pixel, m_imageAtt);
return true;
}
}
}