image_text.xaml.cs
10.6 KB
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
using Acc.ImageBox;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SmartScan.SetControl.WPF
{
/// <summary>
/// image_text.xaml 的交互逻辑
/// </summary>
public partial class image_text : System.Windows.Controls.UserControl
{
public AccImageBox imageBox;
public image_text()
{
InitializeComponent();
// 初始化 imageBox
imageBox = new AccImageBox();
imageBox.Dock = DockStyle.Fill;
imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;
// 将 imageBox 添加到 WindowsFormsHost
imageViewer.Child = imageBox;
// 确保画布已初始化并且透明
if (centerPointsCanvas == null)
{
centerPointsCanvas = new Canvas();
centerPointsCanvas.Background = System.Windows.Media.Brushes.Transparent;
centerPointsCanvas.IsHitTestVisible = false;
}
Console.WriteLine("image_text 控件已初始化,画布已设置");
}
/// <summary>
/// 加载图像
/// </summary>
/// <param name="imagePath"></param>
public void LoadImage(string imagePath)
{
if (File.Exists(imagePath))
{
try
{
if (imageBox.Image != null)
{
imageBox.Image.Dispose();
}
imageBox.Image = System.Drawing.Image.FromFile(imagePath);
FitToScreen(); // 自动适应图像大小
}
catch (Exception ex)
{
// 处理错误
}
}
}
// 公共方法以适应屏幕 - 使其从 WinForms 可访问
public void FitToScreen()
{
if (imageBox?.Image != null)
{
imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;
imageBox.ZoomToFit();
imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Normal;
double currentZoom = imageBox.ZoomFactor;
imageBox.Zoom = (int)(currentZoom * 100);
}
}
// 清空中心点方法
public void ClearCenterPoints()
{
centerPointsCanvas.Children.Clear();
}
// 添加中心点方法
public void AddCenterPoint(double x, double y)
{
// 创建一个表示中心点的椭圆
Ellipse centerPoint = new Ellipse
{
Width = 20,
Height = 20,
Fill = System.Windows.Media.Brushes.Red,
Stroke = System.Windows.Media.Brushes.White,
StrokeThickness = 2
};
// 设置椭圆位置,使其中心在指定坐标
Canvas.SetLeft(centerPoint, x - 5);
Canvas.SetTop(centerPoint, y - 5);
// 添加到画布
centerPointsCanvas.Children.Add(centerPoint);
// 可选:添加十字线或其他标记以更清晰地标识中心点
Line horizontalLine = new Line
{
X1 = x - 10,
Y1 = y,
X2 = x + 10,
Y2 = y,
Stroke = System.Windows.Media.Brushes.White,
StrokeThickness = 1
};
Line verticalLine = new Line
{
X1 = x,
Y1 = y - 10,
X2 = x,
Y2 = y + 10,
Stroke = System.Windows.Media.Brushes.White,
StrokeThickness = 1
};
centerPointsCanvas.Children.Add(horizontalLine);
centerPointsCanvas.Children.Add(verticalLine);
}
// 在 image_text 类中添加
public void DrawCenterPointsOnImage(System.Drawing.PointF[] centers)
{
if (imageBox == null || imageBox.Image == null || centers == null || centers.Length == 0)
return;
// 存储中心点数据
_centers = centers;
// 创建一个事件处理程序来自定义绘制
imageBox.Paint -= ImageBox_Paint;
imageBox.Paint += ImageBox_Paint;
// 强制重绘
imageBox.Invalidate();
}
private System.Drawing.PointF[] _centers;
// 自定义绘制事件处理程序
private void ImageBox_Paint(object sender, PaintEventArgs e)
{
if (_centers == null || _centers.Length == 0 || imageBox == null || imageBox.Image == null)
return;
return;
// 获取图像和显示的关系
double zoomFactor = imageBox.ZoomFactor;
System.Drawing.Rectangle viewport = imageBox.GetImageViewPort();
// 设置高质量绘制
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//int circleRadius = 10; // 原来是15,这里调大到20
// 基础尺寸
float baseCircleRadius = 15;
// 平衡缩放 - 使用平方根或对数来减缓缩放效果
float adjustedZoomFactor = (float)Math.Sqrt(zoomFactor); // 或使用 Math.Log10(zoomFactor * 10)
// 计算圆半径 (平衡缩放效果)
float circleRadius = baseCircleRadius * adjustedZoomFactor;
// 设置合理的限制
circleRadius = Math.Max(10, Math.Min(circleRadius, 40));
// 计算字体大小
float fontSize = 12 * adjustedZoomFactor;
fontSize = Math.Max(8, Math.Min(fontSize, 20));
// 准备绘制工具
using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Transparent, 1))
using (System.Drawing.SolidBrush fillBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Transparent))
using (System.Drawing.SolidBrush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
using (System.Drawing.Font font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold))
{
for (int i = 0; i < _centers.Length; i++)
{
// 计算屏幕坐标
float screenX = (_centers[i].X * (float)zoomFactor) + viewport.X;
float screenY = (_centers[i].Y * (float)zoomFactor) + viewport.Y;
// 绘制圆形背景\
float diameter = circleRadius * 2;
e.Graphics.FillEllipse(fillBrush, screenX - circleRadius, screenY - circleRadius, diameter, diameter);
e.Graphics.DrawEllipse(pen, screenX - circleRadius, screenY - circleRadius, diameter, diameter);
// 绘制数字标记
string number = (i + 1).ToString();
System.Drawing.SizeF textSize = e.Graphics.MeasureString(number, font);
// 计算文本位置使其居中
float textX = screenX - (textSize.Width / 2);
float textY = screenY - (textSize.Height / 2);
e.Graphics.DrawString(number, font, textBrush, textX, textY);
Console.WriteLine($"绘制数字标记 {i + 1} 在位置 ({screenX}, {screenY})");
}
}
}
#region 按钮事件
/// <summary>
/// 放大按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ZoomInButton_Click(object sender, RoutedEventArgs e)
{
if (imageBox?.Image != null)
{
// 减小缩放比例,减少20%
double currentZoom = imageBox.ZoomFactor;
double newZoom = currentZoom * 1.2;
imageBox.Zoom = (int)(newZoom * 100); // 假设Zoom属性是百分比形式
}
}
// 缩小按钮事件处理程序
private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
{
if (imageBox?.Image != null)
{
// 减小缩放比例,减少20%
double currentZoom = imageBox.ZoomFactor;
double newZoom = currentZoom * 0.8;
imageBox.Zoom = (int)(newZoom * 100); // 假设Zoom属性是百分比形式
}
}
// 适应屏幕按钮事件处理程序
private void FitToScreenButton_Click(object sender, RoutedEventArgs e)
{
if (imageBox?.Image != null)
{
// 使用SizeMode属性设置图像适应控件
imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;
imageBox.ZoomToFit();
// 然后切换回Normal模式,以便后续手动缩放
imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Normal;
// 保存当前缩放值,以便在Normal模式下保持相同的显示大小
double currentZoom = imageBox.ZoomFactor;
imageBox.Zoom = (int)(currentZoom * 100);
}
}
// 旋转按钮事件处理程序
private void RotateButton_Click(object sender, RoutedEventArgs e)
{
if (imageBox?.Image != null)
{
try
{
// 创建一个新的临时图像来旋转
Bitmap originalImage = new Bitmap(imageBox.Image);
originalImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
// 替换当前图像
System.Drawing.Image oldImage = imageBox.Image;
imageBox.Image = originalImage;
// 释放旧图像
oldImage.Dispose();
}
catch (Exception ex)
{
//MessageBox.Show($"旋转图片时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
#endregion
}
}