forked from yaunqiying/EasyCharts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathForm_GetData.cs
603 lines (469 loc) · 22.3 KB
/
Form_GetData.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
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 Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn_Graphics
{
public partial class Form_GetData : Form
{
public Point currentPoint, formerpoint;//定义两个点(启点,终点)
public static bool drawing = false;//设置一个启动标志
public static bool Drawing_Line = true;//设置一个启动标志
public static bool Falg_Inputimage = false;
public static int Series_Num = 0;
public static int Point_Num = 0;
public bool Flag_XAxisMin = false;
public bool Flag_XAxisMax = false;
public bool Flag_YAxisMin = false;
public bool Flag_YAxisMax = false;
public Point XAxisMin, XAxisMax, YAxisMin, YAxisMax;
//public int[,] GetData;
public System.Collections.ArrayList DataX = new System.Collections.ArrayList();
public System.Collections.ArrayList DataY = new System.Collections.ArrayList();
public Point PointStart, PointEnd;
public double[] RGBStart = new double[3];
public double[] RGBEnd = new double[3];
public double[] RGBLine = new double[3];
public bool Flag_PointStart = false;
public bool Flag_PointEnd = false;
private Color srcColor;
private Bitmap bmDraw, bmRegion;
public Form_GetData()
{
InitializeComponent();
//string item = "ScatterXolor2.jpg";
//string path = System.AppDomain.CurrentDomain.BaseDirectory + "/" + item;
//this.pictureBox_Data.Image = Image.FromFile(path);
}
private void tabControl1_Selected(Object sender, TabControlEventArgs e)
{
if (tabControl1.SelectedIndex == 1)
{
button_Automatic.Enabled = false;
button_GetData2.Enabled = false;
button_LableData.Enabled = false;
}
else
{
button_Automatic.Enabled = false;
button_GetData2.Enabled = false;
button_LableData.Enabled = true;
}
}
private void button_Xaxismin_Click(object sender, EventArgs e)
{
Flag_XAxisMin = true;
Flag_XAxisMax = false;
Flag_YAxisMin = false;
Flag_YAxisMax = false;
}
private void button_Xaxismax_Click(object sender, EventArgs e)
{
Flag_XAxisMin = false;
Flag_XAxisMax = true;
Flag_YAxisMin = false;
Flag_YAxisMax = false;
}
private void button_Yaxismin_Click(object sender, EventArgs e)
{
Flag_XAxisMin = false;
Flag_XAxisMax = false;
Flag_YAxisMin = true;
Flag_YAxisMax = false;
//button__Yaxismax.Enabled = true;
}
private void button__Yaxismax_Click(object sender, EventArgs e)
{
Flag_XAxisMin = false;
Flag_XAxisMax = false;
Flag_YAxisMin = false;
Flag_YAxisMax = true;
}
private void button_Start_Click(object sender, EventArgs e)
{
Flag_PointStart = true;
}
private void button_End_Click(object sender, EventArgs e)
{
Flag_PointStart = false;
Flag_PointEnd = true;
}
private void button_Automatic_Click(object sender, EventArgs e)
{
DataX = new System.Collections.ArrayList();
DataY = new System.Collections.ArrayList();
for (int i = 0; i < 3; i++)
{ RGBLine[i] = (RGBEnd[i] + RGBStart[i]) / 2; }
int Mat_cols = PointEnd.X - PointStart.X + 1;
int Mat_rows = YAxisMin.Y-YAxisMax.Y + 1;
int pointx, pointy;
double Min_Distance = double.MaxValue;
int Min_indexX = PointStart.X;
int Min_indexY= PointStart.Y;
int Step = Convert.ToInt32(textBox_Step.Text);
double Threshold = double.Parse(textBox_Threshold.Text);
int Search_range = 50;
double[,] Distance = new double[Search_range*2+1, Mat_cols];
for (int j = 0; j < Mat_cols; j++)
{
for (int i = 0; i < Search_range * 2 + 1; i++)
{
Distance[i, j] = double.MaxValue;
}
}
DataX.Add(PointStart.X);
DataY.Add(PointStart.Y);
for (int j = 1; j < Mat_cols; j=j+Step)
{
pointx = PointStart.X + j;
Min_Distance = double.MaxValue;
for (int i = -Search_range; i <= Search_range; i++)
{
pointy = Convert.ToInt32(DataY[DataY.Count - 1]) + i;
if (pointy <= YAxisMin.Y & pointy>= YAxisMax.Y)
{
srcColor = bmDraw.GetPixel(pointx, pointy);
Distance[i+ Search_range, j] = Math.Sqrt(Math.Pow((RGBLine[0]- srcColor.R),2)+ Math.Pow((RGBLine[1] - srcColor.G), 2)+ Math.Pow((RGBLine[2] - srcColor.B), 2));
if (Distance[i + Search_range, j] < Min_Distance)
{
Min_Distance = Distance[i + Search_range, j];
Min_indexX = pointx;
Min_indexY = pointy;
}
}
}
if (Min_Distance< Threshold)
{
DataX.Add(Min_indexX);
DataY.Add(Min_indexY);
}
}
System.Drawing.Graphics g = this.pictureBox_Data.CreateGraphics();
//System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmDraw);
for (int i = 0; i < DataX.Count; i++)
{
g.DrawEllipse(new Pen(Color.Red, 4), Convert.ToInt32(DataX[i]) - 2, Convert.ToInt32(DataY[i]) - 2, 4, 4);
}
//g.Save();
//Dispose();
//this.pictureBox_Data.Image = bmDraw;
button_GetData2.Enabled = true;
Series_Num = Series_Num + 1;
}
private void PictureBox_Data_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (Falg_Inputimage)
{
//System.Drawing.Graphics g = pictureBox_LocalRegion.CreateGraphics();
//Cursor.Current = myCursor;
Rectangle sourceRectangle = new Rectangle(e.X - 10, e.Y - 10, 20, 20);
//Rectangle destRectangle1 = new Rectangle(0, 0, 160, 150);
// g.DrawImage(this.pictureBox_Data.Image, destRectangle1, sourceRectangle, GraphicsUnit.Pixel);
Bitmap bitmap = (Bitmap)this.pictureBox_Data.Image;
Bitmap Region = bitmap.Clone(sourceRectangle, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
int centerx = pictureBox_LocalRegion.Width / 2;
int centery = pictureBox_LocalRegion.Height / 2;
bmRegion = new Bitmap(this.pictureBox_LocalRegion.Width, this.pictureBox_LocalRegion.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmRegion);
double scale_width = Convert.ToDouble(bmRegion.Width) / Region.Width;
double scale_height = Convert.ToDouble(bmRegion.Height) / Region.Height;
double scale = scale_width;
if (scale_width > scale_height) scale = scale_height;
// centerx = bmRegion.Width / 2;
// centery = bmRegion.Height / 2;
int newWidth = Convert.ToInt32(Region.Width * scale / 2);
int newHeight = Convert.ToInt32(Region.Height * scale / 2);
Rectangle rg = new Rectangle(centerx - newWidth, centery - newHeight, newWidth * 2, newHeight * 2);
//将bm内rg所指定的区域绘制到bm1
g.DrawImage(Region, rg);
g.DrawEllipse(new Pen(Color.Red, 2), centerx - 2, centery - 2, 4, 4);
Point point1 =new Point(0, centery);
Point point2 = new Point(bmRegion.Width, centery);
g.DrawLine(new Pen(Color.Blue, 1), point1, point2);
point1 = new Point(centerx,0);
point2 = new Point(centerx, bmRegion.Height);
g.DrawLine(new Pen(Color.Blue, 1), point1, point2);
this.pictureBox_LocalRegion.Image = bmRegion;
System.Drawing.Graphics g2 = this.pictureBox_Data.CreateGraphics();
if (Flag_XAxisMin)
{ this.pictureBox_Data.Refresh();
g2.DrawString("XAxis-min", new System.Drawing.Font("Arial", 8.0F), new SolidBrush(Color.Black), e.X, e.Y); }
if (Flag_XAxisMax)
{
this.pictureBox_Data.Refresh();
g2.DrawString("XAxis-max", new System.Drawing.Font("Arial", 8.0F), new SolidBrush(Color.Black), e.X, e.Y);
}
if (Flag_YAxisMin)
{
this.pictureBox_Data.Refresh();
g2.DrawString("YAxis-min", new System.Drawing.Font("Arial", 8.0F), new SolidBrush(Color.Black), e.X, e.Y);
}
if (Flag_YAxisMax)
{
this.pictureBox_Data.Refresh();
g2.DrawString("YAxis-max", new System.Drawing.Font("Arial", 8.0F), new SolidBrush(Color.Black), e.X, e.Y);
}
}
}
private void Form_GetData_Load(object sender, EventArgs e)
{
button_GetData.Enabled =false;
button_LableData.Enabled = false;
button_Xaxismin.Enabled = false;
button_Xaxismax.Enabled = false;
button_Yaxismin.Enabled = false;
button__Yaxismax.Enabled = false;
button_End.Enabled = false;
drawing = false;//设置一个启动标志
Drawing_Line = true;//设置一个启动标志
Falg_Inputimage = false;
Series_Num = 0;
Point_Num = 0;
Flag_XAxisMin = false;
Flag_XAxisMax = false;
Flag_YAxisMin = false;
Flag_YAxisMax = false;
DataX = new System.Collections.ArrayList();
DataY = new System.Collections.ArrayList();
}
private void textBox_Step_TextChanged(object sender, EventArgs e)
{
if (button_Automatic.Enabled)
{
Series_Num = Series_Num - 1;
button_Automatic_Click(sender, e);
}
}
private void textBox_Threshold_TextChanged(object sender, EventArgs e)
{
if (button_Automatic.Enabled)
{
Series_Num = Series_Num - 1;
button_Automatic_Click(sender, e);
}
}
private void button_GetData2_Click(object sender, EventArgs e)
{
button_GetData_Click(sender, e);
}
//private void PictureBox_Data_MouseEnter(object sender, EventArgs e)
//{
// if (Falg_Inputimage)
// {
// System.Drawing.Graphics g = pictureBox_LocalRegion.CreateGraphics();
// //Cursor.Current = myCursor;
// // Rectangle sourceRectangle = new Rectangle(e.X - 10, e.Y - 10, 20, 20);
// //Rectangle destRectangle1 = new Rectangle(0, 0, 160, 150);
// // g.DrawImage(this.pictureBox_Data.Image, destRectangle1, sourceRectangle, GraphicsUnit.Pixel);
// //Bitmap bitmap = (Bitmap)this.pictureBox_Data.Image;
// //pictureBox_LocalRegion.Image = bitmap.Clone(sourceRectangle, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// int centerx = pictureBox_LocalRegion.Width / 2;
// int centery = pictureBox_LocalRegion.Height / 2;
// g.DrawEllipse(new Pen(Color.Red, 4), centerx - 2, centery - 2, 4, 4);
// }
// //throw new NotImplementedException();
//}
private void button_LableData_Click(object sender, EventArgs e)
{
drawing = true;
Point_Num = 0;
Series_Num = Series_Num+1;
DataX = new System.Collections.ArrayList();
DataY = new System.Collections.ArrayList();
button_GetData.Enabled = true;
}
//throw new System.NotImplementedException();
// }
//private void PictureBox_Data_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
//{
// //drawing = false;
// //throw new System.NotImplementedException();
//}
private void PictureBox_Data_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
drawing = false;
return;
}
currentPoint = new Point(e.X, e.Y);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmDraw);
//System.Drawing.Graphics g = pictureBox_Data.CreateGraphics();
if (drawing)
{
// System.Drawing.Graphics g = pictureBox_Data.CreateGraphics();
g.DrawEllipse(new Pen(Color.Red, 4), e.X-2, e.Y-2, 4, 4);
if (Point_Num >= 1 & Drawing_Line)
{
//Point currentPoint = new Point(e.X, e.Y);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
g.DrawLine(new Pen(Color.Blue, 1), formerpoint, currentPoint);
}
}
formerpoint = new Point(e.X, e.Y);
Point_Num = Point_Num + 1;
DataX.Add(e.X);
DataY.Add(e.Y);
// throw new System.NotImplementedException();
int RectR = 4;
if (Flag_XAxisMin)
{
XAxisMin.X = e.X;
XAxisMin.Y = e.Y;
Flag_XAxisMin = false;
g.DrawRectangle(new Pen(Color.Red, 2), e.X- RectR, e.Y- RectR, RectR * 2, RectR * 2);
button_Xaxismax.Enabled = true;
}
if (Flag_XAxisMax)
{
XAxisMax.X = e.X;
if (XAxisMin.Y==0)
{ XAxisMax.Y = e.Y; }
else
{ XAxisMax.Y = XAxisMin.Y; }
Flag_XAxisMax = false;
//System.Drawing.Graphics g = pictureBox_Data.CreateGraphics();
g.DrawRectangle(new Pen(Color.Red, 2), e.X- RectR, e.Y- RectR, RectR*2, RectR * 2);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
g.DrawLine(new Pen(Color.Red, 1), XAxisMin, XAxisMax);
button_Yaxismin.Enabled = true;
}
if (Flag_YAxisMin)
{
YAxisMin.X = e.X;
YAxisMin.Y = e.Y;
Flag_YAxisMin = false;
g.DrawRectangle(new Pen(Color.Blue, 2), e.X- RectR, e.Y- RectR, RectR * 2, RectR * 2);
button__Yaxismax.Enabled = true;
}
if (Flag_YAxisMax)
{
if (YAxisMin.X==0)
{ YAxisMax.X = e.X; }
else
{ YAxisMax.X = YAxisMin.X; }
YAxisMax.Y = e.Y;
Flag_YAxisMax = false;
// System.Drawing.Graphics g = pictureBox_Data.CreateGraphics();
g.DrawRectangle(new Pen(Color.Blue, 2), e.X- RectR, e.Y- RectR, RectR * 2, RectR * 2);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
g.DrawLine(new Pen(Color.Blue, 1), YAxisMin, YAxisMax);
button_LableData.Enabled = true;
}
if (Flag_PointStart)
{
button_End.Enabled = true;
srcColor = bmDraw.GetPixel(e.X, e.Y);
button_Start.BackColor = srcColor;
PointStart.X = e.X;
PointStart.Y = e.Y;
RGBStart[0] = srcColor.R;
RGBStart[1] = srcColor.G;
RGBStart[2] = srcColor.B;
g.DrawEllipse(new Pen(srcColor, 2), e.X - RectR, e.Y - RectR, RectR * 2, RectR * 2);
}
if (Flag_PointEnd)
{
button_Automatic.Enabled = true;
srcColor = bmDraw.GetPixel(e.X, e.Y);
PointEnd.X = e.X;
PointEnd.Y = e.Y;
button_End.BackColor = srcColor;
RGBEnd[0] = srcColor.R;
RGBEnd[1] = srcColor.G;
RGBEnd[2] = srcColor.B;
g.DrawEllipse(new Pen(srcColor, 2), e.X - RectR, e.Y - RectR, RectR * 2, RectR * 2);
}
g.Save();
//Dispose();
this.pictureBox_Data.Image =bmDraw;
}
private void button_GetData_Click(object sender, EventArgs e)
{
double XAxisMin_Real = double.Parse(textBox__Xaxismin.Text);
double XAxisMax_Real = double.Parse(textBox__Xaxismax.Text);
double YAxisMax_Real = double.Parse(textBox__Yaxismax.Text);
double YAxisMin_Real = double.Parse(textBox__Yaxismin.Text);
Point_Num = DataX.Count;
double[,] Data = new double[Point_Num, 2];
for (int i=0;i < Point_Num;i++)
{
Data[i, 0] = (Convert.ToDouble(DataX[i]) - XAxisMin.X) / (XAxisMax.X-XAxisMin.X)*(XAxisMax_Real - XAxisMin_Real) + XAxisMin_Real;
Data[i, 1] = (Convert.ToDouble(DataY[i]) - YAxisMin.Y) / (YAxisMax.Y - YAxisMin.Y) * (YAxisMax_Real - YAxisMin_Real) + YAxisMin_Real;
}
Microsoft.Office.Tools.Excel.Worksheet worksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
string s= "Serise" + Convert.ToString(Series_Num)+"_X";
((Excel.Range)worksheet.Cells[1, (Series_Num-1)*2+1]).Value2=s;
s = "Serise" + Convert.ToString(Series_Num)+"_Y";
((Excel.Range)worksheet.Cells[1, (Series_Num - 1) * 2 + 2]).Value2 =s;
Excel.Range c1 = (Excel.Range)worksheet.Cells[1+1, (Series_Num - 1) * 2 + 1];
Excel.Range c2 = (Excel.Range)worksheet.Cells[1+Point_Num, (Series_Num - 1) * 2 + 2];
Excel.Range range = worksheet.get_Range(c1, c2);
range.Value = Data;
drawing = false;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
Drawing_Line = false;
checkBox_Line.Checked = false;
}
private void button_ReadImage_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();//创建事例
string dir = Environment.GetFolderPath(Environment.SpecialFolder.Templates);//指定初始目录
dlg.InitialDirectory = dir;//指定初始目录
dlg.Title = "图表对话框";
dlg.ShowReadOnly = true;//以只读方式打开
dlg.ShowHelp = true;//显示帮助按钮 ///////
dlg.Filter = "图表.jpg|*.jpg|图表.tif|*.tif|图表.bmp|*.bmp|所有文件|*.*";//文件过滤器,指定打开文件类型
//dlg.ShowDialog();//打开对话框
//BMP 文件(*.bmp) | *.bmp | JPEG 文件(*.jpg, *.jpeg) | *.jpg, *.jpeg | PNG 文件(*.png) | *.png | GIF 文件(*.gif) | *.gif | TIFF 文件(*.tiff, *.tif) | *.tiff
//MessageBox.Show(dlg.Title);//打开消息
//dlg.Multiselect = true;//是否允许一次打开多个文件
dlg.Multiselect = false;//是否允许一次打开多个文件
// if (dlg.ShowDialog() == DialogResult.OK)
//{
if (dlg.CheckPathExists)//检查路径是否存在
{
if (dlg.CheckFileExists)//检查文件是否存在
{
// if (dlg.ValidateNames)//检查是否有效Win32文件名
// {
if (dlg.ShowDialog() == DialogResult.OK)
{
string s = dlg.FileNames[0];
// foreach (string s in dlg.FileNames)
//{ //string fileName = dlg.FileName;
//MessageBox.Show("打开文件:" + s);//打开消息对话框
bmDraw = new Bitmap(this.pictureBox_Data.Width, this.pictureBox_Data.Height);
Bitmap inputimage=(Bitmap)Image.FromFile(s);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmDraw);
double scale_width = Convert.ToDouble(bmDraw.Width) / inputimage.Width;
double scale_height = Convert.ToDouble(bmDraw.Height) / inputimage.Height;
double scale= scale_width;
if (scale_width > scale_height) scale = scale_height;
int centerx = bmDraw.Width / 2;
int centery = bmDraw.Height / 2;
int newWidth = Convert.ToInt32(inputimage.Width * scale / 2);
int newHeight = Convert.ToInt32(inputimage.Height * scale / 2);
Rectangle rg = new Rectangle(centerx- newWidth, centery- newHeight, newWidth *2, newHeight*2);
//将bm内rg所指定的区域绘制到bm1
g.DrawImage(inputimage, rg);
this.pictureBox_Data.Image = bmDraw;
//this.pictureBox_Data.Image = Image.FromFile(s);
Falg_Inputimage = true;
button_Xaxismin.Enabled = true;
// }
}
// }
}
}
}
}
}