Quantcast
Channel: WriteableBitmapEx
Viewing all 360 articles
Browse latest View live

New Post: Center of rotation

$
0
0
I would like to rotate an image at a certain point, but I don't have any option. There are 2 issues.
I want to rotate an image with mouse. So mouse could be there any place not ine middle of the picture. How this can be done?
What is the bounding rect for the new rotated image.

Any help will be very much appreciated.

Best regards
Agha Khan

New Post: Center of rotation

$
0
0
Check the RotateFree method. IT also has a boolean crop parameter.

Source code checked in, #114480

Patch Applied: #17681

New Post: Center of rotation

$
0
0
Dear Teichgraf:

Thanks for your reply.
I already checked, but there is no such functionally where I could place center of rotation.
I did use function RotateFree .

var modifiedImage = WriteableBitmapExtensions.RotateFree(all[3], 30, false);
 
writeableBmp.Blit(newRect(0, 0, 1000, 1000), modifiedImage, newRect(0, 0, 1000, 1000));
Unfortunately there no no option where I could mention center of rotation and always rotates around center of image. Also I have now I have no idea of bounding Rect, so I placed very large Rect(0,0,1000,1000) , which is not ideal either.
In documentation I didn't anywhere center of rotation.

Once again thanks for the email.


Best regards
Agha Khan

New Post: From... methods

$
0
0
Has anybody reviewed the patch corresponding to this discussion item yet? I would appreciate any feedback.

Thanks in advance,
Anders @ Cureos

New Post: From... methods

$
0
0
Not yet, but I agree with you in general. It is grown ;) the From... methods were there before the BitmapFactory. That's why.
Now I'm considering moving those to the BitmapFactory but keeping the extension methods with an [Obsolete] attribute and remove those a few versions after. Hard to move stuff without breaking existing code of users. ;)
On the other hand this also breaks the consistency with the FromByteArray method. Maybe I'll keep this one in both.

New Post: From... methods

$
0
0
Thanks for responding, René.

Actually, in the patch (ID 17504) that I posted recently, I have done exactly what you are describing, i.e. keeping the extension methods but with an [Obsolete] attribute. I had problems with Subversion, so instead of a diff patch I have included the full files that I have modified. It should be a simple task to just unzip the patch archive and copy the files into the right folders.

New Post: FromByteArray example

$
0
0
Hi all

I am creating a byte[] using ToByteArray (see ToByteArray Snippet), but when I try to reconstitute this into a WritableBitmap (see FromByteArray Snippet) I get an ArgumentException complaining about array out of bounds. I think the resize from 1,1 to the original image size is failing. But I am not sure what I am doing wrong. Any ideas?

Rob

FromByteArray Snippet

foreach (byte[] binary in Model.Client.Photos)
{
WriteableBitmap wbmp = BitmapFactory.New(1, 1).FromByteArray(binary);
}

ToByteArray Snippet

StorageFile file = await openPicker.PickSingleFileAsync();
        // If the file path and name is entered properly, and user has not tapped 'cancel'..
        if (file != null)
        {
            var property = await file.Properties.GetImagePropertiesAsync();

            WriteableBitmap wb = new WriteableBitmap((int)property.Width, (int)property.Height);

            using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                await wb.SetSourceAsync(fileStream);
                byte[] binaryImage = wb.ToByteArray();
                Model.Client.Photos.Add(binaryImage);
            }
        }

New Post: FromByteArray example

$
0
0
You need to keep track of the original width and height of the byte array image data as well and initialize the WriteableBitmap with that. Otherwise the WB won't know just from the byteArray.Length what the dimension of the WB is!

WriteableBitmap wbmp = BitmapFactory.New(widht, height).FromByteArray(binary);
  • Rene

Source code checked in, #114957

Source code checked in, #114959

Source code checked in, #115105

$
0
0
* Added /unsafe to a few project build configs

Updated Wiki: Home

$
0
0

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Samples that come with the WriteableBitmapEx source code in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster as the Silverlight Line element. So if a lot of shapes need to be drawn and anti-aliasing or other SIlverlight Shape properties are not needed, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
writeableBmp.GetBitmapContext();

// Load an image from the calling Assembly's resources only by passing the relative path
writeableBmp = BitmapFactory.New(1, 1).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);

// Line from P1(1, 2) to P2(30, 40) using the fastest draw line method with the color as integerint[] pixels = writeableBmp.Pixels;
int w = writeableBmp.PixelWidth;
int h = writeableBmp.PixelHeight;
WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor);

// Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke thickness of 5
writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5);

// Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10)
writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black);

// 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);

// Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 };
writeableBmp.DrawPolyline(p, Colors.Green);

