gpt4 book ai didi

c# - 为什么 PictureBox.Invalidate() 会导致程序崩溃? System.AccessViolationException异常

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:41 26 4
gpt4 key购买 nike

所以我用 C# 写了一个 Windows 窗体应用程序来打开视频,在 pictureBox_display 中显示视频,并允许用户在 picturebox 上定义(绘制)边界框。唯一的问题是,我可以非常有预见性地通过简单地导致程序抛出 System.AccessViolationException 并崩溃:1)打开视频(任何视频)2) 在图片框上单击并拖动我的鼠标几秒钟。

这是一些代码:

VideoCapture VC;
bool IsMouseDown = false;
bool MouseMoved = false;
string VideoFileName;


private void OpenFileButton_Click(object sender, EventArgs e) {
OpenFileDialog OFD = new OpenFileDialog();
if (OFD.ShowDialog() == DialogResult.OK) {
FrameNumber = 0;
VideoFileName = OFD.FileName;
VC = new VideoCapture(VideoFileName);

if (VC.Width<=0 || VC.Height<=0) {
System.Windows.Forms.MessageBox.Show("ERROR: Please load a valid video file");
return;
}


// Initialize picturebox dimensions according to image
double W_H_ratio = Convert.ToDouble(VC.Width) / VC.Height;
if (W_H_ratio >= (double)(5 / 4)) {
pictureBox_display.Width = Math.Min(VC.Width, 800);
pictureBox_display.Height = Math.Min(VC.Height, (int)((1 / W_H_ratio )* 800));
}
else {
pictureBox_display.Height = Math.Min(VC.Height, 640);
pictureBox_display.Width = Math.Min(VC.Width, (int)(W_H_ratio * 640));
}

Mat tempimg = GetMat(FrameNumber);
Console.WriteLine("Size of tempimg: " + tempimg.Size);
pictureBox_display.Image = tempimg.Bitmap;
}
}

private Mat GetMat(int someFrameNumber) {

try {
Mat m = new Mat();
VC.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.PosFrames, someFrameNumber);
VC.Read(m);

Mat resized = new Mat();
CvInvoke.ResizeForFrame(m, resized, pictureBox_display.Size);
m.Dispose();
return resized;
}

catch {
Mat m = new Mat();
System.Windows.Forms.MessageBox.Show("ERROR: Cannot read video frame");
return m;
}
}

private void pictureBox_display_MouseDown(object sender, MouseEventArgs e) {
if (VideoFileName != null && checkBox1.Checked == false && VideoPlaying == false) {
IsMouseDown = true;
MouseMoved = false;
ThereIsNewSelection = true;
StartLocation = e.Location;
Console.WriteLine(StartLocation);
}
}


// Constantly alter the end location of the mouse to provide the coordinates for the rectangle
private void pictureBox_display_MouseMove(object sender, MouseEventArgs e) {
if (IsMouseDown == true) {
MouseMoved = true;

EndLocation = e.Location;
pictureBox_display.Invalidate();
}
}

private void pictureBox_display_Paint(object sender, PaintEventArgs e) {
if (!VideoPlaying && ThereIsNewSelection) {
e.Graphics.DrawRectangle(Pens.Red, GetRectangle());
}
}

我知道这肯定与 Invalidate() 有关,因为我已经逐行注释了代码,只有在有 Invalidate() 时它才会崩溃。谁能告诉我可能导致问题的原因是什么?

如果有用,这里是堆栈跟踪:

at System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height) at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect) at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

最佳答案

您需要克隆(深层复制)位图,如下所示:

B.Clone(new Rectangle(0, 0, B.Width, B.Height), B.PixelFormat)

关于c# - 为什么 PictureBox.Invalidate() 会导致程序崩溃? System.AccessViolationException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com