Rendering ink and image to a bitmap using WPF

To follow-up on my previous blog post and to complete the story about rendering ink onto pictures and saving the results as a bitmap file, I want to show how this is done in WPF.

In WPF all rendering uses the pipeline - pictures, videos, ink, text, all gets rendered via WPF's 'Media Integration Layer'. As a result, it's very easy and straightforward to combine rendering these different types, both on screen and on a bitmap.

I have attached a WPF sample app that demonstrates how to collect ink on top of an image and then save the combined visual to a bitmap file. In fact, I have used this sample to create the sample picture that is included in my previous post about doing this in Winforms. Here is a screenshot of the sample app (btw, the ink color picker is borrowed from the InkColorPicker SDK sample).

The key class to accomplish our job in WPF is the RenderTargetBitmap class. Here is the relevant code snippet from the sample app:

// render InkCanvas' visual tree to the RenderTargetBitmap

RenderTargetBitmap targetBitmap =

new RenderTargetBitmap((int)inkCanvas1.ActualWidth,

(int)inkCanvas1.ActualHeight,

96d, 96d,

PixelFormats.Default);

targetBitmap.Render(inkCanvas1);

// add the RenderTargetBitmap to a Bitmapencoder

BmpBitmapEncoder encoder = new BmpBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(targetBitmap));

// save file to disk

FileStream fs = File.Open(fileName, FileMode.OpenOrCreate);

encoder.Save(fs);

Unlike the Winforms API, there is no behavioral difference between Windows Vista and Windows XP.

RenderInkOnBitmapWPF.zip