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 ImageDisplayForm : Form { #region 控件字段 protected PictureBox pictureBox; #endregion #region 私有字段 private IContainer components = null; private Bitmap originalImage; private Bitmap markedImage; private Timer blinkTimer; private Timer autoUpdateTimer; private string currentName = ""; private string currentPosition = ""; 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(); SetCurrentName(Init.ImageName); // 初始化自动更新定时器 autoUpdateTimer = new Timer(); 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) { if (string.IsNullOrEmpty(name)) return; string imagesFolder = Path.Combine(Application.StartupPath, "images"); // 检查目录是否存在 if (!Directory.Exists(imagesFolder)) { MessageBox.Show($"图片目录不存在: {imagesFolder}"); return; } // 获取目录中所有文件 string[] files = Directory.GetFiles(imagesFolder); // 查找文件名以指定名称开头的文件 string imagePath = null; foreach (string file in files) { string fileName = Path.GetFileNameWithoutExtension(file); if (fileName.Equals(name, StringComparison.OrdinalIgnoreCase)) { imagePath = file; break; } } if (!string.IsNullOrEmpty(imagePath)) { try { // 停止闪烁定时器并重置状态 blinkTimer?.Stop(); isBlinking = false; // 加载新图片 originalImage = new Bitmap(imagePath); markedImage = new Bitmap(originalImage); pictureBox.Image = markedImage; } catch (Exception ex) { MessageBox.Show($"加载图片失败:{ex.Message}"); } } else { MessageBox.Show($"未找到名称为 {name} 的图片"); } } // 在图像上绘制标记 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 AutoUpdateTimer_Tick(object sender, EventArgs e) { // 获取当前位置变量 //string newPosition = GetCurrentPosition(); string newPosition = Init.ImageLocation.ToString(); if (newPosition != currentPosition || (string.IsNullOrEmpty(currentPosition) && !string.IsNullOrEmpty(newPosition))) { currentPosition = newPosition; // 如果名称和位置都有值,则查询坐标并标注 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 sQLiteHelper_position = new db.SQLiteHelper(); var positions = sQLiteHelper_position.Query(x => x.name == name && x.position == position).Data; if (positions != null && positions.Count > 0) { var pos = positions[0]; // 标记位置 if (originalImage != null) { isBlinking = true; // 设置为 true 后不再切换 currentRadius = MIN_RADIUS; // 重置半径 blinkTimer.Start(); DrawMark(pos.x, pos.y); } } } catch (Exception ex) { Console.WriteLine($"查询坐标失败:{ex.Message}"); } } #endregion #region 事件处理 // 闪烁定时器事件 private void BlinkTimer_Tick(object sender, EventArgs e) { SQLiteHelper sQLiteHelper_position = new db.SQLiteHelper(); 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) { blinkTimer?.Stop(); blinkTimer?.Dispose(); autoUpdateTimer?.Stop(); autoUpdateTimer?.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.pictureBox = new System.Windows.Forms.PictureBox(); this.blinkTimer = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // 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, 0); this.pictureBox.Margin = new System.Windows.Forms.Padding(6); this.pictureBox.Name = "pictureBox"; 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; // // blinkTimer // this.blinkTimer.Interval = 50; // 修改为50毫秒 this.blinkTimer.Tick += new System.EventHandler(this.BlinkTimer_Tick); // // ImageDisplayForm // this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1600, 1250); this.Controls.Add(this.pictureBox); this.Margin = new System.Windows.Forms.Padding(6); this.Name = "ImageDisplayForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "螺丝引导"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); } #endregion } }