/
Form1.cs
253 lines (230 loc) · 9.48 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
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;
using System.Drawing.Drawing2D;
namespace ImageManipulation
{
public partial class Form1 : Form
{
string fileImage;
string fileText;
Image im;
Image im_bw;
Graphics g;
private Point initialMousePos;
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
fileImage = null;
fileText = null;
im = null;
im_bw = null;
label_chars.Text = "No text file";
label_thresh.Text = "Threshold: " + trackThresh.Value.ToString();
}
private void load_image_Click(object sender, EventArgs e)
{
// User has pressed the button to load an image
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
fileImage = openFileDialog1.FileName;
im = Image.FromFile(fileImage);
pictureBox1.Image = im;
button_flattenBW.Enabled = true;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// User has clicked on the image area
if (im != null) // If an image has been loaded...
{
this.initialMousePos = e.Location;
Bitmap bmap = new Bitmap(im);
// Set the value of the black/white threshold to the selected pixel's brightness
trackThresh.Value = (int)(bmap.GetPixel(e.X, e.Y).GetBrightness() * 256);
// Update the display
label_thresh.Text = "Threshold: " + trackThresh.Value.ToString();
}
}
private void trackThresh_Scroll(object sender, EventArgs e)
{
// If user changes the black/white slider, update the display
label_thresh.Text = "Threshold: " + trackThresh.Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
// Flatten the image to just black and white based on a threshold value
Bitmap bmap = new Bitmap(im);
Color col;
// Keep track of the ratio of black to white
int whiteCount = 0;
int blackCount = 0;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
col = bmap.GetPixel(i, j);
if (col.GetBrightness() * 256 > trackThresh.Value)
{
bmap.SetPixel(i, j, Color.White);
whiteCount++;
}
else
{
bmap.SetPixel(i, j, Color.Black);
blackCount++;
}
}
}
double percentWhite = whiteCount * 1.0 / (whiteCount + blackCount);
label_imageratio.Text = ("W/B ratio: " + percentWhite.ToString()).Remove(16);
im_bw = bmap;
pictureBox1.Image = im_bw;
button_load_text.Enabled = true;
}
private void button_load_text_Click(object sender, EventArgs e)
{
// User has clicked button to load text file
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
fileText = openFileDialog1.FileName;
System.IO.StreamReader file = new System.IO.StreamReader(fileText);
string line;
int charCount = 0;
while ((line = file.ReadLine()) != null)
{
charCount += line.Length;
}
label_chars.Text = "Characters: " + charCount.ToString();
button_checktextsize.Enabled = true;
}
}
private void radio_white_CheckedChanged(object sender, EventArgs e)
{
if (radio_white.Checked) radio_black.Checked = false;
}
private void radio_black_CheckedChanged(object sender, EventArgs e)
{
if (radio_black.Checked) radio_white.Checked = false;
}
private void button2_Click(object sender, EventArgs e)
{
// User has clicked button to check amount of text vs size of image
double textHeight = 0.0;
System.IO.StreamReader file = new System.IO.StreamReader(fileText);
string line;
double width = 0.0;
Font myFont = new Font("Arial", (float)Convert.ToDouble(font_size.Text));
while ((line = file.ReadLine()) != null)
{
if (textHeight == 0)
textHeight = g.MeasureString(line, myFont).Height;
width += g.MeasureString(line, myFont).Width;
}
label_textratio.Text = ("Text/image ratio: "
+ (textHeight * width / (im.Height * im.Width)).ToString()).Remove(23);
button_fittext.Enabled = true;
}
public char safeChar(char c)
{
if (char.IsLetterOrDigit(c)) return c;
if (char.IsPunctuation(c)) return c;
if (c == ' ') return c;
return '\n';
}
private void button_fittext_Click(object sender, EventArgs e)
{
Bitmap bmap = new Bitmap(im.Width, im.Height);
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
bmap.SetPixel(i, j, Color.White);
}
}
Bitmap bw = new Bitmap(im_bw);
System.IO.StreamReader file = new System.IO.StreamReader(fileText);
Font myFont = new Font("Arial", (float)Convert.ToDouble(font_size.Text));
double textHeight = g.MeasureString("X", myFont).Height;
SolidBrush myBrush = new SolidBrush(Color.Black);
string word = "";
string nextChar = safeChar(Convert.ToChar(file.Read())).ToString();
while (nextChar == "\n" || nextChar == "\r")
nextChar = safeChar(Convert.ToChar(file.Read())).ToString();
Graphics gr = Graphics.FromImage(bmap);
Boolean stop = false;
double marginLeft = textHeight;
double marginRight = marginLeft;
double marginTop = textHeight;
double marginBottom = marginTop;
double verOff = marginTop;
double horOff = marginLeft;
double verSqueeze = 1.0;
double horSqueeze = 1.0;
Color target = Color.FromArgb(255, 255, 255);
if (radio_black.Checked) target = Color.FromArgb(0, 0, 0);
while (!file.EndOfStream && !stop)
{
//Find next empty pixel
while(bw.GetPixel((int)horOff,(int)verOff) != target)
{
horOff += 1;
if (horOff >= im.Width - marginLeft - marginRight)
{
horOff = marginLeft;
verOff += textHeight * verSqueeze;
}
if (verOff >= im.Height - marginTop - marginBottom) // Ran out of area before we ran out text
{
stop = true;
break;
}
}
if (!stop)
{
while (bw.GetPixel((int)(horOff + gr.MeasureString(word, myFont).Width
* horSqueeze), (int)verOff) == target)
{
word = word + nextChar;
nextChar = safeChar(Convert.ToChar(file.Read())).ToString();
while (nextChar == "\n" || nextChar == "\r")
nextChar = safeChar(Convert.ToChar(file.Read())).ToString();
if ((int)(horOff + gr.MeasureString(word + nextChar, myFont).Width * horSqueeze)
>= im.Width - marginLeft - marginRight) break;
if ((int)verOff > im.Height - marginTop - marginBottom) break;
if (file.EndOfStream) break;
}
}
gr.DrawString(word, myFont, myBrush, (float)horOff, (float)verOff);
horOff += gr.MeasureString(word, myFont).Width * horSqueeze;
word = "";
if ((int)(horOff + gr.MeasureString(word + nextChar, myFont).Width * horSqueeze)
>= im.Width - marginLeft - marginRight)
{
horOff = marginLeft;
verOff += textHeight * verSqueeze;
}
if ((int)verOff > im.Height - marginTop - marginBottom)
break;
}
pictureBox1.Image = bmap;
}
private void button_save_Click(object sender, EventArgs e)
{
DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
String fileSaveImage = openFileDialog1.FileName;
pictureBox1.Image.Save(fileSaveImage);
}
}
}
}