title: Bitmap-Image-Mat manipulation in emgucv : Overflow for imageSize
body: <p>I would like to ask for some help which could lead me to understand Mat, Image and Bitmaps. I'm using <a href="http://www.emgu.com/" rel="nofollow noreferrer">Emgu CV</a> in C# and I would like to convert the captured desktop screen ( which is bitmap ) to mat. I found some advice <a href="https://stackoverflow.com/questions/27281427/how-to-convert-bitmap-to-mat-structur-in-emgucv-how-to-detect-two-images-shift">here</a>, but it didn't work for me.</p>
  
  <p>What I would like to do is to convert the bitmap to image, the image to mat, then the mat back to image and the image back to bitmap, so that I can stream it to a picturebox. The problem is that when I'm converting from mat to image there is an exception thrown :</p>
  
  <p><a href="https://i.stack.imgur.com/ceBvN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ceBvN.png" alt="enter image description here"></a></p>
  
  <p>As I can see, there was a problem during converting the image to mat, as the rows (width) of the mat is exceptionally huge.</p>
  
  <p><a href="https://i.stack.imgur.com/20pE0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/20pE0.png" alt="enter image description here"></a></p>
  
  <p>Here is the code I'm using.</p>
  
  <pre><code>public void Video()
  {
        int Width = Screen.PrimaryScreen.Bounds.Width;
        int Height = Screen.PrimaryScreen.Bounds.Height;
        Size size = new Size(Height, Width);
        for ( ; ; )
        {
            System.Drawing.Image image;
            Bitmap bitmap = new Bitmap(Width, Height );
  
            Graphics graphics = Graphics.FromImage(bitmap as Image);
            graphics.CopyFromScreen(0, 0, Width, Height, bitmap.Size);
  
            Image&lt;Bgr, Byte&gt; imageCV = new Image&lt;Bgr, Byte&gt;(bitmap);
  
            //  CvInvoke.BitwiseNot(imageCV1, imageCV2, null);
            Mat m = new Mat(Height, Width , DepthType.Cv8U , 4);
            m = imageCV.Mat;
  
            Image&lt;Bgr, Byte&gt; mToImage = m.ToImage&lt;Bgr, Byte&gt;();  //error line
  
            //   Bitmap btm = mToImage.ToBitmap();
            Bitmap btm = mToImage.ToBitmap(m.Height, m.Width);
  
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = btm;
  
            imageCV.Dispose(); 
            mToImage.Dispose();
       }
  }
  </code></pre>
  
  <p>I know similar questions have been asked, and maybe I should have been able to put the pieces together, but I ran out of ideas. I tried to set the size of the Mat too, so the number of the rows cannot get too high, I also tried to invert my bitmap before passing it to an image. If anyone could help me to understand the concept behind them and this problem, I would be glad. </p>