Webcam frame -> XNA texture

Ask question on using Emgu CV with C#
Posts: 1
Joined: Tue Oct 19, 2010 9:21 am

Webcam frame -> XNA texture

Postby Ralf74 » Tue Oct 19, 2010 9:33 am


Since yesterday i am using EmguCV together with XNA. I have some experiences with OpenCV and C++. This is my first post here.

Want i want to do is getting a frame from a webcam and putting it onto a XNA texture. Here is the source:

Code: Select all

public bool CreateCamera()
   capture = new Capture();

   if (capture != null)
      capture.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320);
      capture.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240);
      return true;
      return false;

public void QueryFrame(Texture2D texture)
   // get frame and copy data to texture
   Image<Bgr, Byte> image = capture.QueryFrame();

   if (image != null)
      image.Convert<Bgra, Byte>();

My program always crashes in the line "texture.SetData(image.Bytes);"

What do i wrong? Or has anyone an example code for me?

Posts: 15
Joined: Wed Sep 17, 2008 3:22 pm

Re: Webcam frame -> XNA texture

Postby mruce » Mon Oct 25, 2010 6:06 pm

You can use this class:

Code: Select all

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace MrXNAHelper
    public class ImageToTexture
        byte[] bmpBytes;
        Texture2D background;

        int width, height;
        Game game;
        public ImageToTexture(Game game, int width, int height)
            this.width = width;
            this.height = height;
   = game;

            GenerateBitmap(game, width, height);

        public ImageToTexture(Game game)
   = game;

        private void GenerateBitmap(Game game, int width, int height)
            background = new Texture2D(game.GraphicsDevice, width, height);

        public Texture2D ConvertBitmapToTexture(Bitmap b)
                game.GraphicsDevice.Textures[0] = null;
                if (background == null ||
                    b.Width != background.Width ||
                    b.Height != background.Height)
                    width = b.Width;
                    height = b.Height;
                    GenerateBitmap(game, width, height);
                BitmapData bData = b.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), b.Size),

                // number of bytes in the bitmap
                int byteCount = bData.Stride * b.Height;
                if (bmpBytes == null ||
                    bmpBytes.Length != byteCount)
                    bmpBytes = new byte[byteCount];

                // Copy the locked bytes from memory
                Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);

                // don't forget to unlock the bitmap!!

            return background;

Most of this code I've found on the web, I wrapped class around it. As the argument pass bitmap from Image<Bgr, byte> - it'll work.

Return to “C# Help”

Who is online

Users browsing this forum: Google [Bot] and 1 guest