The Microsoft MapPoint program is a great piece of software, with a wealth of capabilities above and beyond any of its Web-based competition, including its own online version. The ability to combine custom data with MapPoint's built-in datasets, such as Census Bureau demographics, allows the developer to display data in a way that is extremely helpful to marketing departments, salespeople, managers, executives, and other data consumers.
However, many of these users don't have access to the MapPoint software. In other cases, adding a map to a Web page, presentation, or even another piece of software would be useful. Sadly, Microsoft MapPoint, even when embedded as an ActiveX control and accessed programmatically via another Windows program, doesn't offer any methods to extract a high resolution image from it. But I'm going to show you how to accomplish that using pure, managed VB.NET code and Microsoft Office technologies. I'll be using Microsoft MapPoint, Microsoft Office Document Imaging, the .NET Framework's graphics capabilities, and the .NET Framework's process control functionality.
The difficulty with getting an image file out of MapPoint seems to derive from a colossal mistake on Microsoft's part. Whether by design or omission, the MapPoint ActiveX control simply does not offer a Save As Image function. The closest alternative is to instruct the control to save the map as an HTML page, take the image file out of the resulting directory structure, and then delete the rest of the file created. This is a perfectly acceptable solution, provided that a 256-color, low resolution GIF file is acceptable to you and your users.
On a similar level, experimentation with copying the contents of the MapPoint ActiveX control to the clipboard shows that approach to be a possible solution. Unfortunately, this method poses some problems. First, changing the contents of the clipboard without the user being aware of it is a major usability flaw. Imagine if the software is performing many of these image saves, and the user starts working on a Word document. One moment, he's cutting a valuable paragraph to the clipboard. The next moment, he tries to paste that paragraph somewhere else—only to get a map instead. This is not very user friendly. Even worse, when I experimented with this technique I found that using it rapidly over the course of many iterations caused it to randomly fail for no explainable reason. There must be a better way.
The solution to this problem lies in a little-known part of the Microsoft Office System: Microsoft Office Document Imaging (MODI). MODI installs itself as part of the Microsoft Office 2003 System. It also functions as an installed printer, allowing you to print from any piece of software to it. It has its own document format, which functions similarly to PDF. The software can also save documents as TIFF files.
The MODI functionality is the key to this solution. By printing to the MODI printer and then taking control of the MODI process, you can save your images as extremely high resolution, high color depth images in the format of your choice, no less! That last part is extremely important; TIFF files tend to be extremely large, thanks to their lossless compression. Not every user has a good piece of software to view or manipulate TIFF files, and many pieces of software can't use them. Users are often forced to open a TIFF file in a graphics editing program, select the entire image, copy the selected area, and then paste it where they want. By being able to get the image into a format of your choice, you return control to the end user.
Drawbacks and difficulties
The major drawback to this technique is that you're going to be starting and stopping the MODI process for each image saved. In addition, printing to the MODI printer driver starts MODI but does not automatically close it. As a result, to make your code more user friendly, you must tap into the .NET Framework's methods for managing processes to shut down MODI after you're finished with it.
To summarize, you need to perform the following steps to get the image out of MapPoint and into the format of your choice:
- Print to the MODI printer driver.
- Control MODI to convert the MODI document to TIFF format.
- Close MODI.
- Open the TIFF document and scale it as desired.
- Save the file to the desired filename and in the desired format (we'll assume a standard bitmap format).
- Remove the MODI document and the TIFF file.
Listing A shows the code to accomplish all of this. The only item to note is that the variable TempFiles is an instance of a custom class that manages temporary files and is assumed to be instantiated elsewhere in the application. The code for the class is included.
Justin James is the Lead Architect for Conigent.