WriteableBitmapEx icon indicating copy to clipboard operation
WriteableBitmapEx copied to clipboard

Add BitmapContext extension methods for direct drawing operations

Open Copilot opened this issue 8 months ago • 3 comments

This PR adds extension methods for the BitmapContext class that allow users to perform drawing operations directly on a BitmapContext instead of having to go through the WriteableBitmap. This enables more efficient code when doing multiple drawing operations since the BitmapContext only needs to be created once.

Key changes

  • Created a new BitmapContextExtensions class with extension methods that mirror existing WriteableBitmap extension methods
  • Implemented drawing methods: lines, rectangles, ellipses
  • Implemented fill methods: rectangles, ellipses
  • Implemented blit operations for copying pixels between contexts
  • Implemented transform operations (flip)
  • Added the class to all relevant project files

Usage example

Before, users needed to use WriteableBitmap methods that create/dispose contexts internally:

var bmp = new WriteableBitmap(500, 500);
// Each call creates and disposes a BitmapContext internally
bmp.DrawLine(10, 10, 100, 100, Colors.Red);
bmp.DrawRectangle(50, 50, 150, 150, Colors.Blue);
bmp.FillEllipse(200, 200, 300, 300, Colors.Green);

Now, users can reuse the same context for multiple operations:

var bmp = new WriteableBitmap(500, 500);
// Create the context once
using(var ctx = bmp.GetBitmapContext())
{
    // Multiple drawing operations using the same context
    ctx.DrawLine(10, 10, 100, 100, Colors.Red);
    ctx.DrawRectangle(50, 50, 150, 150, Colors.Blue);
    ctx.FillEllipse(200, 200, 300, 300, Colors.Green);
}
// Context is disposed only once

This provides better performance when doing many consecutive drawing operations, especially in loops, and also allows users to perform drawing operations from multiple threads.

Fixes #92.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar May 21 '25 06:05 Copilot

@epsi1on Can you take a look if these are the wrappers and interfaces you had in mind?

reneschulte avatar Jun 02 '25 07:06 reneschulte

Seems yes, but not completely. What i had in mind was to simply replace all extensions of WritebaleBitmap into extensions of BitmapContext. (as stated in #76 and maybe other issues).

Although the BitmapContext does not allocated every time bmp.GetBitmapContext() is called (because a caching mechanism is there), but this would make it a little more robust I think.

If you are OK with this idea, I can make a pull request.

Thanks

epsi1on avatar Jun 02 '25 12:06 epsi1on

Instead of replacing all extension methods of WriteableBitmap, there could be an identical copy which accepts BitmapContext instead:

original code:

public static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color, Rect? clipRect = null)
{
    //actual pixel manipulation code here
}

new code:

public static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color, Rect? clipRect = null)
{
    using(var ctx = bmp.GetBitmapContext())
        DrawLineAa(ctx,x1,y1,x2,y2,color,clipRect);
}

public static void DrawLineAa(this BitmapContext ctx, int x1, int y1, int x2, int y2, int color, Rect? clipRect = null)
{
    //actual pixel manipulation code here
}

epsi1on avatar Jun 11 '25 03:06 epsi1on