// Cubic Beziér curve from P1(5, 5) to P4(20, 7) with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7,  Colors.Purple);

// Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5int[] pts = newint[] { 10, 5, 20, 40, 30, 30};
writeableBmp.DrawCurve(pts, 0.5,  Colors.Yellow);

// A filled Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5
writeableBmp.FillCurveClosed(pts, 0.5,  Colors.Green);

// Blit a bitmap using the additive blend mode at P1(10, 10)
writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive);

// Override all pixels with a function that changes the color based on the coordinate
writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100));

// Present the WriteableBitmap!
writeableBmp.Invalidate();

// Take snapshotvar clone = writeableBmp.Clone();

// Save to a TGA image stream (file for example)
writeableBmp.WriteTga(stream);

// Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10);

// Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90);

// Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal);

// Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation methodvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

// Invalidate
writeableBmp.Dispose();

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation.
WriteableBitmap Extension Methods introduced the SetPixel methods.
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods.
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle).
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG.
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions.
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project.
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function.
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample.
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample.
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample.
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version.

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code.
Dr. Andrew Burnett-Thompson proposed the portability refactoring, WPF port and much more beneficial functions.
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function.
Bill Reiss wrote the Blit methods.
Adam Kinney added some Blending modes to the Blit method.
Colin Eberhardt contributed the ForEach method.
Steve Hawley proposed an optimization of the Clear(Color) method.
Liam Bateman suggested the Color Keying BlendMode.
Mattias Fagerlund suggested the convolution method.
Wout de Zeeuw optimized the DrawLine method by 15%.
Lachlan Keown fixed a bug in the Blit alpha blending.
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods
Your name here? We are always looking for valuable contributions.

René Schulte wrote all the rest and coordinates this project.

Ohloh statistics

Updated Wiki: Home

$
0
0

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Silverlight samples that show the WriteableBitmapEx in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster than the Silverlight Line element. If a lot of shapes need to be drawn, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{ // Load an image from the calling Assembly's resources only by passing the relative path writeableBmp = BitmapFactory.New(1, 1).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); // Line from P1(1, 2) to P2(30, 40) using the fastest draw line method with the color as integerint[] pixels = writeableBmp.Pixels; int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor); // Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke thickness of 5 writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5); // Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10) writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black); // 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); // Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 }; writeableBmp.DrawPolyline(p, Colors.Green); // Cubic Beziér curve from P1(5, 5) to P4(20, 7) with the control points P2(10, 15) and P3(15, 0) writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple); // Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5int[] pts = newint[] { 10, 5, 20, 40, 30, 30}; writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow); // A filled Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5 writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green); // Blit a bitmap using the additive blend mode at P1(10, 10) writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive); // Override all pixels with a function that changes the color based on the coordinate writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100)); // Present the WriteableBitmap! writeableBmp.Invalidate(); // Take snapshotvar clone = writeableBmp.Clone(); // Save to a TGA image stream (file for example) writeableBmp.WriteTga(stream); // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10); // Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90); // Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal); // Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation methodvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);
} // Invalidate in the Dispose call

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation. 
WriteableBitmap Extension Methods introduced the SetPixel methods. 
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods. 
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle). 
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG. 
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions. 
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project. 
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function. 
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample. 
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample. 
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample. 
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version. 

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code. 
Dr. Andrew Burnett-Thompson and his team proposed the portability refactoring, provided the WPF port and much more beneficial functions. 
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function. 
Bill Reiss wrote the Blit methods. 
Adam Kinney added some Blending modes to the Blit method. 
Colin Eberhardt contributed the ForEach method. 
Steve Hawley proposed an optimization of the Clear(Color) method. 
Liam Bateman suggested the Color Keying BlendMode.   
Mattias Fagerlund suggested the convolution method. 
Wout de Zeeuw optimized the DrawLine method by 15%. 
Lachlan Keown fixed a bug in the Blit alpha blending. 
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods. 
Your name here? We are always looking for valuable contributions.   



Ohloh statistics


Updated Wiki: Home

