SizeConversion.cs
4.9 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
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Model
{
public static class ObjConversion
{
private const float MULTIPLE = 100f;
public static SizeF MmToPx(SizeF size)
{
return new SizeF(size.Width / 25.4f * MULTIPLE, size.Height / 25.4f * MULTIPLE);
}
public static RectangleF MmToPx(RectangleF rect)
{
float x = rect.X / 25.4f * MULTIPLE;
float y = rect.Y / 25.4f * MULTIPLE;
float w = rect.Width / 25.4f * MULTIPLE;
float h = rect.Height / 25.4f * MULTIPLE;
return new RectangleF(x, y, w, h);
}
public static RectangleF PxToMm(RectangleF rect)
{
float x = rect.X / MULTIPLE * 25.4f;
float y = rect.Y / MULTIPLE * 25.4f;
float w = rect.Width / MULTIPLE * 25.4f;
float h = rect.Height / MULTIPLE * 25.4f;
return new RectangleF(x, y, w, h);
}
public static SizeF PxToMm(Size size)
{
return new SizeF(Convert.ToSingle(size.Width / MULTIPLE * 25.4f), Convert.ToSingle(size.Height / MULTIPLE * 25.4f));
}
public static PrintLabelFieldType StrToFieldType(string s)
{
PrintLabelFieldType type = (PrintLabelFieldType)Enum.Parse(typeof(PrintLabelFieldType), s);
return type;
}
public static RectangleF StrToRectF(string s)
{
RectangleF rect = new();
string[] arr = s.Split(',');
if (arr.Length == 4)
{
if (float.TryParse(arr[0], out float result))
rect.X = result;
if (float.TryParse(arr[1], out result))
rect.Y = result;
if (float.TryParse(arr[2], out result))
rect.Width = result;
if (float.TryParse(arr[3], out result))
rect.Height = result;
}
return rect;
}
public static string RectFToStr(RectangleF rect)
{
return string.Format("{0},{1},{2},{3}", rect.X, rect.Y, rect.Width, rect.Height);
}
public static Font StrToFont(string s)
{
string[] t = s.Split(',');
float emSize = Convert.ToSingle(t[1]);
FontStyle style = FontStyle.Regular;
if (t[2] == "B") style |= FontStyle.Bold;
if (t[3] == "I") style |= FontStyle.Italic;
Font font = new(t[0], emSize, style);
return font;
}
public static string FontToStr(Font font)
{
string[] s = new string[4];
s[0] = font.Name;
s[1] = font.Size.ToString();
if (font.Bold) s[2] = "B";
if (font.Italic) s[3] = "I";
return string.Join(",", s);
}
public static SizeF StrToSizeF(string s)
{
SizeF size = new();
string[] arr = s.Split(',');
if (arr.Length == 2)
{
if (float.TryParse(arr[0], out float result))
size.Width = result;
if (float.TryParse(arr[1], out result))
size.Height = result;
}
return size;
}
public static string SizeFToStr(SizeF size)
{
return string.Format("{0},{1}", size.Width, size.Height);
}
public static string StrRemoveKey(string s)
{
string rtn = "";
bool into = false;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '[')
into = true;
else if (s[i] == ']')
into = false;
else if (!into)
rtn += s[i];
}
return rtn;
}
public static string[] StrGetKey(string s)
{
string tt = "";
List<string> rtn = new();
bool into = false;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '[')
{
into = true;
tt = "";
}
else if (s[i] == ']')
{
into = false;
rtn.Add(tt);
}
else if (into)
{
tt += s[i];
}
}
return rtn.ToArray();
}
public static Bitmap ReadImageFile(string path)
{
if (!System.IO.File.Exists(path))
return null;
using System.IO.FileStream file = new(path, System.IO.FileMode.Open);
byte[] b = new byte[file.Length];
file.Read(b, 0, b.Length);
System.IO.MemoryStream stream = new(b);
return new Bitmap(Image.FromStream(stream));
}
}
}