Skip to content

Add BitmapContext extension methods for direct drawing operations - #94

Open
reneschulte with Copilot wants to merge 4 commits into
masterfrom
copilot/fix-92
Open

Add BitmapContext extension methods for direct drawing operations#94
reneschulte with Copilot wants to merge 4 commits into
masterfrom
copilot/fix-92

Conversation

CopilotAI commented May 21, 2025

Copy link
Copy Markdown
Contributor

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:

varbmp=newWriteableBitmap(500,500);// Each call creates and disposes a BitmapContext internallybmp.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:

varbmp=newWriteableBitmap(500,500);// Create the context onceusing(varctx=bmp.GetBitmapContext()){// Multiple drawing operations using the same contextctx.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.

CopilotAIand others added 3 commits May 21, 2025 07:11
Co-authored-by: reneschulte <7439129+reneschulte@users.noreply.github.com>
Co-authored-by: reneschulte <7439129+reneschulte@users.noreply.github.com>
Co-authored-by: reneschulte <7439129+reneschulte@users.noreply.github.com>
CopilotAI changed the title [WIP] Suggesting major modificationAdd BitmapContext extension methods for direct drawing operationsMay 21, 2025
CopilotAI requested a review from reneschulteMay 21, 2025 07:23
@reneschulte
reneschulte marked this pull request as ready for review June 2, 2025 07:05
@reneschulte

Copy link
Copy Markdown
Owner

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

@epsi1on

epsi1on commented Jun 2, 2025

Copy link
Copy Markdown
Contributor

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

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

original code:

publicstaticvoidDrawLineAa(thisWriteableBitmapbmp,intx1,inty1,intx2,inty2,intcolor,Rect?clipRect=null){//actual pixel manipulation code here}

new code:

publicstaticvoidDrawLineAa(thisWriteableBitmapbmp,intx1,inty1,intx2,inty2,intcolor,Rect?clipRect=null){using(varctx=bmp.GetBitmapContext())DrawLineAa(ctx,x1,y1,x2,y2,color,clipRect);}publicstaticvoidDrawLineAa(thisBitmapContextctx,intx1,inty1,intx2,inty2,intcolor,Rect?clipRect=null){//actual pixel manipulation code here}

Sign up for freeto 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

@reneschulte@epsi1on