Add BitmapContext extension methods for direct drawing operations
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
BitmapContextExtensionsclass with extension methods that mirror existingWriteableBitmapextension 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.