页面布局修改

This commit is contained in:
gf zhou committed 2025-04-04 23:28:46 +08:00
1 parent d20b935213
commit 752e1f37b6
9 files changed
+1636 -1044

No files matched your search

+86 -337
View File
@@ -10,28 +10,29 @@ namespace WindowsFormsApp1
{
public partial class ImageDisplayForm : Form
{
#region 控件字段
protected PictureBox pictureBox;
protected Button btnLoadImage;
protected TextBox txtX;
protected TextBox txtY;
protected TextBox txtName;
protected TextBox txtPosition;
protected Button btnSave;
protected Panel panel;
protected Label lblX;
protected Label lblY;
protected Label lblName;
protected Label lblPosition;
#endregion
#region 私有字段
private IContainer components = null;
private Bitmap originalImage;
private Bitmap markedImage;
private Timer blinkTimer;
private bool isBlinking = false;
private IContainer components = null;
private Timer autoUpdateTimer;
private string currentName = "";
private string currentPosition = "";
private Timer autoUpdateTimer;
private bool isBlinking = false;
// 圆形标记相关参数
private float currentRadius = 20f;
private const float MIN_RADIUS = 20f;
private const float MAX_RADIUS = 50f;
private bool isExpanding = true;
private const float RADIUS_CHANGE_SPEED = 2f;
#endregion
#region 构造函数和初始化
public ImageDisplayForm()
{
InitializeComponent();
@@ -39,18 +40,22 @@ namespace WindowsFormsApp1
// 初始化自动更新定时器
autoUpdateTimer = new Timer();
autoUpdateTimer.Interval = 10000;
autoUpdateTimer.Interval = 1000;
autoUpdateTimer.Tick += new EventHandler(AutoUpdateTimer_Tick);
autoUpdateTimer.Start();
}
#endregion
#region 公共方法
// 设置当前名称的方法,可以从外部调用
public void SetCurrentName(string name)
{
currentName = name;
LoadImageByName(name);
}
#endregion
#region 图像加载和处理
// 根据名称加载图片
private void LoadImageByName(string name)
{
@@ -93,7 +98,6 @@ namespace WindowsFormsApp1
originalImage = new Bitmap(imagePath);
markedImage = new Bitmap(originalImage);
pictureBox.Image = markedImage;
txtName.Text = name;
}
catch (Exception ex)
{
@@ -106,80 +110,12 @@ namespace WindowsFormsApp1
}
}
// 自动更新定时器事件
private void AutoUpdateTimer_Tick(object sender, EventArgs e)
{
// 这里应该从某处获取当前位置变量
string newPosition = Init.ImageLocation.ToString();
if (newPosition != currentPosition || (string.IsNullOrEmpty(currentPosition) && !string.IsNullOrEmpty(newPosition)))
{
currentPosition = newPosition;
txtPosition.Text = currentPosition;
// 如果名称和位置都有值,则查询坐标并标注
if (!string.IsNullOrEmpty(currentName) && !string.IsNullOrEmpty(currentPosition))
{
QueryAndMarkPosition(currentName, currentPosition);
}
}
}
// 获取当前位置的方法(需要根据实际情况实现)
private string GetCurrentPosition()
{
// 生成1-10的随机数
Random random = new Random();
int position = random.Next(1, 11); // 1到10之间的随机数
return position.ToString();
}
// 根据名称和位置查询坐标并标注
private void QueryAndMarkPosition(string name, string position)
{
try
{
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
var positions = sQLiteHelper_position.Query(x => x.name == name
&& x.position == position).Data;
if (positions != null && positions.Count > 0)
{
var pos = positions[0];
// 更新坐标文本框
txtX.Text = pos.x.ToString();
txtY.Text = pos.y.ToString();
// 标记位置
if (originalImage != null)
{
isBlinking = true; // 设置为 true 后不再切换
currentRadius = MIN_RADIUS; // 重置半径
blinkTimer.Start();
DrawMark(pos.x, pos.y);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查询坐标失败:{ex.Message}");
}
}
private float breathScale = 1.0f; // 添加这个字段到类的顶部
private const float MIN_SCALE = 0.6f;
private const float MAX_SCALE = 1.4f;
private bool scaleIncreasing = true;
// 修改半径相关的常量
private float currentRadius = 20f; // 增大初始半径
private const float MIN_RADIUS = 20f; // 最小半径
private const float MAX_RADIUS = 50f; // 增大最大半径
private bool isExpanding = true;
private const float RADIUS_CHANGE_SPEED = 2f;
// 在图像上绘制标记
private void DrawMark(int x, int y)
{
if (originalImage == null)
return;
markedImage = new Bitmap(originalImage);
using (Graphics g = Graphics.FromImage(markedImage))
{
@@ -189,7 +125,7 @@ namespace WindowsFormsApp1
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 只绘制白色圆
using (Brush whiteBrush = new SolidBrush(Color.Yellow))
using (Brush whiteBrush = new SolidBrush(Color.White))
{
g.FillEllipse(whiteBrush,
x - currentRadius,
@@ -221,139 +157,86 @@ namespace WindowsFormsApp1
}
pictureBox.Image = markedImage;
}
#endregion
private void BlinkTimer_Tick(object sender, EventArgs e)
#region 数据查询和更新
// 自动更新定时器事件
private void AutoUpdateTimer_Tick(object sender, EventArgs e)
{
// 移除 isBlinking 切换,保持为 true
if (!int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
return;
DrawMark(x, y);
}
// 获取当前位置变量
//string newPosition = GetCurrentPosition();
string newPosition = Init.ImageLocation.ToString();
private void PictureBox_Click(object sender, EventArgs e)
{
if (originalImage == null || !(e is MouseEventArgs me))
return;
// 获取图片在 PictureBox 中的实际显示尺寸和位置
float ratioX = (float)originalImage.Width / pictureBox.ClientSize.Width;
float ratioY = (float)originalImage.Height / pictureBox.ClientSize.Height;
float ratio = Math.Max(ratioX, ratioY);
float imageWidth = originalImage.Width / ratio;
float imageHeight = originalImage.Height / ratio;
// 计算图片在 PictureBox 中的显示位置
float imageX = (pictureBox.ClientSize.Width - imageWidth) / 2;
float imageY = (pictureBox.ClientSize.Height - imageHeight) / 2;
// 将点击坐标转换为图片实际坐标
int x = (int)((me.X - imageX) * ratio);
int y = (int)((me.Y - imageY) * ratio);
// 确保坐标在图片范围内
if (x >= 0 && x < originalImage.Width && y >= 0 && y < originalImage.Height)
if (newPosition != currentPosition || (string.IsNullOrEmpty(currentPosition) && !string.IsNullOrEmpty(newPosition)))
{
txtX.Text = x.ToString();
txtY.Text = y.ToString();
BtnMark_Click(sender, e); // 自动标记位置
}
}
currentPosition = newPosition;
private void BtnLoadImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp|所有文件|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
// 如果名称和位置都有值,则查询坐标并标注
if (!string.IsNullOrEmpty(currentName) && !string.IsNullOrEmpty(currentPosition))
{
// 停止闪烁定时器并重置状态
blinkTimer?.Stop();
isBlinking = false;
originalImage = new Bitmap(openFileDialog.FileName);
markedImage = new Bitmap(originalImage);
pictureBox.Image = markedImage;
QueryAndMarkPosition(currentName, currentPosition);
}
}
}
private void BtnMark_Click(object sender, EventArgs e)
// 获取当前位置的方法(需要根据实际情况实现)
private string GetCurrentPosition()
{
if (markedImage == null || !int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
{
MessageBox.Show("请先加载图片并输入有效的坐标值");
return;
}
DrawMark(x, y);
isBlinking = true;
blinkTimer.Start();
// 生成1-10的随机数
Random random = new Random();
int position = random.Next(1, 11); // 1到10之间的随机数
return position.ToString();
}
private void BtnSave_Click(object sender, EventArgs e)
// 根据名称和位置查询坐标并标注
private void QueryAndMarkPosition(string name, string position)
{
if (string.IsNullOrWhiteSpace(txtName.Text) ||
string.IsNullOrWhiteSpace(txtPosition.Text) ||
string.IsNullOrWhiteSpace(txtX.Text) ||
string.IsNullOrWhiteSpace(txtY.Text))
{
MessageBox.Show("请填写完整信息");
return;
}
// 添加坐标值验证
if (!int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
{
MessageBox.Show("坐标值必须是有效的数字");
return;
}
// 检查图片是否已加载
if (originalImage == null)
{
MessageBox.Show("请先加载图片");
return;
}
// 检查坐标是否在图片范围内
if (x <= 0 || y <= 0 || x >= originalImage.Width || y >= originalImage.Height)
{
MessageBox.Show($"坐标值超出范围!\n有效范围:X(1-{originalImage.Width}) Y(1-{originalImage.Height})");
return;
}
try
{
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
t_imageposition position = new t_imageposition
{
name = txtName.Text,
position = txtPosition.Text,
x = int.Parse(txtX.Text),
y = int.Parse(txtY.Text),
createtime = DateTime.Now
};
var positions = sQLiteHelper_position.Query(x => x.name == name
&& x.position == position).Data;
var result = sQLiteHelper_position.Insert(position);
if (result.Success)
if (positions != null && positions.Count > 0)
{
MessageBox.Show("保存成功!");
// 清空输入框
//txtName.Text = "";
txtPosition.Text = "";
}
else
{
MessageBox.Show($"保存失败");
var pos = positions[0];
// 标记位置
if (originalImage != null)
{
isBlinking = true; // 设置为 true 后不再切换
currentRadius = MIN_RADIUS; // 重置半径
blinkTimer.Start();
DrawMark(pos.x, pos.y);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"保存失败:{ex.Message}");
Console.WriteLine($"查询坐标失败:{ex.Message}");
}
}
#endregion
#region 事件处理
// 闪烁定时器事件
private void BlinkTimer_Tick(object sender, EventArgs e)
{
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
var positions = sQLiteHelper_position.Query(x => x.name == currentName
&& x.position == currentPosition).Data;
if (positions != null && positions.Count > 0)
{
var pos = positions[0];
DrawMark(pos.x, pos.y);
}
}
// 移除了 BtnLoadImage_Click 事件处理程序
#endregion
#region 资源释放
protected override void Dispose(bool disposing)
{
if (disposing)
@@ -368,163 +251,33 @@ namespace WindowsFormsApp1
}
base.Dispose(disposing);
}
#endregion
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panel = new System.Windows.Forms.Panel();
this.btnLoadImage = new System.Windows.Forms.Button();
this.txtX = new System.Windows.Forms.TextBox();
this.txtY = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtPosition = new System.Windows.Forms.TextBox();
this.btnSave = new System.Windows.Forms.Button();
this.lblX = new System.Windows.Forms.Label();
this.lblY = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblPosition = new System.Windows.Forms.Label();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.blinkTimer = new System.Windows.Forms.Timer(this.components);
this.panel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// panel
//
this.panel.Controls.Add(this.btnLoadImage);
this.panel.Controls.Add(this.txtX);
this.panel.Controls.Add(this.txtY);
this.panel.Controls.Add(this.txtName);
this.panel.Controls.Add(this.txtPosition);
this.panel.Controls.Add(this.btnSave);
this.panel.Controls.Add(this.lblX);
this.panel.Controls.Add(this.lblY);
this.panel.Controls.Add(this.lblName);
this.panel.Controls.Add(this.lblPosition);
this.panel.Dock = System.Windows.Forms.DockStyle.Top;
this.panel.Location = new System.Drawing.Point(0, 0);
this.panel.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(1600, 125);
this.panel.TabIndex = 0;
//
// btnLoadImage
//
this.btnLoadImage.Location = new System.Drawing.Point(20, 21);
this.btnLoadImage.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.btnLoadImage.Name = "btnLoadImage";
this.btnLoadImage.Size = new System.Drawing.Size(200, 62);
this.btnLoadImage.TabIndex = 0;
this.btnLoadImage.Text = "加载图片";
this.btnLoadImage.UseVisualStyleBackColor = true;
this.btnLoadImage.Click += new System.EventHandler(this.BtnLoadImage_Click);
//
// txtX
//
this.txtX.Location = new System.Drawing.Point(240, 21);
this.txtX.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.txtX.Name = "txtX";
this.txtX.Size = new System.Drawing.Size(116, 31);
this.txtX.TabIndex = 1;
this.txtX.Text = "0";
//
// txtY
//
this.txtY.Location = new System.Drawing.Point(380, 21);
this.txtY.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.txtY.Name = "txtY";
this.txtY.Size = new System.Drawing.Size(116, 31);
this.txtY.TabIndex = 2;
this.txtY.Text = "0";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(740, 21);
this.txtName.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(196, 31);
this.txtName.TabIndex = 4;
//
// txtPosition
//
this.txtPosition.Location = new System.Drawing.Point(960, 21);
this.txtPosition.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.txtPosition.Name = "txtPosition";
this.txtPosition.Size = new System.Drawing.Size(196, 31);
this.txtPosition.TabIndex = 5;
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(1180, 21);
this.btnSave.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(200, 62);
this.btnSave.TabIndex = 6;
this.btnSave.Text = "保存";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.BtnSave_Click);
//
// lblX
//
this.lblX.AutoSize = true;
this.lblX.ForeColor = System.Drawing.Color.Gray;
this.lblX.Location = new System.Drawing.Point(240, 83);
this.lblX.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblX.Name = "lblX";
this.lblX.Size = new System.Drawing.Size(68, 25);
this.lblX.TabIndex = 7;
this.lblX.Text = "X坐标";
//
// lblY
//
this.lblY.AutoSize = true;
this.lblY.ForeColor = System.Drawing.Color.Gray;
this.lblY.Location = new System.Drawing.Point(380, 83);
this.lblY.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblY.Name = "lblY";
this.lblY.Size = new System.Drawing.Size(69, 25);
this.lblY.TabIndex = 8;
this.lblY.Text = "Y坐标";
//
// lblName
//
this.lblName.AutoSize = true;
this.lblName.ForeColor = System.Drawing.Color.Gray;
this.lblName.Location = new System.Drawing.Point(740, 83);
this.lblName.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(117, 25);
this.lblName.TabIndex = 9;
this.lblName.Text = "请输入名称";
//
// lblPosition
//
this.lblPosition.AutoSize = true;
this.lblPosition.ForeColor = System.Drawing.Color.Gray;
this.lblPosition.Location = new System.Drawing.Point(960, 83);
this.lblPosition.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblPosition.Name = "lblPosition";
this.lblPosition.Size = new System.Drawing.Size(117, 25);
this.lblPosition.TabIndex = 10;
this.lblPosition.Text = "请输入位置";
//
// pictureBox
//
this.pictureBox.BackColor = System.Drawing.Color.LightGray;
this.pictureBox.Cursor = System.Windows.Forms.Cursors.Cross;
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 125);
this.pictureBox.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(6);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(1600, 1125);
this.pictureBox.Size = new System.Drawing.Size(1600, 1250);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
this.pictureBox.Click += new System.EventHandler(this.PictureBox_Click);
//
// blinkTimer
//
this.blinkTimer.Interval = 50; // 修改为50毫秒
this.blinkTimer.Tick += new System.EventHandler(this.BlinkTimer_Tick);
//
@@ -534,16 +287,12 @@ namespace WindowsFormsApp1
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1600, 1250);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.panel);
this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
this.Margin = new System.Windows.Forms.Padding(6);
this.Name = "ImageDisplayForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "图片展示";
this.panel.ResumeLayout(false);
this.panel.PerformLayout();
this.Text = "螺丝引导";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
@@ -0,0 +1,475 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using WindowsFormsApp1.mod;
using WindowsFormsApp1.db;
namespace WindowsFormsApp1
{
public partial class SimpleImageDisplayForm : Form
{
#region 控件字段
protected PictureBox pictureBox;
protected Button btnLoadImage;
protected TextBox txtX;
protected TextBox txtY;
protected TextBox txtName;
protected TextBox txtPosition;
protected Button btnSave;
protected Panel panel;
protected Label lblX;
protected Label lblY;
protected Label lblName;
protected Label lblPosition;
#endregion
#region 私有字段
private IContainer components = null;
private Bitmap originalImage;
private Bitmap markedImage;
private Timer blinkTimer;
private bool isBlinking = false;
// 圆形标记相关参数
private float currentRadius = 20f;
private const float MIN_RADIUS = 20f;
private const float MAX_RADIUS = 50f;
private bool isExpanding = true;
private const float RADIUS_CHANGE_SPEED = 2f;
#endregion
#region 构造函数和初始化
public SimpleImageDisplayForm()
{
InitializeComponent();
}
#endregion
#region 图像加载和处理
// 在图像上绘制标记
private void DrawMark(int x, int y)
{
if (originalImage == null)
return;
markedImage = new Bitmap(originalImage);
using (Graphics g = Graphics.FromImage(markedImage))
{
if (isBlinking)
{
// 设置高质量绘图模式
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 只绘制白色圆
using (Brush whiteBrush = new SolidBrush(Color.White))
{
g.FillEllipse(whiteBrush,
x - currentRadius,
y - currentRadius,
currentRadius * 2,
currentRadius * 2);
}
// 更新半径
if (isExpanding)
{
currentRadius += RADIUS_CHANGE_SPEED;
if (currentRadius >= MAX_RADIUS)
{
currentRadius = MAX_RADIUS;
isExpanding = false;
}
}
else
{
currentRadius -= RADIUS_CHANGE_SPEED;
if (currentRadius <= MIN_RADIUS)
{
currentRadius = MIN_RADIUS;
isExpanding = true;
}
}
}
}
pictureBox.Image = markedImage;
}
#endregion
#region 事件处理
// 闪烁定时器事件
private void BlinkTimer_Tick(object sender, EventArgs e)
{
if (!int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
return;
DrawMark(x, y);
}
// 图片点击事件
private void PictureBox_Click(object sender, EventArgs e)
{
if (originalImage == null || !(e is MouseEventArgs me))
return;
// 获取图片在 PictureBox 中的实际显示尺寸和位置
float ratioX = (float)originalImage.Width / pictureBox.ClientSize.Width;
float ratioY = (float)originalImage.Height / pictureBox.ClientSize.Height;
float ratio = Math.Max(ratioX, ratioY);
float imageWidth = originalImage.Width / ratio;
float imageHeight = originalImage.Height / ratio;
// 计算图片在 PictureBox 中的显示位置
float imageX = (pictureBox.ClientSize.Width - imageWidth) / 2;
float imageY = (pictureBox.ClientSize.Height - imageHeight) / 2;
// 将点击坐标转换为图片实际坐标
int x = (int)((me.X - imageX) * ratio);
int y = (int)((me.Y - imageY) * ratio);
// 确保坐标在图片范围内
if (x >= 0 && x < originalImage.Width && y >= 0 && y < originalImage.Height)
{
txtX.Text = x.ToString();
txtY.Text = y.ToString();
MarkPosition(); // 自动标记位置
}
}
// 标记位置
private void MarkPosition()
{
if (markedImage == null || !int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
{
MessageBox.Show("请先加载图片并输入有效的坐标值");
return;
}
DrawMark(x, y);
isBlinking = true;
blinkTimer.Start();
}
// 加载图片按钮事件
private void BtnLoadImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp|所有文件|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// 停止闪烁定时器并重置状态
blinkTimer?.Stop();
isBlinking = false;
originalImage = new Bitmap(openFileDialog.FileName);
markedImage = new Bitmap(originalImage);
pictureBox.Image = markedImage;
}
}
}
// 保存按钮事件
private void BtnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtName.Text) ||
string.IsNullOrWhiteSpace(txtPosition.Text) ||
string.IsNullOrWhiteSpace(txtX.Text) ||
string.IsNullOrWhiteSpace(txtY.Text))
{
MessageBox.Show("请填写完整信息");
return;
}
// 添加坐标值验证
if (!int.TryParse(txtX.Text, out int x) || !int.TryParse(txtY.Text, out int y))
{
MessageBox.Show("坐标值必须是有效的数字");
return;
}
// 检查图片是否已加载
if (originalImage == null)
{
MessageBox.Show("请先加载图片");
return;
}
// 检查坐标是否在图片范围内
if (x < 0 || y < 0 || x >= originalImage.Width || y >= originalImage.Height)
{
MessageBox.Show($"坐标值超出范围!\n有效范围:X(0-{originalImage.Width-1}) Y(0-{originalImage.Height-1})");
return;
}
try
{
// 保存数据到数据库
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
// 检查是否存在相同名称和位置的记录
var existingPositions = sQLiteHelper_position.Query(a =>
a.name == txtName.Text &&
a.position == txtPosition.Text).Data;
t_imageposition position = new t_imageposition
{
name = txtName.Text,
position = txtPosition.Text,
x = int.Parse(txtX.Text),
y = int.Parse(txtY.Text),
createtime = DateTime.Now
};
bool success = false;
if (existingPositions != null && existingPositions.Count > 0)
{
// 更新现有记录
position.id = existingPositions[0].id;
var result = sQLiteHelper_position.Update(position);
success = result.Success;
}
else
{
// 插入新记录
var result = sQLiteHelper_position.Insert(position);
success = result.Success;
}
// 保存图片到images文件夹
if (success)
{
SaveImageToFolder(txtName.Text);
// 只清空position的值
txtPosition.Text = "";
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败");
}
}
catch (Exception ex)
{
MessageBox.Show($"保存失败:{ex.Message}");
}
}
// 保存图片到images文件夹
private void SaveImageToFolder(string name)
{
string imagesFolder = Path.Combine(Application.StartupPath, "images");
// 确保目录存在
if (!Directory.Exists(imagesFolder))
{
Directory.CreateDirectory(imagesFolder);
}
string imagePath = Path.Combine(imagesFolder, $"{name}.png");
// 检查文件是否已存在,如果存在则不覆盖
if (!File.Exists(imagePath))
{
originalImage.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
#endregion
#region 资源释放
protected override void Dispose(bool disposing)
{
if (disposing)
{
blinkTimer?.Stop();
blinkTimer?.Dispose();
originalImage?.Dispose();
markedImage?.Dispose();
components?.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panel = new System.Windows.Forms.Panel();
this.btnLoadImage = new System.Windows.Forms.Button();
this.txtX = new System.Windows.Forms.TextBox();
this.txtY = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtPosition = new System.Windows.Forms.TextBox();
this.btnSave = new System.Windows.Forms.Button();
this.lblX = new System.Windows.Forms.Label();
this.lblY = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblPosition = new System.Windows.Forms.Label();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.blinkTimer = new System.Windows.Forms.Timer(this.components);
this.panel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// panel
//
this.panel.Controls.Add(this.btnLoadImage);
this.panel.Controls.Add(this.txtX);
this.panel.Controls.Add(this.txtY);
this.panel.Controls.Add(this.txtName);
this.panel.Controls.Add(this.txtPosition);
this.panel.Controls.Add(this.btnSave);
this.panel.Controls.Add(this.lblX);
this.panel.Controls.Add(this.lblY);
this.panel.Controls.Add(this.lblName);
this.panel.Controls.Add(this.lblPosition);
this.panel.Dock = System.Windows.Forms.DockStyle.Top;
this.panel.Location = new System.Drawing.Point(0, 0);
this.panel.Margin = new System.Windows.Forms.Padding(6);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(1600, 120);
this.panel.TabIndex = 0;
//
// btnLoadImage
//
this.btnLoadImage.Location = new System.Drawing.Point(20, 20);
this.btnLoadImage.Margin = new System.Windows.Forms.Padding(6);
this.btnLoadImage.Name = "btnLoadImage";
this.btnLoadImage.Size = new System.Drawing.Size(200, 60);
this.btnLoadImage.TabIndex = 0;
this.btnLoadImage.Text = "加载图片";
this.btnLoadImage.UseVisualStyleBackColor = true;
this.btnLoadImage.Click += new System.EventHandler(this.BtnLoadImage_Click);
//
// txtX
//
this.txtX.Location = new System.Drawing.Point(240, 20);
this.txtX.Margin = new System.Windows.Forms.Padding(6);
this.txtX.Name = "txtX";
this.txtX.Size = new System.Drawing.Size(116, 35);
this.txtX.TabIndex = 1;
this.txtX.Text = "0";
//
// txtY
//
this.txtY.Location = new System.Drawing.Point(380, 20);
this.txtY.Margin = new System.Windows.Forms.Padding(6);
this.txtY.Name = "txtY";
this.txtY.Size = new System.Drawing.Size(116, 35);
this.txtY.TabIndex = 2;
this.txtY.Text = "0";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(519, 20);
this.txtName.Margin = new System.Windows.Forms.Padding(6);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(196, 35);
this.txtName.TabIndex = 4;
//
// txtPosition
//
this.txtPosition.Location = new System.Drawing.Point(744, 20);
this.txtPosition.Margin = new System.Windows.Forms.Padding(6);
this.txtPosition.Name = "txtPosition";
this.txtPosition.Size = new System.Drawing.Size(196, 35);
this.txtPosition.TabIndex = 5;
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(964, 14);
this.btnSave.Margin = new System.Windows.Forms.Padding(6);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(200, 60);
this.btnSave.TabIndex = 6;
this.btnSave.Text = "保存";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.BtnSave_Click);
//
// lblX
//
this.lblX.AutoSize = true;
this.lblX.ForeColor = System.Drawing.Color.Gray;
this.lblX.Location = new System.Drawing.Point(240, 80);
this.lblX.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblX.Name = "lblX";
this.lblX.Size = new System.Drawing.Size(70, 24);
this.lblX.TabIndex = 7;
this.lblX.Text = "X坐标";
//
// lblY
//
this.lblY.AutoSize = true;
this.lblY.ForeColor = System.Drawing.Color.Gray;
this.lblY.Location = new System.Drawing.Point(380, 80);
this.lblY.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblY.Name = "lblY";
this.lblY.Size = new System.Drawing.Size(70, 24);
this.lblY.TabIndex = 8;
this.lblY.Text = "Y坐标";
//
// lblName
//
this.lblName.AutoSize = true;
this.lblName.ForeColor = System.Drawing.Color.Gray;
this.lblName.Location = new System.Drawing.Point(515, 80);
this.lblName.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(130, 24);
this.lblName.TabIndex = 9;
this.lblName.Text = "请输入名称";
//
// lblPosition
//
this.lblPosition.AutoSize = true;
this.lblPosition.ForeColor = System.Drawing.Color.Gray;
this.lblPosition.Location = new System.Drawing.Point(740, 80);
this.lblPosition.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
this.lblPosition.Name = "lblPosition";
this.lblPosition.Size = new System.Drawing.Size(130, 24);
this.lblPosition.TabIndex = 10;
this.lblPosition.Text = "请输入位置";
//
// pictureBox
//
this.pictureBox.BackColor = System.Drawing.Color.LightGray;
this.pictureBox.Cursor = System.Windows.Forms.Cursors.Cross;
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 120);
this.pictureBox.Margin = new System.Windows.Forms.Padding(6);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(1600, 1080);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
this.pictureBox.Click += new System.EventHandler(this.PictureBox_Click);
//
// blinkTimer
//
this.blinkTimer.Interval = 50;
this.blinkTimer.Tick += new System.EventHandler(this.BlinkTimer_Tick);
//
// SimpleImageDisplayForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1600, 1200);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.panel);
this.Margin = new System.Windows.Forms.Padding(6);
this.Name = "SimpleImageDisplayForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "图片标注";
this.panel.ResumeLayout(false);
this.panel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
}
}
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="blinkTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>