diff --git a/src/thirtytwo/Win32/Graphics/Direct2D/HwndRenderTarget.cs b/src/thirtytwo/Win32/Graphics/Direct2D/HwndRenderTarget.cs index bf042ad..b5a9ca3 100644 --- a/src/thirtytwo/Win32/Graphics/Direct2D/HwndRenderTarget.cs +++ b/src/thirtytwo/Win32/Graphics/Direct2D/HwndRenderTarget.cs @@ -63,5 +63,11 @@ public static HwndRenderTarget CreateForWindow( return new HwndRenderTarget(renderTarget); } + public void Resize(Size size) + { + Pointer->Resize((D2D_SIZE_U)size).ThrowOnFailure(); + GC.KeepAlive(this); + } + public static implicit operator ID2D1HwndRenderTarget*(HwndRenderTarget target) => target.Pointer; } \ No newline at end of file diff --git a/src/thirtytwo/Win32/Graphics/Direct2D/RenderTarget.cs b/src/thirtytwo/Win32/Graphics/Direct2D/RenderTarget.cs index 59cce7b..0a2014c 100644 --- a/src/thirtytwo/Win32/Graphics/Direct2D/RenderTarget.cs +++ b/src/thirtytwo/Win32/Graphics/Direct2D/RenderTarget.cs @@ -2,8 +2,10 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Drawing; +using System.Numerics; using Windows.Support; using Windows.Win32.Graphics.Direct2D.Common; +using Windows.Win32.Graphics.DirectWrite; using Windows.Win32.Graphics.Dxgi.Common; using Windows.Win32.Graphics.Imaging; @@ -17,6 +19,123 @@ public RenderTarget(ID2D1RenderTarget* renderTarget) : base((ID2D1Resource*)rend { } + public void BeginDraw() + { + Pointer->BeginDraw(); + GC.KeepAlive(this); + } + + public void EndDraw(out bool recreateTarget) + { + HRESULT result = Pointer->EndDraw(null, null); + if (result == HRESULT.D2DERR_RECREATE_TARGET) + { + recreateTarget = true; + } + else + { + result.ThrowOnFailure(); + recreateTarget = false; + } + + GC.KeepAlive(this); + } + + public SolidColorBrush CreateSolidColorBrush(Color color) + { + ID2D1SolidColorBrush* solidColorBrush; + D2D1_COLOR_F colorf = (D2D1_COLOR_F)color; + Pointer->CreateSolidColorBrush(&colorf, null, &solidColorBrush).ThrowOnFailure(); + GC.KeepAlive(this); + return new SolidColorBrush(solidColorBrush); + } + + public void SetTransform(Matrix3x2 transform) + { + Pointer->SetTransform((D2D_MATRIX_3X2_F*)&transform); + GC.KeepAlive(this); + } + + public void Clear(Color color) + { + D2D1_COLOR_F colorf = (D2D1_COLOR_F)color; + Pointer->Clear(&colorf); + GC.KeepAlive(this); + } + + public void DrawLine(PointF point0, PointF point1, Brush brush, float strokeWidth = 1.0f) + { + Pointer->DrawLine(*(D2D_POINT_2F*)&point0, *(D2D_POINT_2F*)&point1, brush.Pointer, strokeWidth, null); + GC.KeepAlive(this); + } + + public void FillRectangle(RectangleF rect, Brush brush) + { + D2D_RECT_F rectf = (D2D_RECT_F)rect; + Pointer->FillRectangle(&rectf, brush.Pointer); + GC.KeepAlive(this); + } + + public void DrawRectangle(RectangleF rect, Brush brush, float strokeWidth = 1.0f) + { + D2D_RECT_F rectf = (D2D_RECT_F)rect; + Pointer->DrawRectangle(&rectf, brush.Pointer, strokeWidth, null); + GC.KeepAlive(this); + } + + public SizeF Size() + { + D2D_SIZE_F size = Pointer->GetSizeHack(); + GC.KeepAlive(this); + return *(SizeF*)&size; + } + + /// + public void DrawTextLayout( + PointF origin, + TextLayout textLayout, + Brush defaultFillBrush, + DrawTextOptions options = DrawTextOptions.None) + { + Pointer->DrawTextLayout( + origin, + textLayout.Pointer, + defaultFillBrush.Pointer, + (D2D1_DRAW_TEXT_OPTIONS)options); + + GC.KeepAlive(this); + GC.KeepAlive(textLayout); + GC.KeepAlive(defaultFillBrush); + } + + public void DrawBitmap( + Bitmap bitmap, + RectangleF destinationRectangle = default, + float opacity = 1.0f, + BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.Linear) + { + D2D_RECT_F destination = (D2D_RECT_F)destinationRectangle; + if (destinationRectangle.IsEmpty) + { + D2D_SIZE_F size = Pointer->GetSizeHack(); + destination = new D2D_RECT_F { left = 0, top = 0, right = size.width, bottom = size.height }; + } + else + { + destination = (D2D_RECT_F)destinationRectangle; + } + + Pointer->DrawBitmap( + bitmap.Pointer, + &destination, + opacity, + (D2D1_BITMAP_INTERPOLATION_MODE)interpolationMode, + null); + + GC.KeepAlive(this); + GC.KeepAlive(bitmap); + } + /// public Bitmap CreateBitmapFromWicBitmap( TBitmapSource wicBitmap) diff --git a/src/thirtytwo/Win32/Graphics/Direct2D/RenderTargetExtensions.cs b/src/thirtytwo/Win32/Graphics/Direct2D/RenderTargetExtensions.cs deleted file mode 100644 index 417313e..0000000 --- a/src/thirtytwo/Win32/Graphics/Direct2D/RenderTargetExtensions.cs +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright (c) Jeremy W. Kuhne. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Drawing; -using System.Numerics; -using Windows.Support; -using Windows.Win32.Graphics.Direct2D.Common; -using Windows.Win32.Graphics.DirectWrite; -using Windows.Win32.Graphics.Imaging; - -namespace Windows.Win32.Graphics.Direct2D; - -public static unsafe class RenderTargetExtensions -{ - public static void Resize(this T target, Size size) - where T : IPointer - { - target.Pointer->Resize((D2D_SIZE_U)size).ThrowOnFailure(); - GC.KeepAlive(target); - } - - public static void BeginDraw(this T target) where T : IPointer - { - target.Pointer->BeginDraw(); - GC.KeepAlive(target); - } - - public static void EndDraw(this T target, out bool recreateTarget) - where T : IPointer - { - HRESULT result = target.Pointer->EndDraw(null, null); - if (result == HRESULT.D2DERR_RECREATE_TARGET) - { - recreateTarget = true; - } - else - { - result.ThrowOnFailure(); - recreateTarget = false; - } - - GC.KeepAlive(target); - } - - public static SolidColorBrush CreateSolidColorBrush(this T target, Color color) - where T : IPointer - { - ID2D1SolidColorBrush* solidColorBrush; - D2D1_COLOR_F colorf = (D2D1_COLOR_F)color; - target.Pointer->CreateSolidColorBrush(&colorf, null, &solidColorBrush).ThrowOnFailure(); - GC.KeepAlive(target); - return new SolidColorBrush(solidColorBrush); - } - - public static void SetTransform(this T target, Matrix3x2 transform) where T : IPointer - { - target.Pointer->SetTransform((D2D_MATRIX_3X2_F*)&transform); - GC.KeepAlive(target); - } - - public static void Clear(this T target, Color color) where T : IPointer - { - D2D1_COLOR_F colorf = (D2D1_COLOR_F)color; - target.Pointer->Clear(&colorf); - GC.KeepAlive(target); - } - - public static void DrawLine(this T target, PointF point0, PointF point1, Brush brush, float strokeWidth = 1.0f) - where T : IPointer - { - target.Pointer->DrawLine(*(D2D_POINT_2F*)&point0, *(D2D_POINT_2F*)&point1, brush.Pointer, strokeWidth, null); - GC.KeepAlive(target); - } - - public static void FillRectangle(this T target, RectangleF rect, Brush brush) - where T : IPointer - { - D2D_RECT_F rectf = (D2D_RECT_F)rect; - target.Pointer->FillRectangle(&rectf, brush.Pointer); - GC.KeepAlive(target); - } - - public static void DrawRectangle(this T target, RectangleF rect, Brush brush, float strokeWidth = 1.0f) - where T : IPointer - { - D2D_RECT_F rectf = (D2D_RECT_F)rect; - target.Pointer->DrawRectangle(&rectf, brush.Pointer, strokeWidth, null); - GC.KeepAlive(target); - } - - public static SizeF Size(this T target) where T : IPointer - { - D2D_SIZE_F size = target.Pointer->GetSizeHack(); - GC.KeepAlive(target); - return *(SizeF*)&size; - } - - /// - public static void DrawTextLayout( - this TTarget target, - PointF origin, - TLayout textLayout, - TBrush defaultFillBrush, - DrawTextOptions options = DrawTextOptions.None) - where TTarget : IPointer - where TLayout : IPointer - where TBrush : IPointer - { - target.Pointer->DrawTextLayout( - origin, - textLayout.Pointer, - defaultFillBrush.Pointer, - (D2D1_DRAW_TEXT_OPTIONS)options); - - GC.KeepAlive(target); - GC.KeepAlive(textLayout); - GC.KeepAlive(defaultFillBrush); - } - - public static void DrawBitmap( - this TRenderTarget target, - TBitmap bitmap, - RectangleF destinationRectangle = default, - float opacity = 1.0f, - BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.Linear) - where TRenderTarget : IPointer - where TBitmap : IPointer - { - D2D_RECT_F destination = (D2D_RECT_F)destinationRectangle; - if (destinationRectangle.IsEmpty) - { - D2D_SIZE_F size = target.Pointer->GetSizeHack(); - destination = new D2D_RECT_F { left = 0, top = 0, right = size.width, bottom = size.height }; - } - else - { - destination = (D2D_RECT_F)destinationRectangle; - } - - target.Pointer->DrawBitmap( - bitmap.Pointer, - &destination, - opacity, - (D2D1_BITMAP_INTERPOLATION_MODE)interpolationMode, - null); - - GC.KeepAlive(target); - GC.KeepAlive(bitmap); - } -} \ No newline at end of file diff --git a/src/thirtytwo/Win32/Graphics/Imaging/FormatConverter.cs b/src/thirtytwo/Win32/Graphics/Imaging/FormatConverter.cs index fd43604..55c28b7 100644 --- a/src/thirtytwo/Win32/Graphics/Imaging/FormatConverter.cs +++ b/src/thirtytwo/Win32/Graphics/Imaging/FormatConverter.cs @@ -13,7 +13,6 @@ public FormatConverter(IWICFormatConverter* pointer) : base((IWICBitmapSource*)p public FormatConverter() : this(Create()) { } public FormatConverter(BitmapSource source) : this(Create()) => Initialize(source); - private static IWICFormatConverter* Create() { IWICFormatConverter* converter;