public static void DrawText(this ID2D1DeviceContext device,
                                    string text,
                                    IDWriteTextFormat format,
                                    D2D_RECT_F rect,
                                    ID2D1Brush brush,
                                    D2D1_DRAW_TEXT_OPTIONS options      = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
                                    DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (text == null)
            {
                return;
            }

#pragma warning disable CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
            device.DrawTextW(text, (uint)text.Length, format, ref rect, brush, options, measuringMode);
#pragma warning restore CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
        }
Example #2
0
        public static void DrawImage(this ID2D1DeviceContext context,
                                     ID2D1Image image,
                                     D2D1_INTERPOLATION_MODE interpolationMode = D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR,
                                     D2D1_COMPOSITE_MODE compositeMode         = D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER,
                                     D2D_POINT_2F?targetOffset = null,
                                     D2D_RECT_F?imageRectangle = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            using (var irc = imageRectangle.StructureToMemory())
            {
                using (var to = targetOffset.StructureToMemory())
                {
                    context.DrawImage(image, to.Pointer, irc.Pointer, interpolationMode, compositeMode);
                }
            }
        }
        public static void DrawText(this ID2D1DeviceContext device,
                                    string text,
                                    IDWriteTextFormat format,
                                    D2D_RECT_F rect,
                                    ID2D1Brush brush,
                                    D2D1_DRAW_TEXT_OPTIONS options      = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
                                    DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (text == null)
            {
                return;
            }

            device.DrawTextW(text, (uint)text.Length, format, ref rect, brush, options, measuringMode);
        }
Example #4
0
        public static void DrawImage(this ID2D1DeviceContext context,
                                     ID2D1Effect effect,
                                     D2D1_INTERPOLATION_MODE interpolationMode = D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR,
                                     D2D1_COMPOSITE_MODE compositeMode         = D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER,
                                     D2D_POINT_2F?targetOffset = null,
                                     D2D_RECT_F?imageRectangle = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (effect == null)
            {
                throw new ArgumentNullException(nameof(effect));
            }

            using (var irc = imageRectangle.StructureToMemory())
            {
                using (var to = targetOffset.StructureToMemory())
                {
                    effect.GetOutput(out var image);
                    try
                    {
                        context.DrawImage(image, to.Pointer, irc.Pointer, interpolationMode, compositeMode);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(image);
                    }
                }
            }
        }
Example #5
0
        public static void PopLayer(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.PopLayer();
        }
Example #6
0
        public static void SetTags(this ID2D1DeviceContext context, ulong tag1, ulong tag2 = 0)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.SetTags(tag1, tag2);
        }
Example #7
0
        public static void PushLayer(this ID2D1DeviceContext context, D2D1_LAYER_PARAMETERS1 parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.PushLayer(ref parameters, null);
        }
        public static ComObject <T> CreateBitmap <T>(this ID2D1DeviceContext device, D2D_SIZE_U size, IntPtr srcData, uint pitch, D2D1_BITMAP_PROPERTIES1 properties) where T : ID2D1Bitmap
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            device.CreateBitmap(size, srcData, pitch, ref properties, out var bmp).ThrowOnError();
            return(new ComObject <T>((T)bmp));
        }
Example #9
0
        public static D2D_SIZE_F GetSize(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetSize(out var size);
            return(size);
        }
Example #10
0
        public static IComObject <ID2D1Image> GetTarget(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetTarget(out var target);
            return(target != null ? new ComObject <ID2D1Image>(target) : null);
        }
Example #11
0
        public static IComObject <ID2D1Factory> GetFactory(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetFactory(out var factory);
            return(factory != null ? new ComObject <ID2D1Factory>(factory) : null);
        }
Example #12
0
        public static IComObject <ID2D1Device> GetDevice(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetDevice(out var device);
            return(device != null ? new ComObject <ID2D1Device>(device) : null);
        }
