Display.cs
23.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace LEDhelper
{
public class Display
{
public enum ColorType
{
SINGLE,
DOUBLE,
THREE,
TRUECOLOR
};
public enum MatrixType
{
RG,
RGGR
}
public bool Horizontal { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Font DisplayFont { get; set; }
public Color ForeColor { get; set; }
public IList<string> Split(string content, int engFontWidth = 8)
{
List<string> result = new List<string>();
int start = 0;
int length = 1;
int width = 0;
while ((start + length) <= content.Length)
{
width += System.Text.Encoding.Default.GetBytes(content.Substring(start, 1)).Length == 1 ? engFontWidth : engFontWidth * 2;
string text = content.Substring(start, length);
if (width > Width)
{
width = 0;
if (length == 1)
{
result.Add(text);
start++;
}
else
{
result.Add(content.Substring(start, length - 1));
start += (length - 1);
length = 1;
}
}
else
{
length++;
}
}
if (start < content.Length)
{
result.Add(content.Substring(start, length == 1 ? 1 : length - 1));
}
return result;
}
public int CalculateEngCharCount(string content)
{
int count = 0;
for (int i = 0; i < content.Length; i++)
{
count += System.Text.Encoding.Default.GetBytes(content.Substring(i, 1)).Length > 1 ? 2 : 1;
}
return count;
}
public IList<string> SplitByGraphic(string content)
{
List<string> result = new List<string>();
Bitmap textBitmap = new Bitmap(Width, Height);
Graphics textG = Graphics.FromImage(textBitmap);
textG.FillRectangle(new SolidBrush(Color.Black), 0, 0, textBitmap.Width, textBitmap.Height);
Font textFont = DisplayFont;
int start = 0;
int length = 1;
while ((start + length) <= content.Length)
{
string text = content.Substring(start, length);
SizeF textSize = textG.MeasureString(text, textFont);
if (textSize.Width > Width)
{
if (length == 1)
{
result.Add(text);
start++;
}
else
{
result.Add(content.Substring(start, length - 1));
start += (length - 1);
length = 1;
}
}
else
{
length++;
}
}
result.Add(content.Substring(start, length - 1));
return result;
}
/// <summary>
/// 总的像素bit位点阵,一定是8的倍数
/// </summary>
/// <param name="content">每页的字模数据</param>
/// <param name="colorType"></param>
/// <param name="matrixType"></param>
/// <returns></returns>
public List<byte> ToBytes(Bitmap content, ColorType colorType, MatrixType matrixType)
{
List<byte> result = new List<byte>();
int height = content.Height;
int width = content.Width;
if (colorType == ColorType.SINGLE) // 单色
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x += 8)
{
byte bits = 0x00;
for (int i = 0; i < 8; i++)
{
Color rgb = content.GetPixel(x + i, y);
int r = rgb.R;
int g = rgb.G;
bool flag = r >= 127 | g >= 127;
bits += (byte)((flag ? 0 : 1) << (7 - i));
}
result.Add(bits);
}
}
}
else if (colorType == ColorType.DOUBLE) // 双色
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x += 8)
{
byte Rbits = 0x00;
byte Gbits = 0x00;
for (int i = 0; i < 8; i++)
{
Color rgb = content.GetPixel(x + i, y);
int r = rgb.R;
int g = rgb.G;
Rbits += (byte)((r >= 127 ? 0 : 1) << (7 - i));
Gbits += (byte)((g >= 127 ? 0 : 1) << (7 - i));
}
if (matrixType == MatrixType.RG)//r+g
{
result.Add(Rbits);
result.Add(Gbits);
}
else//g+r,r+g的屏幕反过来就是g+r
{
result.Add(Gbits);
result.Add(Rbits);
}
}
}
}
else if (colorType == ColorType.THREE) // 双色
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x += 8)
{
byte Rbits = 0x00;
byte Gbits = 0x00;
byte Bbits = 0x00;
for (int i = 0; i < 8; i++)
{
Color rgb = content.GetPixel(x + i, y);
int r = rgb.R;
int g = rgb.G;
int b = rgb.B;
Rbits += (byte)((r >= 127 ? 0 : 1) << (7 - i));
Gbits += (byte)((g >= 127 ? 0 : 1) << (7 - i));
Bbits += (byte)((b >= 127 ? 0 : 1) << (7 - i));
}
result.Add(Rbits);
result.Add(Gbits);
result.Add(Bbits);
}
}
}
else //全彩
{
for (int y = 0; y < height; y++)//遍历每列
{
for (int x = 0; x < width; x++)//遍历每行
{
Color rgb = content.GetPixel(x, y);
int r = rgb.R;//红色像素值
int g = rgb.G;//绿色像素值
int b = rgb.B;//蓝色像素值
//按照协议,RGB565模式对像素值编码,红色取高5位,绿色取高6位,蓝色取高5位,低位舍弃,从而一个像素需要用两个字节表示
//int data1 = ((r >> 3) << 11) & 0xF800;//红色
//int data2 = ((g >> 2) << 5) & 0x07E0;//绿色
//int data3 = (b >> 3) & 0x001F;//蓝色
//byte[] dbytes = System.BitConverter.GetBytes(data1 + data2 + data3);
int val = rgb.R >> 8 << 11;
val |= rgb.G >> 2 << 5;
val |= rgb.B >> 8;
// 获取RGB单色,并填充低位
int cRed = (val & 0xf800) >> 8;
int cGreen = (val & 0x07E0) >> 3;
int cBlue = (val & 0x001F) << 3;
int n888Color = (cRed << 16) + (cGreen << 8) + (cBlue << 0);
byte[] dbytes = System.BitConverter.GetBytes(n888Color);
result.Add(dbytes[0]);
result.Add(dbytes[1]);
}
}
}
return result;
}
/// <summary>
/// Create content based on text data. Chinese will be rotated.
/// </summary>
/// <param name="text">Text data.</param>
/// <param name="autoFit">Auto fit.</param>
/// <param name="aligment">Alignment.</param>
/// <returns>The btimap</returns>
public Bitmap CreateContent2(string text, bool autoFit = true, StringAlignment aligment = StringAlignment.Center)
{
Bitmap textBitmap = new Bitmap(Width, Height);
Graphics textG = Graphics.FromImage(textBitmap);
textG.FillRectangle(new SolidBrush(Color.Black), 0, 0, textBitmap.Width, textBitmap.Height);
Font textFont = DisplayFont;
SizeF textSize = textG.MeasureString(text, textFont);
Font tempFont = DisplayFont;
if (autoFit)
{
if (textSize.Width < Width && textSize.Height < Height)
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size + (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Width < Width && textSize.Height < Height);
textSize = textG.MeasureString(text, textFont);
}
else
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size - (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Width >= Width || textSize.Height >= Height);
textFont = tempFont;
}
}
else
{
if (textSize.Height < DisplayFont.Size)
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size + (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Height < DisplayFont.Size);
textFont = tempFont;
}
else
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size - (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Height >= DisplayFont.Size);
textFont = tempFont;
}
}
StringFormat textFormat = new StringFormat();
textFormat.Alignment = aligment;
textFormat.LineAlignment = StringAlignment.Center;
// 横屏
float x = 0.0f;
float y = textBitmap.Height / 2 + textBitmap.Height / 16;
switch (aligment)
{
case StringAlignment.Near:
x = 1;
break;
case StringAlignment.Center:
x = textBitmap.Width / 2;
break;
case StringAlignment.Far:
x = textBitmap.Width - 1;
break;
}
textG.DrawString(
text,
textFont,
new SolidBrush(ForeColor),
x,
y,
textFormat);
// 竖屏
if (!Horizontal)
{
textBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
return textBitmap;
}
/// <summary>
/// Create content based on text data. Chinese won't be rotated.
/// </summary>
/// <param name="text">Text data.</param>
/// <param name="autoFit">Auto fit.</param>
/// <param name="aligment">Alignment.</param>
/// <returns>The btimap</returns>
public Bitmap CreateContent(string text, bool autoFit = true, StringAlignment aligment = StringAlignment.Center)
{
//int calWidth = Horizontal ? Width : Height;
//int calHeight = Horizontal ? Height : Width;
//Bitmap textBitmap = new Bitmap(Width, Height);
Horizontal = true;
int calWidth = 80;
int calHeight = 64;
Bitmap textBitmap = Horizontal ? new Bitmap(calWidth, calHeight) : new Bitmap(calHeight, calWidth);
Font font = new Font("宋体", 9f);
Graphics textG = Graphics.FromImage(textBitmap);
textG.FillRectangle(new SolidBrush(Color.Black), 0, 0, textBitmap.Width, textBitmap.Height);
textG.DrawString(text, font, Brushes.White, new PointF() { X = 0, Y = 0 });
string b = string.Join("", Enumerable.Range(0, 5120).Select(a => new { x = a % 80, y = a / 80 })
.Select(x => textBitmap.GetPixel(x.x, x.y).GetBrightness() > 0.5f ? "1" : "0"));
//textBox5.Text += b;
textBitmap.Save("1.bmp");
DisplayFont = new Font("宋体", 12, FontStyle.Regular);
Font textFont = DisplayFont;
SizeF textSize = textG.MeasureString(text, textFont);
Font tempFont = DisplayFont;
if (autoFit)
{
if (textSize.Width < calWidth && textSize.Height < calHeight)
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size + (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Width < calWidth && textSize.Height < calHeight);
textSize = textG.MeasureString(text, textFont);
}
else
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size - (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Width >= calHeight || textSize.Height >= calHeight);
textFont = tempFont;
}
}
else
{
if (textSize.Height < DisplayFont.Size)
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size + (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Height < DisplayFont.Size);
textFont = tempFont;
}
else
{
do
{
textFont = tempFont;
tempFont = new Font(DisplayFont.FontFamily, textFont.Size - (float)0.2);
textSize = textG.MeasureString(text, tempFont);
}
while (textSize.Height >= DisplayFont.Size);
textFont = tempFont;
}
}
StringFormat textFormat = new StringFormat();
textFormat.Alignment = aligment;
textFormat.LineAlignment = StringAlignment.Center;
if (Horizontal) // 横屏
{
float x = 0.0f;
float y = textBitmap.Height / 2 + textBitmap.Height / 16;
switch (aligment)
{
case StringAlignment.Near:
x = 1;
break;
case StringAlignment.Center:
x = textBitmap.Width / 2;
break;
case StringAlignment.Far:
x = textBitmap.Width - 1;
break;
}
textG.DrawString(
text,
textFont,
new SolidBrush(ForeColor),
x,
y,
textFormat);
}
else // 竖屏
{
//float x = textBitmap.Width / 2 - textBitmap.Width / 16;
float x = textBitmap.Width / 2;
float y = 0.0f;
switch (aligment)
{
case StringAlignment.Near:
y = 2;
break;
case StringAlignment.Center:
y = textBitmap.Height / 2;
break;
case StringAlignment.Far:
y = textBitmap.Height - 2;
break;
}
textFormat.FormatFlags = StringFormatFlags.DirectionVertical;
textG.DrawString(
text,
textFont,
new SolidBrush(ForeColor),
x,
y,
textFormat);
}
// 竖屏
if (!Horizontal)
{
textBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
return textBitmap;
}
/// <summary>
/// Create content based on text data. Chinese won't be rotated.
/// </summary>
/// <param name="text">Text data.</param>
/// <param name="smallFontWidth"></param>
/// <param name="bigFontWidth"></param>
/// <returns>The btimap</returns>
public Bitmap CreateContentX(string text, string fontStyle = "黑体", int smallFontWidth = 8, int bigFontWidth = 12)
{
int engFontWidth = Split(text, bigFontWidth).Count > 1 ? smallFontWidth : bigFontWidth;
Bitmap textBitmap = Horizontal ? new Bitmap(Width, Height) : new Bitmap(Height, Width);
Graphics textG = Graphics.FromImage(textBitmap);
textG.FillRectangle(new SolidBrush(Color.Black), 0, 0, textBitmap.Width, textBitmap.Height);
// English Char
Font engFont = new Font("Tahoma", engFontWidth, FontStyle.Regular);
SizeF engSize = textG.MeasureString("A", engFont);
Font tempEngFont = engFont;
if (engSize.Width <= engFontWidth)
{
while (engSize.Width <= engFontWidth)
{
tempEngFont = engFont;
engFont = new Font("Tahoma", (engFont.Size + 0.05f), FontStyle.Regular);
engSize = textG.MeasureString("A", engFont);
}
}
else
{
while (engSize.Width > engFontWidth)
{
engFont = new Font("Tahoma", (engFont.Size - 0.05f), FontStyle.Regular);
engSize = textG.MeasureString("A", engFont);
tempEngFont = engFont;
}
}
// Chinese Char
Font chiFont = new Font(fontStyle, engFontWidth, FontStyle.Regular);
SizeF chiSize = textG.MeasureString("中", chiFont);
Font tempChiFont = chiFont;
if (chiSize.Width <= engFontWidth)
{
while (chiSize.Width < engFontWidth)
{
tempChiFont = chiFont;
chiFont = new Font(fontStyle, (chiFont.Size + 0.05f), FontStyle.Regular);
chiSize = textG.MeasureString("A", chiFont);
}
}
else
{
while (chiSize.Width >= engFontWidth)
{
chiFont = new Font(fontStyle, (chiFont.Size - 0.05f), FontStyle.Regular);
chiSize = textG.MeasureString("A", chiFont);
tempChiFont = chiFont;
}
}
int chars = CalculateEngCharCount(text);
StringFormat textFormat = new StringFormat();
textFormat.Alignment = StringAlignment.Near;
textFormat.LineAlignment = StringAlignment.Center;
if (Horizontal) // 横屏
{
float x = ((float)textBitmap.Width - (float)chars * engFontWidth) / 2;
float y = textBitmap.Height / 2 + textBitmap.Height / 16;
for (int i = 0; i < text.Length; i++)
{
string c = text.Substring(i, 1);
if (System.Text.Encoding.Default.GetBytes(c).Length == 1)
{
x += engFontWidth;
textG.DrawString(
c,
engFont,
new SolidBrush(ForeColor),
x - engFontWidth + engFontWidth / 4.0f,
y,
textFormat);
}
else
{
x += (engFontWidth * 2);
textG.DrawString(
c,
chiFont,
new SolidBrush(ForeColor),
x - (engFontWidth * 2) + engFontWidth / 3.0f,
y,
textFormat);
}
}
}
else // 竖屏
{
float x = textBitmap.Width / 2 - textBitmap.Width / 16;
float y = ((float)textBitmap.Height - (float)chars * engFontWidth) / 2;
textFormat.FormatFlags = StringFormatFlags.DirectionVertical;
for (int i = 0; i < text.Length; i++)
{
string c = text.Substring(i, 1);
if (System.Text.Encoding.Default.GetBytes(c).Length == 1)
{
y += engFontWidth;
textG.DrawString(
c,
engFont,
new SolidBrush(ForeColor),
x,
y - engFontWidth,
textFormat);
}
else
{
y += (engFontWidth * 2);
textG.DrawString(
c,
chiFont,
new SolidBrush(ForeColor),
x,
y - (engFontWidth * 2) + engFontWidth / 3.0f,
textFormat);
}
}
}
// 竖屏
if (!Horizontal)
{
textBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
return textBitmap;
}
}
}