1

Please I have problem in this program Error appear (Object is currently in use elsewhere)and (Bitmap region is locked)"if you are using the graphic object after the gethdc method call the releasehdc method" I am trying to detect faces using Viola Jones and extract features by SURF algorithm then make matching : Please how can i fix this error? the code is bellow: private void btnStartCam_Click(object sender, EventArgs e) { if (radWepCam.Checked == true)

            if (videoSource.IsRunning)
            {
                _worker = new BackgroundWorker();
                _worker.WorkerSupportsCancellation = true;

                _worker.DoWork += new DoWorkEventHandler((state, args) =>
                {


                    do
                    {
                        try
                        {
                            var cascade = new FaceHaarCascade();
                            var detector = new HaarObjectDetector(cascade, 40);

                            detector.SearchMode = ObjectDetectorSearchMode.Average;
                            detector.Suppression = 3;
                            detector.ScalingFactor = 1.25f;
                            detector.UseParallelProcessing = true;
                            detector.ScalingMode = ObjectDetectorScalingMode.SmallerToGreater;
                            detector.UseParallelProcessing = true;


                            Bitmap image = (Bitmap)pictureBox1.Image.Clone();


                            {

                                lock (image)
                                {
                                    faceObjects = detector.ProcessFrame(image).ToArray();

                                    FaceCount = 0;

                                    foreach (var face in faceObjects)
                                    {

                                        Crop crop = new Crop(face);
                                        newimage = new Bitmap(crop.Apply(image));
                                        face.Inflate(-10, -10);
                                        facearray[FaceCount] = newimage;
                                        pictureBox2.Image = newimage;
                                        FaceCount = FaceCount + 1;
                                    }
                                }

                            int FaceCount2 = FaceCount;
                            for (int r = 0; r < FaceCount2 ; r++)
                            {

                                Bitmap imsetres = facearray[r];
                                System.Drawing.Image imgg1 = imsetres;

                                Image<Gray, Byte> g_image_1 = new Image<Gray, byte>((Bitmap)imgg1);
                                int endj = listAllEmployee.Items.Count - 1;
                                for (int j = 0; j < endj; j++)
                                {
                                    int max = 0;
                                    Bitmap imsetres2 = facearrayDB[j];
                                    imsetres2.SetResolution(320, 280);
                                    System.Drawing.Image imgg2 = imsetres2;

                                    pictureBox3.Image = imgg2;
                                    Image<Gray, Byte> g_image_2 = new Image<Gray, byte>((Bitmap)imgg2);
                                    ImageFeature[] image_feacher_1 = detect1.DetectFeatures(g_image_1, null);
                                    ImageFeature[] image_feacher_2 = detect1.DetectFeatures(g_image_2, null);

                                    Features2DTracker traker = new Features2DTracker(image_feacher_1);
                                    Features2DTracker.MatchedImageFeature[] matching = traker.MatchFeature(image_feacher_2, 2, 50);

                                    matching = Features2DTracker.VoteForUniqueness(matching, 0.9);

                                    matching = Features2DTracker.VoteForSizeAndOrientation(matching, 1.5, 20);
                                    int machno = 0;

                                    foreach (Features2DTracker.MatchedImageFeature matchedFeature in matching)
                                    {
                                        machno = machno + 1;

                                    }
                                    if (max <= machno)
                                    {
                                        max = machno;
                                    }
                                    if (max >= 30)
                                    {
                                        Image<Gray, Byte> res = g_image_1.ConcateVertical(g_image_2);
                                        MessageBox.Show("Test Found " + j + "with matching points " + max);
                                        foreach (Features2DTracker.MatchedImageFeature matchedFeature in matching)
                                        {

                                            PointF f = matchedFeature.ObservedFeature.KeyPoint.Point;
                                            f.Y += g_image_1.Height;
                                            res.Draw(new LineSegment2DF(matchedFeature.SimilarFeatures[0].Feature.KeyPoint.Point, f), new Gray(0), 1);

                                        }
                                        ImageViewer Imagee = new ImageViewer(res);
                                        Imagee.ShowDialog();
                                    }
                                    matching = null;
                                    traker = null;
                                    image_feacher_1 = null;
                                    image_feacher_2 = null;

                                }
                            }

                        }


                        }


                        catch (Exception ex)
                        { 
                            MessageBox.Show("Error" + ex.Message);

                        }


                    }

                    while (!_worker.CancellationPending);
                });
                _worker.RunWorkerAsync();
                btnStartCam.Enabled = false;
                btnPauseCam.Enabled = true;
            }
  • 2
    It is a very unambiguous exception you get when you use a Bitmap object in more than one thread. Clearly the usage of pictureBox1.Image violates that rule, the lock does not accomplish anything since you can't lock PictureBox.OnPaint(). You must deep-copy the bitmap before you start the thread or derive your own class from PictureBox so you can add the lock. – Hans Passant May 27 '17 at 13:44
  • ken // should i unlock it ? – Hiraqi Hmaster May 27 '17 at 14:23
  • @HansPassant ....thanx but what do you mean by deep copy? – Hiraqi Hmaster May 28 '17 at 21:51
  • Just google ".net deep-copy bitmap" to find [this Q+A](https://stackoverflow.com/questions/12709360/whats-the-difference-between-bitmap-clone-and-new-bitmapbitmap). – Hans Passant May 28 '17 at 22:09

0 Answers0