Tuesday, February 05, 2008

Windows Mobile Screen Capture

While waiting for Vista SP1 to hit the MSDN downloads section, I decided to investigate a little bit on an issue that has caught my curiosity time and again: remote capturing of the device screen. There are lots of products on the market that will do this for you, but the question of how it is done kept my curiosity alive.

Searching the net for source code samples turned nothing (blame Live and Google). The best approach I got was from a CodeProject article but the proposed GDI method does not work on Windows Mobile because the CAPTUREBLT flag is not supported. This means that I needed another solution.

GAPI seemed to be a very good candidate. It has a very simple API and I had used it in the past, but it is now declared deprecated by Microsoft and I was prompted to use Direct Draw instead. Willing to learn a bit more about this technology, I decided to give it a go and spent a couple of hours reading through MSDN docs to figure out how this can be done. As it turned out, it's very simple.

To capture the device screen using Direct Draw you need to follow these simple steps (I will write an article on this illustrating a desktop tool to capture the device screen via RAPI):
  1. Initialize Direct Draw using the DirectDrawCreate function using NULL as the first parameter.
  2. Set the cooperative level to DDSCL_NORMAL. This is enough for accessing the primary surface in read-only mode.
  3. Create the primary surface using this code.
  4. Get the surface's HDC and use it to create a compatible DC.
  5. Create a compatible bitmap using the screen dimensions and select it to the compatible DC.
  6. Blit from the surface DC to the compatible DC using BitBlt.
  7. Done! The compatible bitmap now contains the device's screen capture.

I wrote this using a DIBSection in order to marshal it via a RAPI call and was able to deserialize it on the desktop and display the device screen capture. I'm planning to use this same marshalling technique to implement a remote file browser using the device's own system image list.

Stay tuned for the article!

No comments: