Skip to content

Conversation

Copy link
Contributor

Copilot AI commented May 21, 2025

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 AI changed the title [WIP] Suggesting major modification Add BitmapContext extension methods for direct drawing operations May 21, 2025
Copilot AI requested a review from reneschulte May 21, 2025 07:23
Copilot finished work on behalf of reneschulte May 21, 2025 07:23
@reneschulte reneschulte marked this pull request as ready for review June 2, 2025 07:05
@reneschulte
Copy link
Owner

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

@epsi1on
Copy link
Contributor

epsi1on commented Jun 2, 2025

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
Copy link
Contributor

epsi1on commented Jun 11, 2025

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggesting major modification

3 participants