forked from kemot90/APO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThresholdForm.cs
95 lines (79 loc) · 2.92 KB
/
ThresholdForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IMGProc {
public partial class ThresholdForm : Form {
private FastBitmap bmpOriginal;
private FastBitmap bmpModified;
private Graphics graphicsOriginal;
private Graphics graphicsModified;
public FastBitmap Image {
get { return bmpModified; }
set { bmpModified = value; }
}
public ThresholdForm(FastBitmap bmp) {
InitializeComponent();
bmpOriginal = bmp;
bmpModified = new FastBitmap((Bitmap)bmp.Bitmap.Clone());
originalPanel.Size = bmp.Size;
originalPanel.Left = 0;
originalPanel.Top = 0;
containerPanel1.Size = new Size(300, 300);
containerPanel1.Left = 10;
containerPanel1.Top = 10;
graphicsOriginal = originalPanel.CreateGraphics();
modifiedPanel.Size = bmp.Size;
modifiedPanel.Left = 0;
modifiedPanel.Top = 0;
containerPanel2.Size = new Size(300, 300);
containerPanel2.Left = 320;
containerPanel2.Top = 10;
graphicsModified = modifiedPanel.CreateGraphics();
ApplyThreshold((byte)127);
}
public void Redraw() {
bmpOriginal.Draw(graphicsOriginal, 0, 0);
bmpModified.Draw(graphicsModified, 0, 0);
}
public void RedrawOriginal() {
bmpOriginal.Draw(graphicsOriginal, 0, 0);
}
public void RedrawModified() {
bmpModified.Draw(graphicsModified, 0, 0);
}
private void containerPanel1_Scroll(object sender, ScrollEventArgs e) {
RedrawOriginal();
}
private void trackBar1_Scroll(object sender, EventArgs e) {
int value = trackBar1.Value;
label1.Text = value.ToString();
ApplyThreshold((byte)value);
RedrawModified();
}
private void ApplyThreshold(byte value) {
for (int i = 0; i < bmpOriginal.Size.Width; ++i) {
for (int j = 0; j < bmpOriginal.Size.Height; ++j) {
byte newColor = bmpOriginal[i, j].R > value ? (byte)255 : (byte)0;
bmpModified[i, j] = Color.FromArgb(255, newColor, newColor, newColor);
}
}
}
private void originalPanel_Paint(object sender, PaintEventArgs e) {
RedrawOriginal();
}
private void modifiedPanel_Paint(object sender, PaintEventArgs e) {
RedrawModified();
}
private void buttonOK_Click(object sender, EventArgs e)
{
ImagePreview imagePrev = new ImagePreview(bmpModified);
imagePrev.MdiParent = this.MdiParent;
imagePrev.Show();
}
}
}