WicBitmapImpl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 System.IO;
  5. using Avalonia.Win32.Interop;
  6. using SharpDX.WIC;
  7. using APixelFormat = Avalonia.Platform.PixelFormat;
  8. using D2DBitmap = SharpDX.Direct2D1.Bitmap;
  9. namespace Avalonia.Direct2D1.Media
  10. {
  11. /// <summary>
  12. /// A WIC implementation of a <see cref="Avalonia.Media.Imaging.Bitmap"/>.
  13. /// </summary>
  14. public class WicBitmapImpl : BitmapImpl
  15. {
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class.
  18. /// </summary>
  19. /// <param name="fileName">The filename of the bitmap to load.</param>
  20. public WicBitmapImpl(string fileName)
  21. {
  22. using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, fileName, DecodeOptions.CacheOnDemand))
  23. {
  24. WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand);
  25. }
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class.
  29. /// </summary>
  30. /// <param name="stream">The stream to read the bitmap from.</param>
  31. public WicBitmapImpl(Stream stream)
  32. {
  33. using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, stream, DecodeOptions.CacheOnLoad))
  34. {
  35. WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad);
  36. }
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class.
  40. /// </summary>
  41. /// <param name="width">The width of the bitmap.</param>
  42. /// <param name="height">The height of the bitmap.</param>
  43. /// <param name="pixelFormat">Pixel format</param>
  44. public WicBitmapImpl(int width, int height, APixelFormat? pixelFormat = null)
  45. {
  46. if (!pixelFormat.HasValue)
  47. {
  48. pixelFormat = APixelFormat.Bgra8888;
  49. }
  50. PixelFormat = pixelFormat;
  51. WicImpl = new Bitmap(
  52. Direct2D1Platform.ImagingFactory,
  53. width,
  54. height,
  55. pixelFormat.Value.ToWic(),
  56. BitmapCreateCacheOption.CacheOnLoad);
  57. }
  58. public WicBitmapImpl(APixelFormat format, IntPtr data, int width, int height, int stride)
  59. {
  60. WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, width, height, format.ToWic(), BitmapCreateCacheOption.CacheOnDemand);
  61. PixelFormat = format;
  62. using (var l = WicImpl.Lock(BitmapLockFlags.Write))
  63. {
  64. for (var row = 0; row < height; row++)
  65. {
  66. UnmanagedMethods.CopyMemory(
  67. (l.Data.DataPointer + row * l.Stride),
  68. (data + row * stride),
  69. (UIntPtr) l.Data.Pitch);
  70. }
  71. }
  72. }
  73. protected APixelFormat? PixelFormat { get; }
  74. /// <summary>
  75. /// Gets the width of the bitmap, in pixels.
  76. /// </summary>
  77. public override int PixelWidth => WicImpl.Size.Width;
  78. /// <summary>
  79. /// Gets the height of the bitmap, in pixels.
  80. /// </summary>
  81. public override int PixelHeight => WicImpl.Size.Height;
  82. public override void Dispose()
  83. {
  84. WicImpl.Dispose();
  85. }
  86. /// <summary>
  87. /// Gets the WIC implementation of the bitmap.
  88. /// </summary>
  89. public Bitmap WicImpl { get; }
  90. /// <summary>
  91. /// Gets a Direct2D bitmap to use on the specified render target.
  92. /// </summary>
  93. /// <param name="renderTarget">The render target.</param>
  94. /// <returns>The Direct2D bitmap.</returns>
  95. public override OptionalDispose<D2DBitmap> GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget)
  96. {
  97. FormatConverter converter = new FormatConverter(Direct2D1Platform.ImagingFactory);
  98. converter.Initialize(WicImpl, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
  99. return new OptionalDispose<D2DBitmap>(D2DBitmap.FromWicBitmap(renderTarget, converter), true);
  100. }
  101. public override void Save(Stream stream)
  102. {
  103. using (var encoder = new PngBitmapEncoder(Direct2D1Platform.ImagingFactory, stream))
  104. using (var frame = new BitmapFrameEncode(encoder))
  105. {
  106. frame.Initialize();
  107. frame.WriteSource(WicImpl);
  108. frame.Commit();
  109. encoder.Commit();
  110. }
  111. }
  112. }
  113. }