$
0
0

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Silverlight samples that show the WriteableBitmapEx in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster than the Silverlight Line element. If a lot of shapes need to be drawn, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{ // Load an image from the calling Assembly's resources via the relative path writeableBmp = BitmapFactory.New(1, 1).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); // Line from P1(1, 2) to P2(30, 40) using the fastest draw line method
int[] pixels = writeableBmp.Pixels; int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor); // Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke of 5 writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5); // Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10) writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black); // 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); // Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 }; writeableBmp.DrawPolyline(p, Colors.Green); // Cubic Beziér curve from P1(5, 5) to P4(20, 7) with the control points P2(10, 15) and P3(15, 0) writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple); // Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5int[] pts = newint[] { 10, 5, 20, 40, 30, 30}; writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow); // A filled Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5 writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green); // Blit a bitmap using the additive blend mode at P1(10, 10) writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive); // Override all pixels with a function that changes the color based on the coordinate writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100)); // Present the WriteableBitmap! writeableBmp.Invalidate(); // Take snapshotvar clone = writeableBmp.Clone(); // Save to a TGA image stream (file for example) writeableBmp.WriteTga(stream); // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10); // Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90); // Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal); // Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation methodvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);
} // Invalidate in the Dispose call

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation. 
WriteableBitmap Extension Methods introduced the SetPixel methods. 
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods. 
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle). 
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG. 
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions. 
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project. 
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function. 
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample. 
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample. 
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample. 
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version. 

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code. 
Dr. Andrew Burnett-Thompson and his team proposed the portability refactoring, provided the WPF port and much more beneficial functions. 
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function. 
Bill Reiss wrote the Blit methods. 
Adam Kinney added some Blending modes to the Blit method. 
Colin Eberhardt contributed the ForEach method. 
Steve Hawley proposed an optimization of the Clear(Color) method. 
Liam Bateman suggested the Color Keying BlendMode.   
Mattias Fagerlund suggested the convolution method. 
Wout de Zeeuw optimized the DrawLine method by 15%. 
Lachlan Keown fixed a bug in the Blit alpha blending. 
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods. 
Your name here? We are always looking for valuable contributions.   



Ohloh statistics

Updated Wiki: Home

$
0
0

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Silverlight samples that show the WriteableBitmapEx in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster than the Silverlight Line element. If a lot of shapes need to be drawn, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{ // Load an image from the calling Assembly's resources via the relative path writeableBmp = BitmapFactory.New(1, 1).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); // Line from P1(1, 2) to P2(30, 40) using the fastest draw line method
int[] pixels = writeableBmp.Pixels; int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor); // Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke of 5 writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5); // Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10) writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black); // 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); // Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 }; writeableBmp.DrawPolyline(p, Colors.Green); // Cubic Beziér curve from P1(5, 5) to P4(20, 7)
// with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple); // Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
int[] pts = newint[] { 10, 5, 20, 40, 30, 30}; writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow); // A filled Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green); // Blit a bitmap using the additive blend mode at P1(10, 10) writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive); // Override all pixels with a function that changes the color based on the coordinate writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100)); } // Invalidate and present in the Dispose call
// Take snapshotvar clone = writeableBmp.Clone(); // Save to a TGA image stream (file for example) writeableBmp.WriteTga(stream); // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10); // Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90); // Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal); // Resizes the WriteableBitmap to 200px wide and 300px high using bilinear interpolationvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation. 
WriteableBitmap Extension Methods introduced the SetPixel methods. 
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods. 
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle). 
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG. 
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions. 
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project. 
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function. 
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample. 
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample. 
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample. 
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version. 

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code. 
Dr. Andrew Burnett-Thompson and his team proposed the portability refactoring, provided the WPF port and much more beneficial functions. 
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function. 
Bill Reiss wrote the Blit methods. 
Adam Kinney added some Blending modes to the Blit method. 
Colin Eberhardt contributed the ForEach method. 
Steve Hawley proposed an optimization of the Clear(Color) method. 
Liam Bateman suggested the Color Keying BlendMode.   
Mattias Fagerlund suggested the convolution method. 
Wout de Zeeuw optimized the DrawLine method by 15%. 
Lachlan Keown fixed a bug in the Blit alpha blending. 
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods. 
Your name here? We are always looking for valuable contributions.   



Ohloh statistics

Project License Changed

$
0
0
Copyright (c) 2009-2015 Rene Schulte Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Updated Wiki: Home

$
0
0

The source code is now on GitHub:
https://github.com/teichgraf/WriteableBitmapEx/

 

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Silverlight samples that show the WriteableBitmapEx in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster than the Silverlight Line element. If a lot of shapes need to be drawn, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{ // Load an image from the calling Assembly's resources via the relative path writeableBmp = BitmapFactory.New(1, 1).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); // Line from P1(1, 2) to P2(30, 40) using the fastest draw line method
int[] pixels = writeableBmp.Pixels; int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor); // Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke of 5 writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5); // Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10) writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black); // 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); // Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 }; writeableBmp.DrawPolyline(p, Colors.Green); // Cubic Beziér curve from P1(5, 5) to P4(20, 7)
// with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple); // Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
int[] pts = newint[] { 10, 5, 20, 40, 30, 30}; writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow); // A filled Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green); // Blit a bitmap using the additive blend mode at P1(10, 10) writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive); // Override all pixels with a function that changes the color based on the coordinate writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100)); } // Invalidate and present in the Dispose call
// Take snapshotvar clone = writeableBmp.Clone(); // Save to a TGA image stream (file for example) writeableBmp.WriteTga(stream); // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10); // Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90); // Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal); // Resizes the WriteableBitmap to 200px wide and 300px high using bilinear interpolationvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation. 
WriteableBitmap Extension Methods introduced the SetPixel methods. 
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods. 
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle). 
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG. 
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions. 
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project. 
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function. 
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample. 
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample. 
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample. 
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version. 

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code. 
Dr. Andrew Burnett-Thompson and his team proposed the portability refactoring, provided the WPF port and much more beneficial functions. 
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function. 
Bill Reiss wrote the Blit methods. 
Adam Kinney added some Blending modes to the Blit method. 
Colin Eberhardt contributed the ForEach method. 
Steve Hawley proposed an optimization of the Clear(Color) method. 
Liam Bateman suggested the Color Keying BlendMode.   
Mattias Fagerlund suggested the convolution method. 
Wout de Zeeuw optimized the DrawLine method by 15%. 
Lachlan Keown fixed a bug in the Blit alpha blending. 
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods. 
Your name here? We are always looking for valuable contributions.   



Ohloh statistics

Updated Wiki: Home

$
0
0

The source code is now on GitHub:
https://github.com/teichgraf/WriteableBitmapEx/

 

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for all XAML flavors including Windows Phone, WPF, WinRT Windows Store XAML, (Windows 10) UWP and Silverlight. It allows the direct manipulation of a bitmap and can be used for image manipulation, to generate fast procedural images by drawing directly to a bitmap and more.
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 offerGDI+ 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 C# files using a partial class approach. It is possible to include just a few methods by using the specific source code files directly or the full functionality via the built binaries.

The latest binaries are always available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future. Please use theGitHub Issues functionality to add new issues which are not already reported.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, Windows Phone WinRT, desktop Silverlight, WPF, Windows 8/8.1 WinRT XAML and Windows 10 UWP.

  • Base
    • 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
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including various anti-aliased algorithm
    • Variable stroke thickness and penned / stamp lines
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Triangle, quad, simple and complex polygons
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Filtering
    • Convolution, Blur
    • Brightness, contrast, gamma adjustments
    • Gray/brightness, invert
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll
  • External Code

Live samples

Silverlight samples that show the WriteableBitmapEx in action:

  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. 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. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.

Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster than the Silverlight Line element. If a lot of shapes need to be drawn, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{ // Load an image from the calling Assembly's resources via the relative path writeableBmp = BitmapFactory.New(1, 1).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); // Line from P1(1, 2) to P2(30, 40) using the fastest draw line method
int[] pixels = writeableBmp.Pixels; int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor); // Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke of 5 writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5); // Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10) writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black); // 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); // Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 }; writeableBmp.DrawPolyline(p, Colors.Green); // Cubic Beziér curve from P1(5, 5) to P4(20, 7)
// with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple); // Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
int[] pts = newint[] { 10, 5, 20, 40, 30, 30}; writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow); // A filled Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green); // Blit a bitmap using the additive blend mode at P1(10, 10) writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive); // Override all pixels with a function that changes the color based on the coordinate writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100)); } // Invalidate and present in the Dispose call
// Take snapshotvar clone = writeableBmp.Clone(); // Save to a TGA image stream (file for example) writeableBmp.WriteTga(stream); // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10); // Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90); // Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal); // Resizes the WriteableBitmap to 200px wide and 300px high using bilinear interpolationvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation. 
WriteableBitmap Extension Methods introduced the SetPixel methods. 
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods. 
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle). 
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG. 
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions. 
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project. 
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function. 
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample. 
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample. 
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample. 
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version. 

Support it

Donate

Credits

Rene Schulte started this project, maintains it and provided most of the code. 
Dr. Andrew Burnett-Thompson and his team proposed the portability refactoring, provided the WPF port and much more beneficial functions. 
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function. 
Bill Reiss wrote the Blit methods. 
Adam Kinney added some Blending modes to the Blit method. 
Colin Eberhardt contributed the ForEach method. 
Steve Hawley proposed an optimization of the Clear(Color) method. 
Liam Bateman suggested the Color Keying BlendMode.   
Mattias Fagerlund suggested the convolution method. 
Wout de Zeeuw optimized the DrawLine method by 15%. 
Lachlan Keown fixed a bug in the Blit alpha blending. 
John Ng San Ping added the AdjustBrightness, Contrast and Gray methods. 
Your name here? We are always looking for valuable contributions.   



Ohloh statistics

Viewing all 360 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>