RenderTargetBitmap.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Platform;
  5. using Avalonia.Rendering;
  6. using Avalonia.VisualTree;
  7. namespace Avalonia.Media.Imaging
  8. {
  9. /// <summary>
  10. /// A bitmap that holds the rendering of a <see cref="IVisual"/>.
  11. /// </summary>
  12. public class RenderTargetBitmap : Bitmap, IDisposable, IRenderTarget
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="RenderTargetBitmap"/> class.
  16. /// </summary>
  17. /// <param name="width">The width of the bitmap.</param>
  18. /// <param name="height">The height of the bitmap.</param>
  19. public RenderTargetBitmap(int width, int height)
  20. : base(CreateImpl(width, height))
  21. {
  22. }
  23. /// <summary>
  24. /// Gets the platform-specific bitmap implementation.
  25. /// </summary>
  26. public new IRenderTargetBitmapImpl PlatformImpl => (IRenderTargetBitmapImpl)base.PlatformImpl;
  27. /// <summary>
  28. /// Disposes of the bitmap.
  29. /// </summary>
  30. public void Dispose()
  31. {
  32. PlatformImpl.Dispose();
  33. }
  34. /// <summary>
  35. /// Creates a platform-specific imlementation for a <see cref="RenderTargetBitmap"/>.
  36. /// </summary>
  37. /// <param name="width">The width of the bitmap.</param>
  38. /// <param name="height">The height of the bitmap.</param>
  39. /// <returns>The platform-specific implementation.</returns>
  40. private static IBitmapImpl CreateImpl(int width, int height)
  41. {
  42. IPlatformRenderInterface factory = AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();
  43. return factory.CreateRenderTargetBitmap(width, height);
  44. }
  45. public DrawingContext CreateDrawingContext() => PlatformImpl.CreateDrawingContext();
  46. }
  47. }