Search code examples
c#image-processingaforge

How to convert an Unmanaged Image to a managed bitmap image?


Hi I need to convert an unmanaged image to a managed bitmap image which I need to display on a picturebox but I it seems to throw an exception saying "object reference not set to an instance of an object". Does anyone have an idea about it? I have commented the line which throws the exception.

for (int i = 0; i < characters.Length; i++)
                    {
                        area = characters[i].Area;
                        UnmanagedImage numer = characters[i].Image;
                        System.Drawing.Image plateImage = numer.ToManagedImage();//Exception
                        numberplate = new Bitmap(new Bitmap(plateImage));
                        pictureBox2.Image = numberplate;
                        pictureBox2.Refresh();                            
                    }

I am using Aforge.net framework with C#

UPDATE

for (int i = 0; i < characters.Length; i++)
                    {
                        area = characters[i].Area;
                        Bitmap numer = characters[i].Image.ToManagedImage();
                        //System.Drawing.Image plateImage = numer.ToManagedImage();
                        //numberplate = new Bitmap(new Bitmap(plateImage));
                        pictureBox2.Image = numberplate;
                        pictureBox2.Refresh();                            
                    }

Solution

  • I found this code on the Aforge.Net forums and it seemed to work.

                        BlobCounterBase bc = new BlobCounter();
                        bc.FilterBlobs = true;
                        bc.MinHeight = 5;
                        bc.MinWidth = 5;
    
                        bc.ProcessImage(numberplate);
                        Blob[] blobs = bc.GetObjectsInformation();
                        MessageBox.Show(bc.ObjectsCount.ToString());
                        for (int i = 0, n = blobs.Length; i < n; i++)
                        {
                            if (blobs.Length > 0)
                            {
    
                                bc.ExtractBlobsImage(numberplate, blobs[i], true);
    
                                Bitmap copy = blobs[i].Image.ToManagedImage();
                                pictureBox2.Image = numberplate;
                                pictureBox2.Refresh();
                            }
                        }