|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace uk.JohnCook.dotnet.MessageToImage |
| 8 | +{ |
| 9 | + public class Direct2DWrapper : IDisposable |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Instances of ID2D1Factory, IDWriteFactory, and IWICImagingFactory should be reused for the life of an application |
| 13 | + /// </summary> |
| 14 | + private Interop.Direct2DPointers direct2DPointers = new Interop.Direct2DPointers(); |
| 15 | + private bool disposedValue; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Constructor |
| 19 | + /// </summary> |
| 20 | + public Direct2DWrapper() |
| 21 | + { |
| 22 | + CreateDirect2DPointers(); |
| 23 | + |
| 24 | + // Original DLL existence test - README needs modifying if changed |
| 25 | + Trace.WriteLine(Interop.UnsafeNativeMethods.Add(3, 6)); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Create pointers for ID2D1Factory, IDWriteFactory, and IWICImagingFactory |
| 30 | + /// </summary> |
| 31 | + private void CreateDirect2DPointers() |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + Marshal.ThrowExceptionForHR(Interop.UnsafeNativeMethods.CreateD2D1Factory(ref direct2DPointers)); |
| 36 | + Marshal.ThrowExceptionForHR(Interop.UnsafeNativeMethods.CreateDWriteFactory(ref direct2DPointers)); |
| 37 | + Marshal.ThrowExceptionForHR(Interop.UnsafeNativeMethods.CreateImagingFactory(ref direct2DPointers)); |
| 38 | + } |
| 39 | + catch (COMException ce) |
| 40 | + { |
| 41 | + Interop.UnsafeNativeMethods.ReleaseDWriteFactory(direct2DPointers); |
| 42 | + Interop.UnsafeNativeMethods.ReleaseImagingFactory(direct2DPointers); |
| 43 | + Interop.UnsafeNativeMethods.ReleaseD2D1Factory(direct2DPointers); |
| 44 | + Trace.WriteLine($"COMException in {nameof(CreateDirect2DPointers)} - {ce.Message} - {ce.InnerException?.Message}"); |
| 45 | + throw; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Create a MessagePanel instance |
| 51 | + /// </summary> |
| 52 | + /// <param name="canvasSize">Size of the canvas</param> |
| 53 | + /// <param name="backgroundColor">Background color of the canvas</param> |
| 54 | + /// <returns></returns> |
| 55 | + public MessagePanel CreateMessagePanel(Interop.SizeU canvasSize, UInt32 backgroundColor) |
| 56 | + { |
| 57 | + return new MessagePanel( |
| 58 | + direct2DPointers: ref direct2DPointers, |
| 59 | + canvasSize: canvasSize, |
| 60 | + backgroundColor: backgroundColor |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + protected virtual void Dispose(bool disposing) |
| 65 | + { |
| 66 | + if (!disposedValue) |
| 67 | + { |
| 68 | + if (disposing) |
| 69 | + { |
| 70 | + // TODO: dispose managed state (managed objects) |
| 71 | + } |
| 72 | + |
| 73 | + // TODO: free unmanaged resources (unmanaged objects) and override finalizer |
| 74 | + Interop.UnsafeNativeMethods.ReleaseDWriteFactory(direct2DPointers); |
| 75 | + Interop.UnsafeNativeMethods.ReleaseImagingFactory(direct2DPointers); |
| 76 | + Interop.UnsafeNativeMethods.ReleaseD2D1Factory(direct2DPointers); |
| 77 | + // TODO: set large fields to null |
| 78 | + disposedValue = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources |
| 83 | + ~Direct2DWrapper() |
| 84 | + { |
| 85 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 86 | + Dispose(disposing: false); |
| 87 | + } |
| 88 | + |
| 89 | + public void Dispose() |
| 90 | + { |
| 91 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 92 | + Dispose(disposing: true); |
| 93 | + GC.SuppressFinalize(this); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments