How to make an Avalonia UI compatible bitmap in code?

Viewed 1394

I wish to have a background bitmap in my Avalonia application. Getting it displayed is easy, it is making one in code that is hard.

For display, I have in my main window xaml:

  <Window.Background>
    <ImageBrush Source="{Binding BackgroundImage}"/>
  </Window.Background>

I can load a bitmap into the view model just fine using:

  Bitmap loader = new Bitmap( @"C:\image.BMP");
  _backgroundImage = loader;

So, now all I want to do is have the text "A2" (for example) made into a bitmap using code instead.

I've done a lot of experimenting and looking at samples, and the closest I have is:

var brush = new SolidColorBrush(Colors.Blue);
var text = new FormattedText();
text.Text = "A2";
var bitmap = new RenderTargetBitmap(new PixelSize(width, height));
using (IDrawingContextImpl ctx = bitmap.CreateDrawingContext(null))
{
  ctx.DrawText(brush, new Point(0, 0), text); // last param error
}
_backgroundImage = bitmap;

Which is fine apart from the fact that the text variable "cannot convert from 'Avalonia.Media.FormattedText' to 'Avalonia.Platform.IFormattedTextImpl'". I've experimented around it, tried to work out what is different between this and the Avalonia code, but got stuck. What am I missing please? (Yes, I've not got the details of the text size done yet - need to get the basics working first).

1 Answers

As of versions 0.9 and 0.10 you can convert IDrawingContextImpl to a proper DrawingContext by passing it to the constructor.

The API is higly likely to be changed in 0.11, so RenderTargetBitmap will be usable only for rendering controls and a different Bitmap class could be used for immediate-mode drawing.

Related