Ledrunning/FacialDetection

The Convert function in the ValueConverter always throws an exception

BeautyOfCoding opened this issue · 2 comments

May I ask why occasionally an exception is thrown when converting Bitmap to BitmapImage in the BitmapSourceConvert class? The exception content is: System. Runtime. InteropServices. ExternalException (0x80004005): A general error occurred in GDI+.
Strangely, if I abandon the MVVM mode and directly rewrite the Convert function in BitmapSourceConvert in MainWindow. CS file, it will not throw an exception:

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
                Mat mat = new Mat();
                bool b = capture.Retrieve(mat, 0);
                if (b && !mat.IsEmpty)
                {
                      this.Dispatcher.Invoke(new Action(() =>
                      {
                          image1.Source = Bitmap2BitmapImage(mat.Bitmap);
                      }));
                }
        }
        private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
        {
            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    bitmap.Save(memoryStream, ImageFormat.Bmp);
                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    image.StreamSource = new MemoryStream(memoryStream.ToArray());
                    image.EndInit();
                    return image;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                return new BitmapImage();
            }
        }

I want to know why.

Thank you for your question! Have you solved this problem? If so, could you please share this and make a pull request? If not, I'll try to analyze it...
Can you also describe under what circumstances the exception occurs?
Try the release build in my repository. It includes all necessary files and libs. Also, please check the project structure and output files in the bin folder.

Screenshot 2024-01-01 225246
Screenshot 2024-01-01 225324

I'm closing the issue as there was no response and I don't get this error.