Capturing and Rendering a Raw Image

Surface 1.0 SP1

This example shows how to capture and render a raw image.


To capture and render raw images in a Surface application

  1. Open Microsoft Visual C# 2008 Express Edition (or Microsoft Visual Studio 2008), and then create a new project by using the Surface Application (XNA) template.

  2. Initialize the application to receive raw images.

    The C# InitializeSurfaceInput method is automatically generated by the Surface Application (XNA) template. The application must call the EnableImage method and assign a new event handler for the FrameReceived event. Modify InitializeSurfaceInput to look like this:

    This code example receives a normalized image (which, in this case, is a grayscale image that is easy to render inside the Microsoft XNA development platform). This example could also receive a binarized image (which is useful for image recognition).

  3. Define what happens when a FrameReceived event occurs by declaring a byte array called normalizedImage as a member of the application class and then passing the return value of the TryGetRawImage method into the normalizedImage array.

    The code for the FrameReceived event handler should be similar to the following code example.

    This code example uses the UpdateRawImage method to improve performance because this method uses the byte array that is already initialized, normalizedImage. The Update method uses the imageMetrics object to create a Texture2D object.

    If the Microsoft Surface unit has an external monitor installed, the origin might not be at (0,0) because the values are in desktop coordinates, not screen coordinates. Use DefaultInteractiveSurface values (as the preceding code shows) to get the actual coordinates of the interactive surface.

  4. Create a sprite to draw during every update by using the normalizedImage byte array and then creating a Texture2D object from this array.

    The code for the Update method should be similar to the following code example.

    You can now draw the Texture2D sprite to the screen by using standard XNA drawing procedures, as the following code example shows. The input resolution from the vision system does not necessarily match the output resolution of the screen. The ratio between the two depends on the hardware. In the following situation, the Scale variable is used to draw the image at the physical size of the object.

  5. Disable capturing raw image data when the application is deactivated. Re-enable capture when the application is activated.

    The following code enables raw image input in the ApplicationActivated event handler (OnApplicationActivated in the following example). It disables raw image input in the ApplicationDeactivated event handler (OnApplicationDeactivated in the following example).

Community Additions