Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am getting Error CS1061  'Image<Bgr, byte>' does not contain a definition for 'Bitmap' and no accessible extension method 'Bitmap' accepting a first argument of type 'Image<Bgr, byte>' could be found (are you missing a using directive or an assembly reference?)


What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;



namespace objectdetection
{
  
    public partial class Form1 : Form
    {
        Image<Bgr, byte> imgInput;
        private Bitmap img;
        bool show = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                imgInput = new Image<Bgr, byte>(ofd.FileName);
                pictureBox1.Image = imgInput.Bitmap;  //<- here i am getting error
            }

        }

        private async void button2_Click(object sender, EventArgs e)
        {
            Image<Gray, byte> imgout = imgInput.Convert<Gray, byte>().Not().ThresholdBinary(new Gray(50), new Gray(255));
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            Mat hier = new Mat();

            CvInvoke.FindContours(imgout, contours, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

            if (contours.Size > 0)
            {
                show = true;

                for (int i = 0; i < 10; i++)
                {
                    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
                    imgInput.ROI = rect;
                    img = imgInput.Copy().Bitmap; //<- here i am getting error
                    this.Invalidate();
                    await Task.Delay(500);
                }
                show = false;
            }
            return;
            Dictionary<int, double> dict = new Dictionary<int, double>();

            if (contours.Size > 0)
            {
                for (int i = 0; i < contours.Size; i++)
                {
                    double area = CvInvoke.ContourArea(contours[i]);
                    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

                    if (rect.Width > 50 && rect.Height > 30 && area < 3000)
                    {
                        dict.Add(i, area);
                    }
                }
            }



            var item = dict.OrderByDescending(v => v.Value);

            Image<Bgr, byte> imgout1 = new Image<Bgr, byte>(imgInput.Width, imgInput.Height, new Bgr(0, 0, 0));

            foreach (var it in item)
            {
                int key = int.Parse(it.Key.ToString());
                Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
                //CvInvoke.DrawContours(imgInput, contours, key, new MCvScalar(255, 255, 255),4);
                CvInvoke.Rectangle(imgout1, rect, new MCvScalar(255, 255, 255), 3);
            }

            pictureBox1.Image = imgout1.Bitmap;  //<- here i am getting error

        }
    }
}
Posted
Updated 27-Dec-22 22:05pm

1 solution

Look at your code:
C#
imgInput = new Image<Bgr, byte>(ofd.FileName);
pictureBox1.Image = imgInput.Bitmap;
imgInput is an Image instance, and Bitmap is a separate class that derives from Image - that doesn't mean that the Image class contains a property which is the derived class!

Think about it: an apple is a fruit, but that doesn't mean every fruit is an apple - an orange is a completely different fruit and can't be treated in the same way.
Images and Bitmaps share the same relationship - not every Image is a Bitmap, but every Bitmap is an Image!

And if you look at the documentation for the PictureBox class, you will see that the PicturBox.Image property accepts an Image anyway - so any class derived from Image goes straight in. So chnage you code to
C#
imgInput = new Image<Bgr, byte>(ofd.FileName);
pictureBox1.Image = imgInput;
And it'll compile.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900