Example #13
0
        public static IComObject <ID2D1Effect> CreateEffect(this ID2D1DeviceContext context, Guid id)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.CreateEffect(id, out var effect).ThrowOnError();
            return(effect != null ? new ComObject <ID2D1Effect>(effect) : null);
        }
Example #14
0
        public static D2D1_PIXEL_FORMAT GetPixelFormat(this ID2D1DeviceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetPixelFormat(out var format);
            return(format);
        }
Example #15
0
        private static ID2D1Bitmap1 CreateD2dBitmap(
            IWICImagingFactory imagingFactory,
            string filename,
            ID2D1DeviceContext renderTarget)
        {
            using IWICBitmapDecoder decoder   = imagingFactory.CreateDecoderFromFileName(filename);
            using IWICBitmapFrameDecode frame = decoder.GetFrame(0);

            using IWICFormatConverter converter = imagingFactory.CreateFormatConverter();
            converter.Initialize(frame, PixelFormat.Format32bppPBGRA, BitmapDitherType.None, null, 0, BitmapPaletteType.Custom);
            return(renderTarget.CreateBitmapFromWicBitmap(converter, null));
        }
Example #16
0
 public void InitializeDevice(IntPtr windowHandle)
 {
     D3Device = DirectXTools.CreateD3Device();
     {
         RenderTarget = DirectXTools.CreateRenderTarget(Direct2DFactory, D3Device);
         _solidBrush  = RenderTarget.CreateSolidColorBrush(Color4.Black);
         SwapChain    = DirectXTools.CreateSwapChainForHwnd(D3Device, windowHandle);
         DirectXTools.CreateDeviceSwapChainBitmap(SwapChain, RenderTarget);
         Bitmaps.SetRenderTarget(RenderTarget);
         TextLayouts.SetRenderTarget(RenderTarget);
     }
 }
Example #17
0
 public void InitializeDeviceGdiCompatible(IntPtr windowHandle, int width, int height)
 {
     D3Device = DirectXTools.CreateD3Device();
     {
         RenderTarget = DirectXTools.CreateRenderTarget(Direct2DFactory, D3Device);
         _solidBrush  = RenderTarget.CreateSolidColorBrush(Color4.Black);
         SwapChain    = DirectXTools.CreateSwapChainForHwnd(D3Device, windowHandle);
         //DirectXTools.CreateDeviceSwapChainBitmap(SwapChain, RenderTarget);
         DirectXTools.CreateDeviceContextCPUBitmap(RenderTarget, width, height);
         Bitmaps.SetRenderTarget(RenderTarget);
         TextLayouts.SetRenderTarget(RenderTarget);
     }
 }
        public static ComObject <T> CreateSolidColorBrush <T>(this ID2D1DeviceContext device, _D3DCOLORVALUE color, D2D1_BRUSH_PROPERTIES?properties = null) where T : ID2D1SolidColorBrush
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            using (var props = properties.StructureToMemory())
            {
                device.CreateSolidColorBrush(ref color, props.Pointer, out var brush).ThrowOnError();
                return(new ComObject <T>((T)brush));
            }
        }
Example #19
0
        public static void SetTarget(this ID2D1DeviceContext context, ID2D1Image target)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (target == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.SetTarget(target);
        }
        public static void FillEllipse(this ID2D1DeviceContext context, D2D1_ELLIPSE ellipse, ID2D1Brush brush)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (brush == null)
            {
                throw new ArgumentNullException(nameof(brush));
            }

            context.FillEllipse(ref ellipse, brush);
        }
        public static void FillRectangle(this ID2D1DeviceContext context, D2D_RECT_F rect, ID2D1Brush brush)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (brush == null)
            {
                throw new ArgumentNullException(nameof(brush));
            }

            context.FillRectangle(ref rect, brush);
        }
        public static void DrawEllipse(this ID2D1DeviceContext context, D2D1_ELLIPSE ellipse, ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (brush == null)
            {
                throw new ArgumentNullException(nameof(brush));
            }

            context.DrawEllipse(ref ellipse, brush, strokeWidth, strokeStyle);
        }
        public static void DrawRectangle(this ID2D1DeviceContext context, D2D_RECT_F rect, ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (brush == null)
            {
                throw new ArgumentNullException(nameof(brush));
            }

            context.DrawRectangle(ref rect, brush, strokeWidth, strokeStyle);
        }
        public static void DrawLine(this ID2D1DeviceContext context, D2D_POINT_2F point0, D2D_POINT_2F point1, ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (brush == null)
            {
                throw new ArgumentNullException(nameof(brush));
            }

            context.DrawLine(point0, point1, brush, strokeWidth, strokeStyle);
        }
