【发布时间】:2020-10-27 17:12:39
【问题描述】:
根据在线示例,我使用 zXing 和 aForge 库在 c# 中编写了 qrcode 网络摄像头阅读器。
我在 C# 中遇到了 System.Windows.Forms.Timer 的非常奇怪的行为:我已将它放在寡妇窗体上,启用它,设置间隔 1 秒并附加刻度事件处理程序。
一切似乎都正常工作,但是当我将窗口调整(放大)到特定大小时,或者如果我将窗口设为全屏,时间滴答事件会停止触发。当我将窗口从全屏变为正常大小时,或者当我减小窗口大小时,计时器会自动重新启动。
我正在使用以下版本的 Visual Studio:
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
namespace QrCodeWebCamReader
{
public partial class Form1 : Form
{
FilterInfoCollection filterInfoColletion;
VideoCaptureDevice captureDevice;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
filterInfoColletion = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo filterInfo in filterInfoColletion)
{
cboDevice.Items.Add(filterInfo.Name);
}
cboDevice.SelectedIndex = 0;
}
private void BtnStart_Click(object sender, EventArgs e)
{
captureDevice = new VideoCaptureDevice(filterInfoColletion[cboDevice.SelectedIndex].MonikerString);
captureDevice.NewFrame += CaptureDevice_NewFrame;
captureDevice.Start();
timer1.Start();
}
private void CaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// pictureBox.Image = (Bitmap)eventArgs.Frame.Clone();
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
using (Graphics gr = Graphics.FromImage(img))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Rectangle cropArea = new Rectangle(img.Width / 2 - 150, img.Height / 2 - 150, 300, 300);
gr.DrawRectangle(new Pen(Color.Red,5), cropArea);
}
pictureBox.Image = (Bitmap)img.Clone();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
if (captureDevice.IsRunning)
{
captureDevice.Stop();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("fungus");
System.Diagnostics.Debug.WriteLine(pictureBox.Image);
if(pictureBox.Image != null)
{
Rectangle cropArea = new Rectangle(pictureBox.Image.Width / 2 - 150, pictureBox.Image.Height / 2 - 150, 300, 300);
BarcodeReader barcodeReader = new BarcodeReader();
Result result = barcodeReader.Decode((Bitmap)((Bitmap)pictureBox.Image).Clone(cropArea, pictureBox.Image.PixelFormat));
picCropped.Image = (Bitmap)((Bitmap)pictureBox.Image).Clone(cropArea, pictureBox.Image.PixelFormat);
if (result != null)
{
txtQRCode.Text += result.ToString();
Console.Beep();
}
}
}
}
}
我的项目配置为使用 net framework 2.0
此行为的原因可能是什么,我该如何防止它发生?
谢谢
编辑:关于这种行为,我能想到的唯一逻辑是编译器可能正在对生成的可执行代码进行某种优化/混淆/最小化? c# windows forms应用程序的Visual Studio社区版如何开启优化/混淆/最小化?
更新:仅当正在捕获视频时才会发生此行为。如果未捕获视频,则计时器不会停止。
【问题讨论】:
-
如果注释掉
Timer1_Tick()中除调试写入行之外的所有代码,它是否仍然停止工作? (我假设您连接了一个调试器来查看调试写入行输出。) -
Visual Studio 实际上并不运行您的代码(如果您愿意,您甚至可以使用记事本编写所有内容并通过命令行编译它而无需安装 Visual Studio)。您最好告诉我们您正在使用的框架的版本,因为它包含 WinForms 库。
-
@MatthewWatson 我已经列出了所有行,除了: System.Diagnostics.Debug.WriteLine("fungunsi");但行为是一样的
-
@John 我正在使用框架 2.0
-
“也许编译器正在做某种......” - 你在找错误的树。您的代码中有一个错误,某处。或者可能在您正在使用的库中(执行“帧捕获”的东西)。不幸的是,您未能提供minimal reproducible example,因此任何人都无法尝试重现您的问题以帮助您解决问题。请改进问题。