0

Last line which sets the picturebox's.image property gives me an error, error is

Cannot implicitly convert type 'Emgu.CV.Image' to 'System.Drawing.Image'

I have tried to use toBitmap() but it said there was no such a thing which belongs to imageToShow. Any help would be great..

   Image<Bgr, byte> source = new Image<Bgr, byte>(@"C:\\Users\\ereno\\OneDrive\\Masaüstü\\TRButton.png"); 
                    Image<Bgr, byte> template = new Image<Bgr, byte>(@"C:\\Users\\ereno\\OneDrive\\Masaüstü\\Screenshot_2.png"); 
                    Image<Bgr, byte> imageToShow = source.Copy();

                    using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
                    {
                        double[] minValues, maxValues;
                        Point[] minLocations, maxLocations;
                        result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);


                        if (maxValues[0] > 0.9)
                        {

                            Rectangle match = new Rectangle(maxLocations[0], template.Size);
                            imageToShow.Draw(match, new Bgr(Color.Red), 3);
                        }
                    }


                    pictureBox1.Image = imageToShow;
Mustafa
  • 977
  • 3
  • 12
  • 25

1 Answers1

0

If you want to show the image that you read in inside of your WPF, than you could use an image source, which is found in Xaml. You should convert your image object to a bitmap, then a bitmap to an image source object, as that would be needed to display the image. This stack form here goes into good detail on how to do this.

Convert your image object to a bitmap first.

//Convert the image object to a bitmap
Bitmap img = image.ToBitmap();

//Using the method below, convert the bitmap to an imagesource
imgOutput.Source = ImageSourceFromBitmap(img);

The function that I called above can be achieved from the code below.

//If you get 'dllimport unknown'-, then add 'using System.Runtime.InteropServices;'
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);

public ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
    var handle = bmp.GetHbitmap();
    try
    {
        return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
    finally { DeleteObject(handle); }               
}

You will have to add some references to your project, but this method has worked for me in the past.

Aaron Jones
  • 1,140
  • 1
  • 9
  • 26