using System;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Utilities;
using Avalonia.VisualTree;
namespace Avalonia.Media.Imaging
{
///
/// A bitmap that holds the rendering of a .
///
public class RenderTargetBitmap : Bitmap, IDisposable, IRenderTarget
{
///
/// Initializes a new instance of the class.
///
/// The size of the bitmap.
public RenderTargetBitmap(PixelSize pixelSize)
: this(pixelSize, new Vector(96, 96))
{
}
///
/// Initializes a new instance of the class.
///
/// The size of the bitmap in device pixels.
/// The DPI of the bitmap.
public RenderTargetBitmap(PixelSize pixelSize, Vector dpi)
: this(RefCountable.Create(CreateImpl(pixelSize, dpi)))
{
}
private RenderTargetBitmap(IRef impl) : base(impl)
{
PlatformImpl = impl;
}
///
/// Gets the platform-specific bitmap implementation.
///
public new IRef PlatformImpl { get; }
///
/// Renders a visual to the .
///
/// The visual to render.
public void Render(Visual visual) => ImmediateRenderer.Render(visual, this);
///
/// Creates a platform-specific implementation for a .
///
/// The size of the bitmap in device pixels.
/// The DPI of the bitmap.
/// The platform-specific implementation.
private static IRenderTargetBitmapImpl CreateImpl(PixelSize size, Vector dpi)
{
IPlatformRenderInterface factory = AvaloniaLocator.Current.GetRequiredService();
return factory.CreateRenderTargetBitmap(size, dpi);
}
///
public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer? vbr) => PlatformImpl.Item.CreateDrawingContext(vbr);
}
}