WritableBitmap Extension by Anton

Sometimes native classes aren’t providing the needed functionality or works not like you want. If you are working with some 2D graphics and want to actually focus on how your’s algorithm works without some basic image manipulations – WritableBitmapEx from CodePlex.

The WriteableBitmapEx library is a collection of extension methods for Silverlight’s WriteableBitmap. The WriteableBitmap class that was added in Silverlight 3, allows the direct manipulation of a bitmap and could be used to generate fast procedural images by drawing directly to a bitmap. The WriteableBitmap API is very minimalistic and there’s only the raw Pixels array for such operations. The WriteableBitmapEx library tries to compensate that with extensions methods that are easy to use like built in methods and offer GDI+ like functionality. The library extends the WriteableBitmap class with elementary and fast (2D drawing) functionality, conversion methods and functions to combine (blit) WriteableBitmaps. The extension methods are grouped into different CS files with a partial class. It is possible to include just a few methods by using the specific source CS files directly or all extension methods through the built library assembly.

In modern version of this WriteableBitmap we have:

  • Support for the Color structure (alpha premultiplication will be performed)
  • Also overloads for faster int32 as color (assumed to be already alpha premultiplied)
  • SetPixel method with various overloads
  • GetPixel method to get the pixel color at a specified x, y coordinate
  • Fast Clear methods
  • Fast Clone method to copy a WriteableBitmap
  • ForEach method to apply a given function to all pixels of the bitmap

A lot of standard transformations:

  • Crop method to extract a defined region
  • Resize method with support for bilinear interpolation and nearest neighbor
  • Rotate in 90° steps clockwise
  • Flip vertical and horizontal

Some basic shapes, that can be easily drawn:

  • Fast line drawing algorithms
  • Ellipse, polyline, quad, rectangle and triangle
  • Cubic Bezier, Cardinal spline and closed curves

Fast filling algorithms:

  • Fast ellipse and rectangle fill method
  • Polygon, triangle and quad
  • Bezier and Cardinal spline curves

Blitting:

  • Different blend modes including alpha, additive, subtractive, multiply, mask and none
  • Optimized fast path for non blended blitting

Support different file types to convert :

  • Convert a WriteableBitmap to a byte array
  • Create a WriteableBitmap from a byte array
  • Create a WriteableBitmap easily from the application resource
  • Write a WriteableBitmap as a TGA image to a stream

All this features are working 20-30 times faster then some in original classes and every release includes some new performance improvements. Here is some example from the official site:

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control WriteableBitmap
writeableBmp = new WriteableBitmap(512, 512);
ImageControl.Source = writeableBmp;
// Load an image from the calling Assembly's resources only by passing the relative path
writeableBmp = new WriteableBitmap(0, 0).FromResource("Data/flower2.png");
// Clear the WriteableBitmap with white color
writeableBmp.Clear(Colors.White);
// Set the pixel at P(10, 13) to black
writeableBmp.SetPixel(10, 13, Colors.Black);
// Get the color of the pixel at P(30, 43)
Color color = writeableBmp.GetPixel(30, 43);
// Green line from P1(1, 2) to P2(30, 40)
writeableBmp.DrawLine(1, 2, 30, 40, Colors.Green);
// Red rectangle from the point P1(2, 4) that is 10px wide and 6px high
writeableBmp.DrawRectangle(2, 4, 12, 10, Colors.Red);
// Filled blue ellipse with the center point P1(2, 2) that is 8px wide and 5px high
writeableBmp.FillEllipseCentered(2, 2, 8, 5, Colors.Blue);
// Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copy
var rotated = writeableBmp.Rotate(90);
// Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copy
var flipped = writeableBmp.Flip(FlipMode.Horizontal);
// Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation method
var resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

You can use this class in Windows Phone 7 development also. Official site: WritableBitmapEx on Codeplex.