Example #25
0
        public static void CreateDeviceContextCPUBitmap(
            ID2D1DeviceContext target, int width, int height)
        {
            BitmapProperties1 props = new()
            {
                BitmapOptions = BitmapOptions.Target | BitmapOptions.GdiCompatible,
                PixelFormat   = new Vortice.DCommon.PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied)
            };

            using (ID2D1Bitmap1 bitmap = target.CreateBitmap(new Size(width, height), IntPtr.Zero, 0, ref props))
            {
                target.Target = bitmap;
            }
        }
    }
Example #26
0
        public override void Draw(ID2D1DeviceContext renderTarget, ID2D1SolidColorBrush brush)
        {
            var lines = new[]
            {
                new [] { Points[0], Points[1] },
                new [] { Points[1], Points[2] },
                new [] { Points[2], Points[3] },
                new [] { Points[3], Points[0] },
            };

            foreach (var line in lines)
            {
                renderTarget.DrawLine(line[0].ToPoint(), line[1].ToPoint(), brush, 2.0f);
            }
        }
Example #27
0
 public static void CreateDeviceSwapChainBitmap(
     IDXGISwapChain1 swapChain,
     ID2D1DeviceContext target)
 {
     using (IDXGISurface surface = swapChain.GetBuffer <IDXGISurface>(0))
     {
         var props = new BitmapProperties1
         {
             BitmapOptions = BitmapOptions.Target | BitmapOptions.CannotDraw,
             PixelFormat   = new Vortice.DCommon.PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Ignore)
         };
         using (var bitmap = target.CreateBitmapFromDxgiSurface(surface, props))
         {
             target.Target = bitmap;
         }
     }
 }
Example #28
0
        public static void DrawTextLayout(this ID2D1DeviceContext context,
                                          D2D_POINT_2F origin,
                                          IDWriteTextLayout layout,
                                          ID2D1Brush defaultFillBrush    = null,
                                          D2D1_DRAW_TEXT_OPTIONS options = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (layout == null)
            {
                throw new ArgumentNullException(nameof(layout));
            }

            context.DrawTextLayout(origin, layout, defaultFillBrush, options);
        }
        public static ComObject <T> CreateBitmapFromDxgiSurface <T>(this ID2D1DeviceContext device, IDXGISurface surface, D2D1_BITMAP_PROPERTIES1?properties = null) where T : ID2D1Bitmap1
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (surface == null)
            {
                throw new ArgumentNullException(nameof(surface));
            }

            using (var mem = properties.StructureToMemory())
            {
                device.CreateBitmapFromDxgiSurface(surface, mem.Pointer, out var bmp).ThrowOnError();
                return(new ComObject <T>((T)bmp));
            }
        }
Example #30
0
        public static IComObject <T> CreateBitmapFromWicBitmap <T>(this ID2D1DeviceContext context, IWICBitmapSource source, D2D1_BITMAP_PROPERTIES1?properties = null) where T : ID2D1Bitmap1
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            using (var mem = properties.StructureToMemory())
            {
                context.CreateBitmapFromWicBitmap(source, mem.Pointer, out ID2D1Bitmap1 bmp).ThrowOnError();
                return(new ComObject <T>((T)bmp));
            }
        }