-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
718 lines (622 loc) · 26.2 KB
/
Form1.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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
using MaxRev.Gdal.Core;
using OSGeo.GDAL;
using System.Diagnostics;
using System.Drawing.Imaging;
namespace Tiffpaint
{
public partial class Form1 : Form
{
//èçîáðàæåíèå
short[] red;
short[] green;
short[] blue;
int width;
int height;
//ðèñîâàíèå
private Point previousPoint;
private bool isDrawing = false;
private Bitmap drawingBitmap;
private Bitmap OriginalImage;
private int grey;
public int GreyThreshold
{
get
{
return grey;
}
set
{
if (value != GreyThreshold)
{
grey = value;
TrackGrey.Value = grey;
Greytxt.Text = grey.ToString();
}
}
}
private int white;
public int WhiteThreshold
{
get
{
return white;
}
set
{
if (value != WhiteThreshold)
{
white = value;
Whitetxt.Text = white.ToString();
TrackWhite.Value = white;
}
}
}
Bitmap ChunkImage;
Pen pen;
Color CurrentColor;
private string imagePath; // Ïåðåìåííàÿ äëÿ õðàíåíèÿ ïóòè ê èçîáðàæåíèþ
string filePath;
private int chunkSize = 512;
private int currentChunkX = 0;
private int currentChunkY = 0;
private int ConvertingNumber = 13; // ×èñëî, íà êîòîðîå íàäî ðàçäåëèòü 16 äëÿ ïðèâåäåíèÿ ê 8
public Form1()
{
InitializeComponent();
GdalBase.ConfigureAll();
}
private void Form1_Load(object sender, EventArgs e)
{
TrackInit();
InitializePen();
GreyThreshold = 300;
WhiteThreshold = 800;
pictureBox2.Visible = false;
pictureBox2.MouseWheel += pictureBox_MouseWheel;
pictureBox1.MouseWheel += pictureBox_MouseWheel;
pictureBox3.MouseWheel += pictureBox_MouseWheel;
}
/// <summary>
/// Èíèöèàëèçàöèÿ ïîëçóíêîâ
/// </summary>
private void TrackInit()
{
TrackGrey.Minimum = 100;
TrackGrey.Maximum = 500;
TrackGrey.TickFrequency = 10;
TrackGrey.SmallChange = 10; // Øàã èçìåíåíèÿ çíà÷åíèÿ ïðè èñïîëüçîâàíèè ñòðåëîê
TrackGrey.LargeChange = 20; // Øàã èçìåíåíèÿ çíà÷åíèÿ ïðè êëèêå íà òðåêáàðå âíå îáëàñòè ïîëçóíêà
TrackGrey.ValueChanged += (sender, e) =>
{
GreyThreshold = TrackGrey.Value;
PrintSelectingAreasImage(pictureBox2, ChunkImage, GreyThreshold, WhiteThreshold);
};
TrackWhite.Minimum = 600;
TrackWhite.Maximum = 1000;
TrackWhite.TickFrequency = 10;
TrackWhite.SmallChange = 10; // Øàã èçìåíåíèÿ çíà÷åíèÿ ïðè èñïîëüçîâàíèè ñòðåëîê
TrackWhite.LargeChange = 20; // Øàã èçìåíåíèÿ çíà÷åíèÿ ïðè êëèêå íà òðåêáàðå âíå îáëàñòè ïîëçóíêà
TrackWhite.ValueChanged += (sender, e) =>
{
WhiteThreshold = TrackWhite.Value;
PrintSelectingAreasImage(pictureBox2, ChunkImage, GreyThreshold, WhiteThreshold);
};
}
/// <summary>
/// Çàãðóæàåò â ìàññèâû çíà÷åíèÿ ïèêñåëåé
/// </summary>
public void LoadImage()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Èçîáðàæåíèÿ|*.tif;*.tiff;*.jpg;*.jpeg;*.png;*.bmp|Âñå ôàéëû|*.*";
openFileDialog.Title = "Âûáåðèòå èçîáðàæåíèå";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
imagePath = openFileDialog.FileName; // Ñîõðàíÿåì ïóòü ê ôàéëó
filePath = imagePath; // Òàêæå ñîõðàíÿåì ïóòü ê ôàéëó â ëîêàëüíóþ ïåðåìåííóþ, åñëè ýòî íåîáõîäèìî
Dataset dataset = Gdal.Open(filePath, Access.GA_ReadOnly);
if (dataset == null)
{
MessageBox.Show("Íå óäàëîñü îòêðûòü ôàéë", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
width = dataset.RasterXSize;
height = dataset.RasterYSize;
short[] buffer = new short[width * height * 3];
dataset.ReadRaster(0, 0, width, height, buffer, width, height, 3, null, 0, 0, 0);
dataset.Dispose();
red = new short[width * height];
green = new short[width * height];
blue = new short[width * height];
for (int i = 0; i < width * height; i++)
{
red[i] = buffer[i];
}
for (int i = 0; i < width * height; i++)
{
green[i] = buffer[i + width * height];
}
for (int i = 0; i < width * height; i++)
{
blue[i] = buffer[i + width * height * 2];
}
}
}
/// <summary>
/// Âûâîäèò êóñîê îðèãèíàëüíîãî èçîáðàæåíèÿ
/// </summary>
/// <param name="box">Êîíòåéíåð äëÿ âûâîäà</param>
/// <param name="chunkX">êîîðäèíàòà X âåðõíåãî ëåâîãî óãëà êóñêà</param>
/// <param name="chunkY">êîîðäèíàòà Y âåðõíåãî ëåâîãî óãëà êóñêà</param>
/// <param name="chunkHeight">Âûñîòà ÷àíêà</param>
/// <param name="chunkWidth">Øèíèíà ÷àíêà</param>
public void PrintImageChunk(PictureBox box, int chunkX, int chunkY, int chunkWidth, int chunkHeight)
{
Bitmap chunkImage = new Bitmap(chunkWidth, chunkHeight);
// Ïðåîáðàçîâàíèå çíà÷åíèé èç short â byte (0-255) è ïîëó÷åíèå êóñêà èçîáðàæåíèÿ
for (int y = 0; y < chunkHeight; y++)
{
for (int x = 0; x < chunkWidth; x++)
{
int index = (chunkY + y) * width + chunkX + x;
if (index >= 0 && index < red.Length)
{
byte redByte = (byte)(red[index] / ConvertingNumber);
byte greenByte = (byte)(green[index] / ConvertingNumber);
byte blueByte = (byte)(blue[index] / ConvertingNumber);
Color color = Color.FromArgb(redByte, greenByte, blueByte);
chunkImage.SetPixel(x, y, color);
}
else
{
// Åñëè èíäåêñ íàõîäèòñÿ çà ïðåäåëàìè ìàññèâà, çàïîëíèòå ýòîò ïèêñåëü ÷åðíûì öâåòîì èëè ëþáûì äðóãèì ïî âàøåìó âûáîðó
chunkImage.SetPixel(x, y, Color.Black);
}
}
}
box.Image = chunkImage;
ChunkImage = chunkImage;
string fileName = Path.GetFileNameWithoutExtension(imagePath); // Èìÿ ôàéëà áåç ðàñøèðåíèÿ
string chunkName = $"{fileName}_Chunk_{currentChunkX}_{currentChunkY}.tiff"; // Óíèêàëüíîå èìÿ ÷àíêà
string chunkPath = Path.Combine(Path.GetDirectoryName(imagePath), chunkName); // Ïîëíûé ïóòü ê ÷àíêó
if (LoadChunkMask(chunkPath))
{
PrintSelectingAreasImage(pictureBox2, ChunkImage, 300, 800);
}
}
/// <summary>
/// Âûâîäèò èçîáðàæåíèå ñ âûäåëåííûìè îáëàñòÿìè
/// </summary>
/// <param name="box">Êîíòåéíåð, êóäà âûâîäèòü</param>
/// <param name="bitmap">Èñõîäíîå èçîáðàæåíèå</param>
/// <param name="grayThreshold">Ïîðîã ÿðêîñòè äëÿ îïðåäåëåíèÿ ñåðîé îáëàñòè</param>
/// <param name="whiteThreshold">Ïîðîã ÿðêîñòè äëÿ îïðåäåëåíèÿ áåëîé îáëàñòè</param>
public void PrintSelectingAreasImage(PictureBox box, Bitmap bitmap, int grayThreshold, int whiteThreshold)
{
if (bitmap != null)
{
// Ñîçäàíèå îáúåêòà Bitmap
drawingBitmap = new Bitmap(bitmap.Width, bitmap.Height);
Color gray = Color.FromArgb(128, 128, 128);
Color black = Color.Black;
Color white = Color.White;
// Çàïîëíåíèå èçîáðàæåíèÿ äàííûìè
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
int brightness = (int)(pixelColor.R * ConvertingNumber + pixelColor.G * ConvertingNumber + pixelColor.B * ConvertingNumber) / 3;
if (brightness < grayThreshold)
drawingBitmap.SetPixel(x, y, gray);
else if (brightness < whiteThreshold && brightness > grayThreshold)
drawingBitmap.SetPixel(x, y, white);
else
drawingBitmap.SetPixel(x, y, black);
}
}
box.Image = drawingBitmap;
pictureBox3.Image = drawingBitmap;
}
}
//=================================================== Ðèñîâàíèå
/// <summary>
/// Ñîáûòèå íàæàòèÿ ìûøè ïðè ðèñîâàíèè
/// </summary>
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
if (pictureBox2.SizeMode == PictureBoxSizeMode.Zoom)
{
previousPoint = PictureBoxToBitmapCoordinates(pictureBox2, e.Location);
}
else
previousPoint = e.Location;
}
/// <summary>
/// Îáðáîò÷èê ñîáûòèÿ ïåðåìåùåíïèÿ ìûøè ïðè ðèñîâàíèè
/// </summary>
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// Ïðåîáðàçîâàíèå êîîðäèíàò ìûøè èç PictureBox â êîîðäèíàòû Bitmap
Point bitmapLocation;
if (pictureBox2.SizeMode == PictureBoxSizeMode.Zoom)
{
bitmapLocation = PictureBoxToBitmapCoordinates(pictureBox2, e.Location);
}
else
bitmapLocation = e.Location;
using (Graphics g = Graphics.FromImage(drawingBitmap))
{
// Îêðóãëåíèå êîîðäèíàò ìûøè
Point roundedPreviousPoint = new Point((int)Math.Round((double)previousPoint.X), (int)Math.Round((double)previousPoint.Y));
Point roundedBitmapLocation = new Point((int)Math.Round((double)bitmapLocation.X), (int)Math.Round((double)bitmapLocation.Y));
g.DrawLine(pen, roundedPreviousPoint, roundedBitmapLocation);
}
pictureBox2.Invalidate(); // Îáíîâëåíèå PictureBox
pictureBox3.Invalidate(); // Îáíîâëåíèå PictureBox
previousPoint = bitmapLocation;
}
}
/// <summary>
/// Îáðàáîò÷èê ñîáûòèÿ îòïóñêàíèÿ ìûøè ïðè ðèñîâàíèè
/// </summary>
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
/// <summary>
/// Ìåòîä äëÿ ïðåîáðàçîâàíèÿ êîîðäèíàò ìûøè èç PictureBox â êîîðäèíàòû Bitmap
/// </summary>
private Point PictureBoxToBitmapCoordinates(PictureBox pictureBox, Point pictureBoxLocation)
{
float scaleX = (float)drawingBitmap.Width / pictureBox.ClientSize.Width;
float scaleY = (float)drawingBitmap.Height / pictureBox.ClientSize.Height;
// Ó÷èòûâàåì ñìåùåíèå PictureBox ïðè ìàñøòàáèðîâàíèè
int bitmapX = (int)(pictureBoxLocation.X * scaleX);
int bitmapY = (int)(pictureBoxLocation.Y * scaleY);
return new Point(bitmapX, bitmapY);
}
/// <summary>
/// Ôóíêöèÿ ñîõðàíåíèÿ ãîòîâîãî èçîáðàæåíèÿ
/// </summary>
/// <param name="bitmap">Èçîáðàæåíèå</param>
/// <param name="outputPath">Ïóòü ñîõðàíåíèÿ</param>
public static void SaveAsGeoTiff(Bitmap bitmap, string outputPath)
{
Gdal.AllRegister();
Dataset ds = Gdal.GetDriverByName("GTiff").Create(outputPath, bitmap.Width, bitmap.Height, 1, DataType.GDT_UInt16, null);
// Write bitmap data to the GeoTIFF dataset
BitmapDataToRaster(bitmap, ds);
// Close the dataset
ds.Dispose();
}
/// <summary>
/// Ôóíêöèÿ êîïèðîâàíèÿ äàííûõ èç bitmap â dataset
/// </summary>
private static void BitmapDataToRaster(Bitmap bitmap, Dataset ds)
{
// Áëîêèðîâêà èçîáðàæåíèÿ äëÿ ïîëó÷åíèÿ äàííûõ â ôîðìàòå BitmapData
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
try
{
int stride = data.Stride;
byte[] buffer = new byte[stride * bitmap.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
// Èòåðàöèÿ ïî êàæäîìó ïèêñåëþ èçîáðàæåíèÿ
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
int index = y * stride + x * 3; // Èíäåêñ òåêóùåãî ïèêñåëÿ â ìàññèâå buffer
// Ïîëó÷åíèå çíà÷åíèÿ êðàñíîãî êàíàëà òåêóùåãî ïèêñåëÿ
byte redValue = buffer[index]; // Êðàñíûé êàíàë (èíäåêñ 2 â ôîðìàòå BGR)
// Çàïèñü çíà÷åíèÿ êðàñíîãî êàíàëà ïèêñåëÿ â ðàñòð
ds.GetRasterBand(1).WriteRaster(x, y, 1, 1, new byte[] { redValue }, 1, 1, 1, 1);
}
}
}
finally
{
// Ðàçáëîêèðîâêà äàííûõ èçîáðàæåíèÿ
bitmap.UnlockBits(data);
}
}
/// <summary>
/// Îáðàáîò÷èê çàïðåùàþùèé ââîäèòü â òåêñòîâîå ïîëå ÷òî-ëèáî êðîìå öèôð
/// </summary>
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true; // Ïðåäîòâðàòèòü ââîä ñèìâîëà
}
}
/// <summary>
/// Èíèöèàëèçàöèÿ êèñòè äëÿ ðèñîâàíèÿ
/// </summary>
private void InitializePen()
{
txtSize.Text = "2";
BlackRd.Checked = true;
}
/// <summary>
/// Âûáîð êíîïêè ÷åðíîãî öâåòà
/// </summary>
private void BlackRd_CheckedChanged(object sender, EventArgs e)
{
if (BlackRd.Checked)
{
pen = new Pen(Color.Black, Convert.ToInt32(txtSize.Text));
CurrentColor = Color.Black;
}
}
/// <summary>
/// Âûáîð êíîïêè áåëîãî öâåòà
/// </summary>
private void WhiteRb_CheckedChanged(object sender, EventArgs e)
{
if (WhiteRb.Checked)
{
CurrentColor = Color.White;
pen = new Pen(Color.White, Convert.ToInt32(txtSize.Text));
}
}
/// <summary>
/// Âûáîð êíîïêè ñåðîãî öâåòà
/// </summary>
private void GrayRb_CheckedChanged(object sender, EventArgs e)
{
if (GrayRb.Checked)
{
pen = new Pen(Color.Gray, Convert.ToInt32(txtSize.Text));
CurrentColor = Color.Gray;
}
}
/// <summary>
/// Îáðàáîò÷èê ñîáûòèÿ èçìåíåíèÿ òåêñòîâîãî ïîëÿ
/// </summary>
private void txtSize_TextChanged(object sender, EventArgs e)
{
pen = new Pen(CurrentColor, Convert.ToInt32(txtSize.Text));
}
/// <summary>
/// Îáðàáîò÷èê ïðîêðóòêè êîëåñà ìûøè äëÿ ðåàëèçàöèÿ ôóíêöèîíàëà zoom
/// </summary>
private void pictureBox_MouseWheel(object sender, MouseEventArgs e)
{
if (sender is PictureBox box)
{
if (e.Delta > 0)
{
// Óâåëè÷åíèå ìàñøòàáà ïðè ïðîêðóòêå êîëåñèêà ââåðõ
if (box.SizeMode != PictureBoxSizeMode.Zoom)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
//previousPoint = PictureBoxToBitmapCoordinates(pictureBox2, previousPoint);
}
}
else
{
// Óìåíüøåíèå ìàñøòàáà ïðè ïðîêðóòêå êîëåñèêà âíèç
if (box.SizeMode != PictureBoxSizeMode.Normal)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox2.SizeMode = PictureBoxSizeMode.Normal;
pictureBox3.SizeMode = PictureBoxSizeMode.Normal;
}
}
}
}
/// <summary>
/// Îáðàáîò÷èê íàæàòèÿ êíîïîê ñ êëàâèàòóðû
/// </summary>
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Îáðàáîòêà íàæàòèÿ êëàâèø Z, X è C
if (e.KeyCode == Keys.Z)
{
if (pictureBox2.Visible == true)
{
pictureBox2.Visible = false;
}
}
else if (e.KeyCode == Keys.X)
{
if (pictureBox2.Visible == false)
{
pictureBox2.Visible = true;
}
}
else if (e.KeyCode == Keys.A)
{
ShowPreviousChunk();
}
else if (e.KeyCode == Keys.S)
{
ShowNextChunk();
}
}
/// <summary>
/// Îáðàáîò÷èê ñîáûòèÿ íàæàòèÿ êíîïêè çàãðóçêè èçîáðàæåíèÿ
/// </summary>
private void LoadOriginalImagebtn_Click(object sender, EventArgs e)
{
LoadImage();
StatisticksWrite();
//PrintOriginalImage(pictureBox1);
PrintImageChunk(pictureBox1, 0, 0, 512, 512);
ShowChunkInfo();
}
/// <summary>
/// Âûâîä ñòàòèñòèêè
/// </summary>
private void StatisticksWrite()
{
int chunkSize = 512;
int rows = (int)Math.Ceiling((double)height / chunkSize);
int cols = (int)Math.Ceiling((double)width / chunkSize);
int totalChunks = rows * cols;
int lastRowChunks = (int)Math.Ceiling((double)(width % chunkSize) / chunkSize);
int lastColChunks = (int)Math.Ceiling((double)(height % chunkSize) / chunkSize);
totalChunks += (lastRowChunks * (rows - 1)) + lastColChunks;
Debug.WriteLine("Êîëè÷åñòâî êóñî÷êîâ ðàçìåðîì 512x512 â èçîáðàæåíèè {0}x{1}: {2}", width, height, totalChunks);
}
/// <summary>
/// Ïðîâåðêà ïîðîãîâûõ çíà÷åíèé
/// </summary>
private void Colortxt_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Greytxt.Text) || string.IsNullOrEmpty(Whitetxt.Text))
{
ApplyBtn.Enabled = false;
}
else
{
ApplyBtn.Enabled = true;
}
}
/// <summary>
/// Îáðàáîò÷èê íàæàòèÿ êíîïêè ïðèìåíåíèÿ ïîðîãîâûõ çíà÷åíèé
/// </summary>
private void ApplyBtn_Click(object sender, EventArgs e)
{
if (GreyThreshold > 0 && WhiteThreshold > 0 && GreyThreshold < WhiteThreshold)
{
PrintSelectingAreasImage(pictureBox2, ChunkImage, GreyThreshold, WhiteThreshold);
}
}
/// <summary>
/// Ìåòîä äëÿ îòîáðàæåíèÿ ñëåäóþùåãî ÷àíêà
/// </summary>
private void ShowNextChunk()
{
int chunkWidth = Math.Min(chunkSize, width - currentChunkX);
int chunkHeight = Math.Min(chunkSize, height - currentChunkY);
// Ñîõðàíÿåì òåêóùèé ÷àíê ïîä óíèêàëüíûì èìåíåì
string fileName = Path.GetFileNameWithoutExtension(imagePath); // Èìÿ ôàéëà áåç ðàñøèðåíèÿ
string chunkName = $"{fileName}_Chunk_{currentChunkX}_{currentChunkY}.tiff"; // Óíèêàëüíîå èìÿ ÷àíêà
string chunkPath = Path.Combine(Path.GetDirectoryName(imagePath), chunkName); // Ïîëíûé ïóòü ê ÷àíêó
// Ñîõðàíÿåì òåêóùèé ÷àíê êàê èçîáðàæåíèå ïî ïîëó÷åííîìó ïóòè
SaveChunkImage(chunkPath);
// Åñëè òåêóùèé ÷àíê çàíèìàåò âåñü ðÿä, ïåðåõîäèì ê ñëåäóþùåìó ðÿäó
if (currentChunkX + chunkWidth >= width)
{
currentChunkX = 0;
currentChunkY += chunkSize;
}
else
{
currentChunkX += chunkSize;
}
// Îòîáðàæàåì ÷àíê è îáíîâëÿåì èíôîðìàöèþ î ÷àíêå
PrintImageChunk(pictureBox1, currentChunkX, currentChunkY, chunkWidth, chunkHeight);
ShowChunkInfo();
chunkName = $"{fileName}_Chunk_{currentChunkX}_{currentChunkY}.tiff"; // Óíèêàëüíîå èìÿ ÷àíêà
chunkPath = Path.Combine(Path.GetDirectoryName(imagePath), chunkName); // Ïîëíûé ïóòü ê ÷àíêó
if (LoadChunkMask(chunkPath))
{
PrintSelectingAreasImage(pictureBox2, ChunkImage, 300, 800);
}
}
private void SaveChunkImage(string chunkPath)
{
// Ïðîâåðÿåì, ñóùåñòâóåò ëè ôàéë
if (File.Exists(chunkPath))
{
File.Delete(chunkPath);
}
// Ñîõðàíÿåì Bitmap â ôàéë
drawingBitmap.Save(chunkPath, ImageFormat.Tiff);
drawingBitmap.Dispose();
}
private bool LoadChunkMask(string chunkPath)
{
if (File.Exists(chunkPath))
{
Bitmap originalBitmap = new Bitmap(chunkPath);
drawingBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);
for (int y = 0; y < originalBitmap.Height; y++)
{
for (int x = 0; x < originalBitmap.Width; x++)
{
// Ïîëó÷àåì öâåò ïèêñåëÿ èç originalBitmap
Color pixelColor = originalBitmap.GetPixel(x, y);
// Óñòàíàâëèâàåì öâåò ïèêñåëÿ â drawingBitmap
drawingBitmap.SetPixel(x, y, pixelColor);
}
}
originalBitmap.Dispose();
// Îòîáðàæàåì êëîíèðîâàííûé Bitmap íà PictureBox'àõ
pictureBox2.Image = drawingBitmap;
pictureBox3.Image = drawingBitmap;
return false;
}
return true;
}
/// <summary>
/// Ìåòîä äëÿ îòîáðàæåíèÿ ïðåäûäóùåãî ÷àíêà
/// </summary>
private void ShowPreviousChunk()
{
// Ñîõðàíÿåì òåêóùèé ÷àíê ïîä óíèêàëüíûì èìåíåì
string fileName = Path.GetFileNameWithoutExtension(imagePath); // Èìÿ ôàéëà áåç ðàñøèðåíèÿ
string chunkName = $"{fileName}_Chunk_{currentChunkX}_{currentChunkY}.tiff"; // Óíèêàëüíîå èìÿ ÷àíêà
string chunkPath = Path.Combine(Path.GetDirectoryName(imagePath), chunkName); // Ïîëíûé ïóòü ê ÷àíêó
SaveChunkImage(chunkPath);
int newChunkX = currentChunkX - chunkSize;
// Åñëè íîâàÿ êîîðäèíàòà X îòðèöàòåëüíàÿ, çíà÷èò íóæíî ïåðåéòè ê ïðåäûäóùåìó ðÿäó
if (newChunkX < 0 && currentChunkY >= chunkSize)
{
// Ïåðåéòè ê ïðåäûäóùåìó ðÿäó
currentChunkX = Math.Max(0, width - chunkSize);
currentChunkY -= chunkSize;
}
// Åñëè íîâàÿ êîîðäèíàòà X íå îòðèöàòåëüíàÿ, îñòàâëÿåì åå áåç èçìåíåíèé
else
{
currentChunkX = Math.Max(0, newChunkX);
}
// Îòêðûòü ìàñêó ïðåäûäóùåãî ÷àíêà, åñëè îíà ñóùåñòâóåò
// Îòîáðàçèòü ÷àíê
PrintImageChunk(pictureBox1, currentChunkX, currentChunkY, chunkSize, chunkSize);
string maskFolderPath = Path.GetDirectoryName(imagePath);
string fileName2 = Path.GetFileNameWithoutExtension(imagePath); // Èìÿ ôàéëà áåç ðàñøèðåíèÿ
string maskFileName = $"{fileName2}_Chunk_{currentChunkX}_{currentChunkY}.tiff";
string maskFilePath = Path.Combine(maskFolderPath, maskFileName);
LoadChunkMask(maskFilePath);
ShowChunkInfo();
}
private void Back_btn_Click(object sender, EventArgs e)
{
ShowPreviousChunk();
}
private void Further_btn_Click(object sender, EventArgs e)
{
ShowNextChunk();
}
private void ShowChunkInfo()
{
int chunkWidth = Math.Min(chunkSize, width - currentChunkX);
int chunkHeight = Math.Min(chunkSize, height - currentChunkY);
int chunkNumberX = currentChunkX / chunkSize + 1;
int chunkNumberY = currentChunkY / chunkSize + 1;
int totalChunksX = (int)Math.Ceiling((double)width / chunkSize);
int totalChunksY = (int)Math.Ceiling((double)height / chunkSize);
int totalChunks = totalChunksX * totalChunksY;
int currentChunkNumber = (chunkNumberY - 1) * totalChunksX + chunkNumberX;
string info = string.Format("Êîîðäèíàòû ÷àíêà: ({0}, {1})\n", currentChunkX, currentChunkY);
info += string.Format("Ðàçìåð ÷àíêà: {0}x{1}\n", chunkWidth, chunkHeight);
info += string.Format("Îáùåå êîëè÷åñòâî ÷àíêîâ: {0}\n", totalChunks);
info += string.Format("Ýòî ÷àíê íîìåð {0} èç {1}", currentChunkNumber, totalChunks);
ChunkInfo_lbl.Text = info;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}