Search code examples
c#avaloniauiavalonia

How to draw pixel-by-pixel on bitmap in AvaloniaUI?


I am trying to write a drawing application that allows users to select two points on canvas and draws a line between those points pixel-by-pixel. In WinForms that would be an easy solution - create a canvas, get its bitmap, draw on the bitmap using the SetPixel method, and replace the canvas bitmap. I am wondering if there is a way to do this in a similar way in AvaloniaUI? From what I understood, it uses SkiaSharp under the hood. However, solutions to render SKCanvas on Avalonia.Controls.Canvas seem... hard. Although SKBitmap also has the SetPixel method. The question is not about how to draw a line - it is about how to set single pixels on Bitmap in AvaloniaUI and set this bitmap to the Avalonia.Controls.Canvas. Here are some links I found:

P.S. As tempting as this may sound, drawing a rectangle-by-rectangle of size 1x1 as in the official documentation example (https://docs.avaloniaui.net/docs/controls/canvas) is also not allowed by the university.

P.P.S. I am not asking anyone to do the homework for me - in case you are worried about that please, consider this a framework comparison question, not a "please do that for me" one. I have found and linked solutions that I can use - it just seems to me that there should be a more easy way to achieve the result I seek. That's why I have asked the question - in case I missed something.

Doing a Jedi thing: this is not some student trying to lay off his work on other people. You can go by your business


Solution

  • UPDATED: you can swap bitmap on the image control: https://docs.avaloniaui.net/docs/controls/image

    The original question (although poorly stated, my bad) was intented to clarify if there is a way to perform the SetPixel operation on the Avalonia bitmap. Unfortunately, currently, it doesn't seem like an available option: so the only way is to either create a custom drawing context OR control OR conversion between different Bitmap types (like System.Drawing.Bitmap or SkBitmap from SkiaSharp) to Avalonia.WriteableBitmap. All those solutions are referenced in the original question.