diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index fc80e8b..5510671 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -63,7 +63,7 @@ namespace Camera this.picBoxCamera.Location = new System.Drawing.Point(0, 0); this.picBoxCamera.Name = "picBoxCamera"; this.picBoxCamera.Size = new System.Drawing.Size(700, 515); - this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; + this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal; this.picBoxCamera.TabIndex = 0; this.picBoxCamera.TabStop = false; this.picBoxCamera.Paint += new System.Windows.Forms.PaintEventHandler(this.PicBoxCamera_Paint); diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 2f8cc7a..cc27c4a 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -41,12 +41,21 @@ namespace Camera public void SetImage(Image image) { - if (picBoxCamera.Image != null) + try { - picBoxCamera.Image.Dispose(); + if (picBoxCamera.Image != null) + { + picBoxCamera.Image.Dispose(); + } + Bitmap newImage = new Bitmap(image.Width, image.Height); + using (Graphics g = Graphics.FromImage(newImage)) + { + g.DrawImage(image, 0, 0); + } + picBoxCamera.Image = newImage; + picBoxCamera.Invalidate(); } - picBoxCamera.Image = (Image)image.Clone(); - picBoxCamera.Invalidate(); + catch { } } public void SetConfigPath(string path) @@ -184,11 +193,20 @@ namespace Camera private void UpdateImage(Image image) { - if (picBoxCamera.Image != null) + try { - picBoxCamera.Image.Dispose(); + if (picBoxCamera.Image != null) + { + picBoxCamera.Image.Dispose(); + } + Bitmap newImage = new Bitmap(image.Width, image.Height); + using (Graphics g = Graphics.FromImage(newImage)) + { + g.DrawImage(image, 0, 0); + } + picBoxCamera.Image = newImage; } - picBoxCamera.Image = image; + catch { } } private void UpdateDataGridView() @@ -216,8 +234,16 @@ namespace Camera private void PicBoxCamera_Paint(object sender, PaintEventArgs e) { - Image currentImage = picBoxCamera.Image; - if (currentImage == null) return; + Image currentImage = null; + try + { + currentImage = picBoxCamera.Image; + if (currentImage == null) return; + } + catch + { + return; + } int imageWidth = 0, imageHeight = 0; bool imageValid = false; @@ -232,12 +258,17 @@ namespace Camera } catch { + return; } if (!imageValid) return; Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; + g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; + + Rectangle destRect = new Rectangle(0, 0, picBoxCamera.ClientSize.Width, picBoxCamera.ClientSize.Height); + g.DrawImage(currentImage, destRect, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel); float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth; float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight; @@ -498,7 +529,7 @@ namespace Camera { _camera.SetLedZone(newZone); } - picBoxCamera.Invalidate(); + picBoxCamera.Update(); } } else if (_isMoving) @@ -526,7 +557,7 @@ namespace Camera { _camera.SetLedZone(newZone); } - picBoxCamera.Invalidate(); + picBoxCamera.Update(); } } else diff --git a/Windows/CS/Framework4.0/Camera/Test/Form1.cs b/Windows/CS/Framework4.0/Camera/Test/Form1.cs index dcc2409..f8e0518 100644 --- a/Windows/CS/Framework4.0/Camera/Test/Form1.cs +++ b/Windows/CS/Framework4.0/Camera/Test/Form1.cs @@ -27,6 +27,7 @@ namespace Test private void Camera_ImageCaptured(object sender, ImageEventArgs e) { + System.Diagnostics.Debug.WriteLine("Form1: 收到图像事件"); if (InvokeRequired) { Invoke(new Action(UpdatePictureBox), e.Image); @@ -39,11 +40,10 @@ namespace Test private void UpdatePictureBox(Image image) { - if (pictureBox1.Image != null) - { - pictureBox1.Image.Dispose(); - } - pictureBox1.Image = image; + System.Diagnostics.Debug.WriteLine("Form1: 更新图像, Size=" + image.Width + "x" + image.Height); + Bitmap bitmap = new Bitmap(image); + pictureBox1.Image = bitmap; + pictureBox1.Refresh(); } private void button1_Click(object sender, EventArgs e) @@ -62,7 +62,14 @@ namespace Test private void button2_Click(object sender, EventArgs e) { - _camera.SetArea(); + _camera.SetConfigPath(@"D:\YeXian\EasyTest\EasyTest\bin\Debug\生产测试\TCM232V0.8_自动\Config"); + Setting settingForm = new Setting(); + settingForm.SetCamera(_camera); + if (_camera.GetCurrentImage() != null) + { + settingForm.SetImage(_camera.GetCurrentImage()); + } + settingForm.ShowDialog(); } protected override void OnFormClosing(FormClosingEventArgs e) diff --git a/Windows/CS/Framework4.0/Camera/Test/Form2.Designer.cs b/Windows/CS/Framework4.0/Camera/Test/Form2.Designer.cs new file mode 100644 index 0000000..6e94bb3 --- /dev/null +++ b/Windows/CS/Framework4.0/Camera/Test/Form2.Designer.cs @@ -0,0 +1,74 @@ +namespace Test +{ + partial class Form2 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.button1 = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.SuspendLayout(); + // + // pictureBox1 + // + this.pictureBox1.Location = new System.Drawing.Point(30, 50); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(454, 376); + this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; + this.pictureBox1.TabIndex = 0; + this.pictureBox1.TabStop = false; + // + // button1 + // + this.button1.Location = new System.Drawing.Point(569, 152); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(109, 49); + this.button1.TabIndex = 1; + this.button1.Text = "button1"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // Form2 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.button1); + this.Controls.Add(this.pictureBox1); + this.Name = "Form2"; + this.Text = "Form2"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.Button button1; + } +} \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Test/Form2.cs b/Windows/CS/Framework4.0/Camera/Test/Form2.cs new file mode 100644 index 0000000..d7cef39 --- /dev/null +++ b/Windows/CS/Framework4.0/Camera/Test/Form2.cs @@ -0,0 +1,195 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using System.Drawing.Imaging; + +namespace Test +{ + public partial class Form2 : Form + { + // 摄像头配置信息 + private const string IP = "192.168.100.60"; + private const string SNAPSHOT_URI = "/snapshot.jpg"; + private const string USER = "admin"; + private const string PASSWD = "Yexian.net.168"; + + // 保存路径 + private const string SAVE_DIRECTORY = @"D:\Projects"; + + private string _authorization; + + public Form2() + { + InitializeComponent(); + + // 确保保存目录存在 + if (!Directory.Exists(SAVE_DIRECTORY)) + { + Directory.CreateDirectory(SAVE_DIRECTORY); + } + } + + private void button1_Click(object sender, EventArgs e) + { + Image image = GetCameraImage(); + pictureBox1.Image = image; + + // 自动保存图像到D:\Projects目录 + SaveImageToProjects(image); + } + + /// + /// 将图像保存到D:\Projects目录 + /// + private void SaveImageToProjects(Image image) + { + try + { + if (image != null) + { + // 生成带时间戳的文件名 + string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff"); + string fileName = $"CameraSnapshot_{timestamp}.jpg"; + string filePath = Path.Combine(SAVE_DIRECTORY, fileName); + + // 保存图像 + image.Save(filePath, ImageFormat.Jpeg); + + MessageBox.Show($"图像已保存到:{filePath}", "保存成功", + MessageBoxButtons.OK, MessageBoxIcon.Information); + + System.Diagnostics.Debug.WriteLine($"图像保存成功:{filePath}"); + } + } + catch (Exception ex) + { + MessageBox.Show($"保存图像失败:{ex.Message}", "保存失败", + MessageBoxButtons.OK, MessageBoxIcon.Error); + System.Diagnostics.Debug.WriteLine($"图像保存失败:{ex.Message}"); + } + } + + /// + /// 手动保存当前显示的图像 + /// + public void SaveCurrentImage() + { + if (pictureBox1.Image != null) + { + SaveImageToProjects(pictureBox1.Image); + } + else + { + MessageBox.Show("没有可保存的图像", "提示", + MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + + private Bitmap GetCameraImage() + { + string url = "http://" + IP + SNAPSHOT_URI; + + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.Headers.Add("Cache-Control", "no-cache"); + request.Headers.Add("Pragma", "no-cache"); + + if (!string.IsNullOrEmpty(_authorization)) + { + request.Headers.Add("Authorization", _authorization); + } + + HttpWebResponse response = null; + try + { + response = (HttpWebResponse)request.GetResponse(); + } + catch (System.Net.WebException ex) + { + if (ex.Response != null && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Unauthorized) + { + response = (HttpWebResponse)ex.Response; + string authHeader = response.Headers["WWW-Authenticate"]; + response.Close(); + + string realm = ExtractAuthParam(authHeader, "realm"); + string nonce = ExtractAuthParam(authHeader, "nonce"); + + _authorization = GenerateDigestAuthorization(USER, PASSWD, realm, nonce, "GET", SNAPSHOT_URI); + + request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.Headers.Add("Authorization", _authorization); + response = (HttpWebResponse)request.GetResponse(); + } + else + { + throw; + } + } + + byte[] imageData; + using (Stream stream = response.GetResponseStream()) + using (MemoryStream memoryStream = new MemoryStream()) + { + stream.CopyTo(memoryStream); + imageData = memoryStream.ToArray(); + } + + return new Bitmap(new MemoryStream(imageData)); + } + + /// + /// 从WWW-Authenticate头中提取参数 + /// + private string ExtractAuthParam(string authHeader, string paramName) + { + Regex regex = new Regex(paramName + "=\"([^\"]+)\""); + Match match = regex.Match(authHeader); + if (match.Success) + { + return match.Groups[1].Value; + } + return string.Empty; + } + /// + /// 生成Digest认证的Authorization头 + /// + private string GenerateDigestAuthorization(string username, string password, string realm, string nonce, string method, string uri) + { + string ha1 = CalculateMD5(username + ":" + realm + ":" + password); + string ha2 = CalculateMD5(method + ":" + uri); + string response = CalculateMD5(ha1 + ":" + nonce + ":" + ha2); + + return string.Format("Digest username=\"{0}\", realm=\"{1}\", nonce=\"{2}\", uri=\"{3}\", response=\"{4}\"", + username, realm, nonce, uri, response); + } + /// + /// 计算MD5哈希值 + /// + private string CalculateMD5(string input) + { + using (MD5 md5 = MD5.Create()) + { + byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); + byte[] hashBytes = md5.ComputeHash(inputBytes); + + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + for (int i = 0; i < hashBytes.Length; i++) + { + sb.Append(hashBytes[i].ToString("x2")); + } + return sb.ToString(); + } + } + } +} diff --git a/Windows/CS/Framework4.0/Camera/Test/Form2.resx b/Windows/CS/Framework4.0/Camera/Test/Form2.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Windows/CS/Framework4.0/Camera/Test/Form2.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Test/Test.csproj b/Windows/CS/Framework4.0/Camera/Test/Test.csproj index 143893d..4b45671 100644 --- a/Windows/CS/Framework4.0/Camera/Test/Test.csproj +++ b/Windows/CS/Framework4.0/Camera/Test/Test.csproj @@ -1,4 +1,4 @@ - + @@ -56,11 +56,20 @@ Form1.cs + + Form + + + Form2.cs + Form1.cs + + Form2.cs + ResXFileCodeGenerator Resources.Designer.cs