forked from bestyize/PictureFetcher
/
MainForm.cs
277 lines (246 loc) · 8.71 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PictureReader
{
public partial class MainForm : Form
{
private string src_pic_name;
public MainForm()
{
InitializeComponent();
}
private void Btn_mult_convert_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string[] filenames = openFileDialog.FileNames;
for (int i = 0; i < filenames.Length; i++)
{
pb_src.Image = Image.FromFile(filenames[i]);
Bitmap bitmap = new Bitmap(pb_src.Image);
Bitmap newPic = convertSize(bitmap, 128, 64);
pb_to.Image = newPic;
src_pic_name = filenames[i];
newPic.Save(src_pic_name.Substring(0, src_pic_name.LastIndexOf(".")) + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
MessageBox.Show("转换完成,已保存到同级目录下");
}
}
private void Btn_single_convert_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filename = openFileDialog.FileName;
src_pic_name = filename;
label_src_pic_name.Text = filename.Substring(filename.LastIndexOf("\\")+1);
pb_src.Image = Image.FromFile(filename);
convert();
}
}
private Bitmap convertSize(Bitmap srcPicBitmap, int newWidth, int newHeight)
{
int width = srcPicBitmap.Width;
int height = srcPicBitmap.Height;
int xStart = width % newWidth/2;
int yStart = height % newHeight / 2;
int widthStep = width / newWidth;
int heightStep = height / newHeight;
Bitmap newPicBitmap = new Bitmap(newWidth, newHeight);
int x = 0, y = 0;
for (int i = xStart; i < width- xStart-1; i += widthStep)
{
for (int j = yStart; j < height-yStart-1; j += heightStep)
{
Color color = srcPicBitmap.GetPixel(i, j);
if (cb_reverse.Checked == false)
{
if ((color.R + color.G + color.B) > Convert.ToInt32(nud_yuzhi.Value))
{
color = Color.Black;
}
else
{
color = Color.White;
}
}
else {
if ((color.R + color.G + color.B) < Convert.ToInt32(nud_yuzhi.Value))
{
color = Color.Black;
}
else
{
color = Color.White;
}
}
newPicBitmap.SetPixel(x, y, color);
y++;
}
y = 0;
x++;
}
convertToArray(newPicBitmap);
return newPicBitmap;
}
private void nud_test(object sender, System.EventArgs e)
{
convert();
}
private void Btn_save_Click(object sender, EventArgs e)
{
Bitmap newPic = new Bitmap(pb_to.Image);
newPic.Save(src_pic_name.Substring(0,src_pic_name.LastIndexOf("."))+".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
MessageBox.Show("保存成功!");
}
private void convertToArray(Bitmap bitmap,int mode)
{
int width = bitmap.Width;
int high = bitmap.Height;
string newString = "unsigned char yize_image[1024]={\r\n";
int pixel = 0;
Byte ch=0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < high; j++) {
Color color = bitmap.GetPixel(i, j);
if ((color.R + color.G + color.B) < Convert.ToInt32(nud_yuzhi.Value))
{
pixel = 0;
}
else
{
pixel = 1;
}
if (j % 8 == 0)
{
newString+=ch+",";
ch = 0;
}
if (pixel == 1)
{
ch |= (Byte)(1 << (j % 8));
}
}
newString += "\r\n";
}
tb_data.Text=newString+"};";
}
private void convertToArray(Bitmap bitmap)
{
int width = bitmap.Width;
int high = bitmap.Height;
string newString = "";
int pixel = 0;
Byte ch = 0;
List<Byte> byteList = new List<byte>();
if (cb_text.Checked)
{
newString += "unsigned char yize_image[1024]={\r\n";
}
for (int page = 0; page < 8; page++)
{
for (int i = 0; i < 128; i++)
{
for (int j = page * 8; j < page * 8 + 8; j++)
{
Color color = bitmap.GetPixel(i, j);
if ((color.R + color.G + color.B) < Convert.ToInt32(nud_yuzhi.Value))
{
pixel = 0;
}
else
{
pixel = 1;
}
if (j % 8 == 0)
{
//byteList.Add(ch);
if (cb_ascii.Checked)
{
newString += ch.ToString("X2") + " ";
}else if(cb_text.Checked){
newString += ch + ",";
}
ch = 0;
}
if (pixel == 1)
{
ch |= (Byte)(1 << (j % 8));
}
}
}
if (cb_text.Checked)
{
newString += "\r\n";
}
}
//byteList.ToString();
if (cb_text.Checked)
{
newString += "};";
}
tb_data.Text = newString ;
}
private void Cb_text_CheckedChanged(object sender, EventArgs e)
{
if (cb_ascii.Checked && cb_text.Checked)
{
cb_ascii.Checked = false;
}
else {
cb_ascii.Checked = true;
}
convert();
}
private void Cb_ascii_CheckedChanged(object sender, EventArgs e)
{
if (cb_ascii.Checked && cb_text.Checked)
{
cb_text.Checked = false;
}
else
{
cb_text.Checked = true;
}
if (pb_src.Image == null)
{
MessageBox.Show("请先选择图像","温馨提示");
return;
}
convert();
}
private void Cb_reverse_CheckedChanged(object sender, EventArgs e)
{
convert();
}
private void convert()
{
if (pb_src.Image == null)
{
MessageBox.Show("请先选择图像", "温馨提示");
return;
}
Bitmap bitmap = new Bitmap(pb_src.Image);
Bitmap newPic = convertSize(bitmap, 128, 64);
pb_to.Image = newPic;
}
private void Track_bar_ocap_ValueChanged(object sender, EventArgs e)
{
MainForm.ActiveForm.Opacity=track_bar_ocap.Value/100.0;
}
private void Ll_open_source_address_Clicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/bestyize/PictureFetcher");
}
}
}