RichEdit 8.0 Image Support

Up until RichEdit 8.0, RichEdit’s native image support was limited to metafiles, enhanced metafiles, and simple images like bitmaps (bmp’s). If OLE (Object Linking and Embedding) had supported other types, such as jpg’s, png’s and gif’s, RichEdit would have supported them automatically. But OLE’s functionality was frozen years ago. RichEdit 5.0 added “blobs”, which are light-weight OLE-like objects that the RichEdit client, like OneNote, renders. These blobs allow OneNote to insert and render many kinds of images. More recently, Microsoft created the Windows Imaging Component (see also), which supports the most popular image formats and allows extensions. RichEdit 8.0 uses this component to provide image support for jpg’s png’s and gif’s. This blog post summarizes the capabilities and APIs to take advantage of this facility.

A number of facilities that ship in Office RichEdit’s are not available in the Windows 8 RichEdit mostly because there wasn’t time to test them thoroughly outside the Office environment and to document them properly. One such facility is the RichEdit blob. RichEdit 8 uses its own built-in implementation of the blob to store and render images using the Windows Imaging Component.

There are two ways to insert images into a classic RichEdit 8 instance (use Windows 8 msftedit.dll): the EM_INSERTIMAGE message and the TOM2 ITextRange2::InsertImage() method. In addition, you can insert images into a WinRT RichEditBox using the WinRT TOM Windows.UI.Text.ITextRange.InsertImage() method. The first two specify the image dimensions in HIMETRIC units (.01 mm = 2540/inch) and the WinRT method uses Device Independent Pixels (96/inch). For example, to have a 4”×3” image in a classic RichEdit instance, use a width of 4×2540 = 10160 and a height of 7620.

In addition to the height and width, the APIs have an ascent parameter, which is usually zero. It’s included in case the image contains text that should be aligned with the text base line. In the original blob implementation for OneNote, this was used (and still is) for aligning hand written images with the text baseline. It’s also useful for aligning images of mathematics with the text baseline. This second use is supported by the ITextServices2::TxGetNaturalSize2() to return the baseline of text images created using RichEdit. This facility is used in the Office Equation ribbon.

You can save files including images in the RTF file format. The images are saved via RichEdit’s blob extension to RTF. This uses the OLE \object destination with a RichEdit-specific type of \objblob1. Unfortunately this type is known only to RichEdit. It would be better to save it as a native RTF shape ({\*\shppict{\pict{…}}) so that Word and other programs could understand it. Also then RichEdit could support such images in Word-generated RTF files. Hopefully next time…