C#图像,对象其他地方使用错误 [英] C# images, object in use elsewhere error

查看:594
本文介绍了C#图像,对象其他地方使用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#所以原谅我,如果我不解释这一点。

i am extremely new to C# so excuse me if i don't explain this well.

我取回我从我的电脑摄像头的图像,并显示沿他们在一个图片框,我为他们向编码JPEG文件,并将它们发送到一个共享的字典。这里是我的代码:

i'm retrieving images from my computer camera, and along with displaying them in a PictureBox, i'm encoding them to jpegs and sending them to a shared dictionary. here's my code:

void CurrentCamera_OnImageCaptured(object sender, CameraEventArgs e)
        {
            this.pictureBoxMe.Image = e.Image;

            if (myName != "" && Form1.PicSent)
            {
                SendPic sendP = new SendPic((Image)e.Image.Clone());
                new System.Threading.Thread(new System.Threading.ThreadStart(sendP.send)).Start();
            }

        }

 public class SendPic
        {
            Image im;
            public SendPic (Image im)
            {
                this.im = im;
            }
            public void send(){

                Form1.PicSent = false;
                var memoryStream = new MemoryStream();

                im.Save(memoryStream, ImageFormat.Jpeg);

                var byteArray = memoryStream.ToArray();

                Form1.sd["/" + myName + "/video"] = byteArray;

                memoryStream.Close();
                Form1.PicSent = true;

            }
        }



问题是,我是让对象是目前在其他地方使用。错误就行了:SendPic sendP((图)e.Image.Clone())=新SendPic;

the problem is that i'm getting the "Object is currently in use elsewhere." error on the line: SendPic sendP = new SendPic((Image)e.Image.Clone());

根据其他论坛的帖子我已经找到了,我已经改变它,以便将图像传递给线程,因此,它的一个克隆。但我仍然得到同样的错误(虽然现在坠毁前持续时间更长)。

based on other forum posts i've found, i already changed it so that the image is passed to the thread, and so that it's a clone. however i'm still getting the same error (though it lasts longer before crashing now).

我读一些有关锁定?如何实现,在这种情况下?或者是有别的东西,我需要做什么?

i read something about locking? how do i implement that in this case? or is there something else i need to do?

感谢。

推荐答案

它的行为就像OnImageCaptured方法的线程运行。这不是不可能的相机接口。设置断点,并使用调试器的调试+的Windows +线程窗口,看看有什么线程正在运行该代码。

It behaves as though the OnImageCaptured method runs on a thread. Which isn't unlikely for camera interfaces. Set a breakpoint and use the debugger's Debug + Windows + Threads window to see what thread is running this code.

失效模式是那么UI线程正在访问该图像与此工作线程调用克隆来绘制图片框,同时()。 GDI +不允许两个线程同时访问同一图像对象。这确实是片状,没有告诉在什么确切的时刻,当时的UI线程开始绘画。 PicSent是另一个事故等待发生。

The failure mode is then that the UI thread is accessing the image to paint the picture box, simultaneously with this worker thread calling Clone(). GDI+ does not permit two threads accessing the same image object at the same time. It would indeed be flaky, no telling at what exact moment in the time the UI thread starts painting. PicSent is another accident waiting to happen.

这篇关于C#图像,对象其他地方使用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