Over a million developers have joined DZone.

Rounder, Faster, Better - WriteableBitmapEx

DZone 's Guide to

Rounder, Faster, Better - WriteableBitmapEx

· ·
Free Resource

A lot of things happened since the release of the last WriteableBitmapEx version. I used the time I have to wait for a third party [spoiler] until I could release my Silverlight Augmented Reality framework SLARToolkit [/spoiler] to work on the WriteableBitmapEx project. Beside some optimizations and smaller extensions, the main addition in the new version is the support for parametric curves. I implemented the cubic Beziér spline and the Cardinal spline. The Cardinal spline is not part of Silverlight's and WPF's Geometry nor shape classes, but the DrawCurve method is part of GDI+ and many people appreciate the characteristics of cardinal splines. Fortunately the WriteableBitmapEx library fills this gap now for the WriteableBitmap.

New features

  • Draw methods for parametric curves: Cubic Beziér splines and the Cardinal splines similar to the GDI+ API. 
  • Clone method to copy the whole WriteableBitmap to a new instance.
  • Boundary check for the Draw* (Shape) methods. The coordinates are now clipped to the WriteableBitmap's
  • A new, even faster DrawLine method overload that uses the bitmap's pixels int array, width and height directly as parameters. This method should be used if many lines are drawn to the same WriteableBitmap.
  • Optimization of the byte array conversion methods using the Buffer.BlockCopy method. This improvement was suggested in this comment.
  • WriteTga method to write a WriteableBitmap as a TGA image to a stream. The original method was provided in this blog post.
  • Fast path for Blitting using the Buffer.BlockCopy method if the BlendMode is None and the bitmap should not be tinted. The result is a performance boost by factor 3.5 compared to traditional loop iteration. This improvement was suggested in this CodePlex discussion
  • Adam Kinney contributed two new blend modes for the Blit method: Multiply and Mask. He used these in his cool torn photo effect.
  • A new method that takes a function as parameter and iterates over each pixel and sets its color. The method is called ForEach and has two overloads. Usage example:
    writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, color.R / 2, (byte)(x * y), 100));
    The method was suggested in this CodePlex discussion
The code listing on the project's CodePlex site was updated to show the usage of the new methods.


I also wrote a new sample application that shows the spline methods in action. The sample starts with a demo animation that uses the Cardinal spline DrawCurve method to draw an artificial plant that grows procedurally. The other part of the sample is interactive and allows to draw and manipulate Beziér and Cardinal splines with the mouse.

Uncheck the "Growing plant demo" Checkbox to change to the interactive mode where you can add new or move curve control points with the left mouse button. Press the DEL key while a control point is selected to remove it. The used curve type can be changed with the Radio Buttons. A cardinal spline needs at least 3 points and a cubic Beziér 4 points, a start point, the control point 1, the control point 2 and the end point.The Slider controls the tension of the Cardinal spline. The tension defines the shape of the curve and is usually between 0 and 1. A value of 0 would be a straight line.
Uncheck the "Points" Checkbox to hide the control points and press the Clear Button to remove all control points. Click the Save Button to write the WriteableBitmap to a TGA image file.
The other controls should be self-explaining.

Get it while it's hot

The WriteableBitmapEx library is hosted at CodePlex. You can find the new binary release here.

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}