当前位置: 首页 > news >正文

C#小桌面程序调试出错,如何解决??

🏆本文收录于《CSDN问答解惑-专业版》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!

问题描述

  C#小桌面程序调试出错,我用VS Community 2022编一个小的桌面程序,C#语言,代码调试时出错,只有Form1.cs和Form1.designer.cs文件,总共约200行代码,我怀疑是否引用的EmguCV设置有问题,还是别的问题,如何解决??

这是Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Numerics;namespace ImageMeasurementApp
{public partial class Form1 : Form{private Image<Bgr, byte> _originalImage; // 原始图像private Image<Gray, byte> _grayImage; // 灰度图像private Image<Gray, byte> _thresholdImage; // 二值化图像private Image<Bgr, byte> _resultImage; // 结果图像,用于显示轮廓和尺寸private VectorOf<VectorOf<Point>> _contours; // 所有检测到的轮廓public Form1(){InitializeComponent(); // 初始化组件,由设计器生成pictureBox1.Image = new Image<Bgr, byte>(pictureBox1.Width, pictureBox1.Height); // 初始化 pictureBox1 的 Image 属性pictureBox1.MouseMove += PictureBox1_MouseMove;}// 加载图像按钮的事件处理器private void LoadImageButton_Click(object sender, EventArgs e){using (OpenFileDialog openFileDialog = new OpenFileDialog()){openFileDialog.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";if (openFileDialog.ShowDialog() == DialogResult.OK){_originalImage = new Image<Bgr, byte>(openFileDialog.FileName);_grayImage = _originalImage.Convert<Gray, byte>();pictureBox1.Image = _originalImage;}}}// 二值化处理按钮的事件处理器private void ThresholdImageButton_Click(object sender, EventArgs e){if (_grayImage != null){_thresholdImage = _grayImage.ThresholdBinary(new Gray(128), new Gray(255));pictureBox1.Image = _thresholdImage;}}// 检测轮廓按钮的事件处理器private void DetectContoursButton_Click(object sender, EventArgs e){if (_thresholdImage != null){_resultImage = _originalImage.Copy();_contours = new VectorOf<VectorOf<Point>>();CvInvoke.FindContours(_thresholdImage, _contours, IntPtr.Zero, RetrType.List, ChainApproxMethod.ChainApproxSimple);foreach (VectorOf<Point> contour in _contours){if (contour.Size > 100){Rectangle boundingRect = CvInvoke.BoundingRectangle(contour);CvInvoke.DrawContours(_resultImage, contour, new MCvScalar(0, 0, 255), 2);CvInvoke.Rectangle(_resultImage, boundingRect, new MCvScalar(0, 255, 0), 2);double area = CvInvoke.ContourArea(contour);CvInvoke.PutText(_resultImage, $"Area: {area:F2}", new Point(boundingRect.X, boundingRect.Y - 10), FontFace.HersheySimplex, 0.5, new MCvScalar(0, 255, 0), 1);double perimeter = CvInvoke.ArcLength(contour, true);CvInvoke.PutText(_resultImage, $"Perimeter: {perimeter:F2}", new Point(boundingRect.X, boundingRect.Y + boundingRect.Height + 20), FontFace.HersheySimplex, 0.5, new MCvScalar(0, 255, 0), 1);}}pictureBox1.Image = _resultImage;}}// 图像框的MouseMove事件处理器,用于显示鼠标位置上的轮廓尺寸信息private void PictureBox1_MouseMove(object sender, MouseEventArgs e){if (_resultImage != null && _contours != null){var point = new Point(e.X, e.Y);var pixel = _resultImage[point.Y, point.X];if (pixel.B > 0 || pixel.G > 0 || pixel.R > 0){foreach (VectorOf<Point> contour in _contours){Rectangle boundingRect = CvInvoke.BoundingRectangle(contour);if (boundingRect.Contains(point)){statusLabel.Text = $"X: {e.X}, Y: {e.Y}, Width: {boundingRect.Width}, Height: {boundingRect.Height}, Area: {CvInvoke.ContourArea(contour):F2}";break;}}}else{statusLabel.Text = "";}}}}
}---------------------------这是Form1.designer.csnamespace ImageMeasurementApp
{partial class Form1{private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){// 释放组件容器中托管的所有资源components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.components = new System.ComponentModel.Container();this.pictureBox1 = new Emgu.CV.UI.ImageBox(); // 图像显示控件this.loadImageButton = new System.Windows.Forms.Button(); // 加载图像按钮this.thresholdImageButton = new System.Windows.Forms.Button(); // 二值化处理按钮this.detectContoursButton = new System.Windows.Forms.Button(); // 检测轮廓按钮this.statusLabel = new System.Windows.Forms.Label(); // 状态标签,显示尺寸信息((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();this.SuspendLayout();// // pictureBox1// this.pictureBox1.Location = new System.Drawing.Point(12, 12); // 设置图像显示控件的位置this.pictureBox1.Name = "pictureBox1"; // 控件名称this.pictureBox1.Size = new System.Drawing.Size(600, 400); // 设置控件的大小this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; // 图像缩放模式this.pictureBox1.TabIndex = 0; // 控件的索引号this.pictureBox1.TabStop = false; // 是否允许键盘焦点// // loadImageButton// this.loadImageButton.Location = new System.Drawing.Point(618, 12); // 设置按钮的位置this.loadImageButton.Name = "loadImageButton"; // 按钮名称this.loadImageButton.Size = new System.Drawing.Size(150, 23); // 设置按钮的大小this.loadImageButton.TabIndex = 1; // 按钮的索引号this.loadImageButton.Text = "Load Image"; // 按钮文本this.loadImageButton.UseVisualStyleBackColor = true; // 是否使用默认样式this.loadImageButton.Click += new System.EventHandler(this.LoadImageButton_Click); // 点击事件处理器// // thresholdImageButton// this.thresholdImageButton.Location = new System.Drawing.Point(618, 41); // 设置按钮的位置this.thresholdImageButton.Name = "thresholdImageButton"; // 按钮名称this.thresholdImageButton.Size = new System.Drawing.Size(150, 23); // 设置按钮的大小this.thresholdImageButton.TabIndex = 2; // 按钮的索引号this.thresholdImageButton.Text = "Threshold Image"; // 按钮文本this.thresholdImageButton.UseVisualStyleBackColor = true; // 是否使用默认样式this.thresholdImageButton.Click += new System.EventHandler(this.ThresholdImageButton_Click); // 点击事件处理器// // detectContoursButton// this.detectContoursButton.Location = new System.Drawing.Point(618, 70); // 设置按钮的位置this.detectContoursButton.Name = "detectContoursButton"; // 按钮名称this.detectContoursButton.Size = new System.Drawing.Size(150, 23); // 设置按钮的大小this.detectContoursButton.TabIndex = 3; // 按钮的索引号this.detectContoursButton.Text = "Detect Contours"; // 按钮文本this.detectContoursButton.UseVisualStyleBackColor = true; // 是否使用默认样式this.detectContoursButton.Click += new System.EventHandler(this.DetectContoursButton_Click); // 点击事件处理器// // statusLabel// this.statusLabel.AutoSize = true; // 标签自动调整大小this.statusLabel.Location = new System.Drawing.Point(618, 104); // 设置标签的位置this.statusLabel.Name = "statusLabel"; // 标签名称this.statusLabel.Size = new System.Drawing.Size(35, 13); // 设置标签的大小this.statusLabel.TabIndex = 4; // 标签的索引号this.statusLabel.Text = "label1"; // 初始标签文本// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); // 设置字体大小比例this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; // 字体缩放模式this.ClientSize = new System.Drawing.Size(800, 450); // 设置窗体的大小this.Controls.Add(this.statusLabel); // 添加控件到窗体this.Controls.Add(this.detectContoursButton);this.Controls.Add(this.thresholdImageButton);this.Controls.Add(this.loadImageButton);this.Controls.Add(this.pictureBox1);this.Name = "Form1"; // 窗体名称this.Text = "Image Measurement App"; // 窗体标题((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); // 初始化图像显示控件this.ResumeLayout(false); // 重新布局控件this.PerformLayout(); // 更新控件的布局和外观}#endregionprivate Emgu.CV.UI.ImageBox pictureBox1; // 图像显示控件实例private System.Windows.Forms.Button loadImageButton; // 加载图像按钮实例private System.Windows.Forms.Button thresholdImageButton; // 二值化处理按钮实例private System.Windows.Forms.Button detectContoursButton; // 检测轮廓按钮实例private System.Windows.Forms.Label statusLabel; // 状态标签实例}
}

这是错误页面:

如上问题有来自我自身项目开发,有的收集网站,有的来自读者…如有侵权,立马删除。

解决方案

  如下是上述问题的解决方案,仅供参考:

  根据你提供的代码和描述,以下是一些可能的问题和解决方案:

1. EmguCV引用问题

  • 检查EmguCV库是否正确安装:确保EmguCV库已经正确安装在你的项目中。可以通过Visual Studio的“解决方案资源管理器”检查“引用”部分。
  • 检查命名空间:你的代码中已经正确引用了EmguCV的命名空间,但确保没有拼写错误或遗漏。

2. 图像显示问题

  • ImageBox类型问题:你的代码中使用了Emgu.CV.UI.ImageBox作为图像显示控件。确保这个控件已经在项目中正确注册和使用。
  • 初始化问题:在Form1构造函数中,你初始化了pictureBox1.Image,但此时pictureBox1可能还没有完全初始化。可以考虑将初始化代码移动到Form1_Load事件中。

3. 事件处理器问题

  • 事件绑定问题:检查事件处理器是否正确绑定到相应的按钮。可以通过断点调试检查事件是否被触发。

4. 异常处理

  • 添加异常处理:在关键操作(如文件加载、图像处理)周围添加异常处理,以便捕获和调试错误。例如:
     try{_originalImage = new Image<Bgr, byte>(openFileDialog.FileName);}catch (Exception ex){MessageBox.Show("Error loading image: " + ex.Message);}

5. 调试技巧

  • 断点调试:使用Visual Studio的断点调试功能,逐步执行代码,检查变量的值和程序的执行流程。
  • 输出调试信息:在关键位置添加Console.WriteLine()语句,输出调试信息,帮助定位问题。

6. 代码逻辑问题

  • 检查逻辑流程:确保代码逻辑正确,比如图像加载、二值化处理、轮廓检测的顺序和条件。

7. 资源释放问题

  • 确保资源释放:在Dispose方法中,确保释放所有使用的资源,特别是图像资源。

8. UI线程问题

  • UI线程操作:确保所有UI操作都在主线程中执行。可以使用Invoke方法来确保在UI线程中更新控件。

9. 编译错误

  • 检查编译错误:查看Visual Studio的“错误列表”窗口,检查是否有编译错误或警告。

10. 运行时错误

  • 检查运行时错误:如果程序在运行时崩溃,检查是否有未处理的异常或资源访问冲突。

11. 图像路径问题

  • 检查图像路径:确保加载的图像路径正确,文件存在且可访问。

12. 版本兼容性问题

  • 检查EmguCV版本:确保使用的EmguCV版本与你的项目兼容。

  如果这些建议仍然无法解决问题,建议提供更详细的错误信息或截图,以便进一步分析。你也可以尝试创建一个简单的示例项目,逐步添加功能,以便逐步定位问题。

  希望如上措施及解决方案能够帮到有需要的你。

  PS:如若遇到采纳如下方案还是未解决的同学,希望不要抱怨&&急躁,毕竟影响因素众多,我写出来也是希望能够尽最大努力帮助到同类似问题的小伙伴,即把你未解决或者产生新Bug黏贴在评论区,我们大家一起来努力,一起帮你看看,可以不咯。

  若有对当前Bug有与如下提供的方法不一致,有个不情之请,希望你能把你的新思路或新方法分享到评论区,一起学习,目的就是帮助更多所需要的同学,正所谓「赠人玫瑰,手留余香」。

☀️写在最后

  ok,以上就是我这期的Bug修复内容啦,如果还想查找更多解决方案,你可以看看我专门收集Bug及提供解决方案的专栏《CSDN问答解惑-专业版》,都是实战中碰到的Bug,希望对你有所帮助。到此,咱们下期拜拜。

码字不易,如果这篇文章对你有所帮助,帮忙给 bug菌 来个一键三连(关注、点赞、收藏) ,您的支持就是我坚持写作分享知识点传播技术的最大动力。

同时也推荐大家关注我的硬核公众号:「猿圈奇妙屋」 ;以第一手学习bug菌的首发干货,不仅能学习更多技术硬货,还可白嫖最新BAT大厂面试真题、4000G Pdf技术书籍、万份简历/PPT模板、技术文章Markdown文档等海量资料,你想要的我都有!

📣关于我

我是bug菌,CSDN | 掘金 | InfoQ | 51CTO | 华为云 | 阿里云 | 腾讯云 等社区博客专家,C站博客之星Top30,华为云2023年度十佳博主,掘金多年度人气作者Top40,掘金等各大社区平台签约作者,51CTO年度博主Top12,掘金/InfoQ/51CTO等社区优质创作者;全网粉丝合计 30w+;硬核微信公众号「猿圈奇妙屋」,欢迎你的加入!免费白嫖最新BAT互联网公司面试真题、4000G PDF电子书籍、简历模板等海量资料,你想要的我都有,关键是你不来拿哇。


http://www.lryc.cn/news/422811.html

相关文章:

  • Seatunnel Mysql数据同步到Mysql
  • Java Web —— 第五天(请求响应1)
  • 【LLMOps】手摸手教你把 Dify 接入微信生态
  • Ftrans文件摆渡方案:重塑文件传输与管控的科技先锋
  • LaTeX中的除号表示方法详解
  • DID、DID文档、VC、VP分别是什么 有什么关系
  • 网络安全应急响应
  • Qt数据和视图分离——中MCV和MVVM
  • 重定义变量类型:如#define FLOAT float和typedef float FLOAT的区别
  • Qt 使用阿里矢量图标库
  • 仓颉语言运行时轻量化实践
  • 深入理解Python中的subprocess模块
  • 从零开始搭建 EMQX 集群压测框架
  • ArkUI基本介绍
  • vue2+OpenLayers 天地图上打点并且显示相关的信息(2)
  • c++继承(二)
  • 低代码开发的崛起:机遇与挑战
  • Json-JacksonUtils工具类
  • svn客户端装完后没有svn.exe
  • TinyWebserver的复现与改进(4):主线程的具体实现
  • DaemonSet 不能帮助我们做什么事情?
  • 开源模型应用落地-LangChain高阶-记忆组件-RedisChatMessageHistory正确使用(八)
  • 解决Openwrt 串口默认是没有密码的方法
  • 【vue讲解:v-model 之 lazy、number、trim、与后端交互、小电影案例】
  • ECCV 2024 | 南洋理工三维数字人生成新范式:结构扩散模型
  • 2024.8.13-算法学习(原创+转载)
  • beautifulsoup的简单使用
  • 【Python】Jupyter Notebook的安装及简单使用
  • 中国自动驾驶出租车冲击网约车市场
  • 解决浏览器书签同步问题,极空间部署开源免费的跨平台书签同步工具『xBrowserSync』