Putting It All Together

We now have everything we need for the project, we just need to combine the “HelloWebServer” and “SketchPad” projects into one, and add some code to serve back the image on the screen rather than just text.

Load the project “WebMessenger” into Visual Studio.

Figure 4-10 shows the arrangements of the modules and as you would expect, we use a combination of all the modules from the “HelloWebServer” and “SketchPad” projects.

One of the first things to notice in the code is that there is a new “using” line at the top. This is to include a utility method that we need to use to convert the bitmap on the screen to a BMP file format:

using GHIElectronics.NETMF.System;

This method is to be found in an assembly that is not automatically included in the project. So, if you were starting your own project that needed to use this, you would have to refer to it from your project.

To do this, right-click over the project, select the option “Add Reference…”, then select “GHIElectronics.NETMF.System” from the list (see Figure 4-9).

Documentation for this method and other useful things can be found here: http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/

We define a WebEvent and call it sketch. The following shows the handler for WebEventReceived for this WebEvent:

void sketch_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
{
  Bitmap bitmap = background.Bitmap;
  byte[] buff = new byte[bitmap.Width * bitmap.Height * 3 + 54];
  Util.BitmapToBMPFile(bitmap.GetBitmap(), bitmap.Width, bitmap.Height, buff);
  GT.Picture picture = new GT.Picture(buff, GT.Picture.PictureEncoding.BMP);
  responder.Respond(picture);
}
Referencing a Package

Figure 4-9. Referencing a Package

Web Messenger Modules

Figure 4-10. Web Messenger Modules

The method BitmapToBMPFile requires a byte array buffer into which it puts the data for the BMP image. The size of this buffer must be the width of the image times its height x 3 + 54. The “times 3” is because each pixel is made up of 3 bytes, one each for the red, green, and blue components of the pixel.

The buffer length has 54 added to it to allow room for the BMP file’s header.

A new Picture is created from the buffer and used as the argument to Respond.

Get Getting Started with .NET Gadgeteer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.