Example #1
0
        public static void DrawText(this ID2D1RenderTarget renderTarget,
                                    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 (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

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

            if (text == null)
            {
                return;
            }

            renderTarget.DrawTextW(text, (uint)text.Length, format, ref rect, brush, options, measuringMode);
        }
        public static async Task <ID2D1Bitmap> LoadBitmapFromFileAsync(
            this D2DImageLoader loader,
            ID2D1RenderTarget renderTarget,
            string filePath,
            Guid?format = null,
            WICBitmapDitherType dither            = WICBitmapDitherType.None,
            double alphaThresholdPercent          = 0,
            WICBitmapPaletteType paletteTranslate = WICBitmapPaletteType.Custom,
            IWICPalette palette = null)
        {
            if (!format.HasValue)
            {
                format = PixelFormatGUID.Format32bppPBGRA;
            }

            ID2D1Bitmap bitmap = await Task.Run(() =>
            {
                return(loader.LoadBitmapFromFile(
                           renderTarget,
                           filePath,
                           format,
                           dither,
                           alphaThresholdPercent,
                           paletteTranslate,
                           palette
                           ));
            }).ConfigureAwait(false);

            return(bitmap);
        }
Example #3
0
        public static void PopLayer(this ID2D1RenderTarget context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            context.SetTags(tag1, tag2);
        }
Example #5
0
        public static void PushAxisAlignedClip(this ID2D1RenderTarget renderTarget, D2D_RECT_F clipRect, D2D1_ANTIALIAS_MODE antialiasMode = D2D1_ANTIALIAS_MODE.D2D1_ANTIALIAS_MODE_PER_PRIMITIVE)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            renderTarget.PushAxisAlignedClip(ref clipRect, antialiasMode);
        }
Example #6
0
        public static void PopAxisAlignedClip(this ID2D1RenderTarget renderTarget)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            renderTarget.PopAxisAlignedClip();
        }
Example #7
0
        public static void PushLayer(this ID2D1RenderTarget context, D2D1_LAYER_PARAMETERS parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.PushLayer(ref parameters, null);
        }
Example #8
0
        public static IComObject <T> CreateBitmap <T>(this ID2D1RenderTarget renderTarget, D2D_SIZE_U size, IntPtr srcData, uint pitch, D2D1_BITMAP_PROPERTIES properties) where T : ID2D1Bitmap
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

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

            context.GetPixelFormat(out var format);
            return(format);
        }
Example #10
0
        public static D2D_SIZE_F GetSize(this ID2D1RenderTarget context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.GetSize(out var size);
            return(size);
        }
Example #11
0
        public static void SetTarget(this ID2D1RenderTarget context, ID2D1Image target)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            context.SetTarget(target);
        }
Example #12
0
        public static void FillEllipse(this ID2D1RenderTarget 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);
        }
Example #13
0
        public static void DrawLine(this ID2D1RenderTarget 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 #14
0
        public static void DrawEllipse(this ID2D1RenderTarget 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);
        }
Example #15
0
        public static void FillRectangle(this ID2D1RenderTarget 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);
        }
Example #16
0
        public static void DrawRectangle(this ID2D1RenderTarget 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);
        }
Example #17
0
        public static void Clear(this ID2D1RenderTarget renderTarget, _D3DCOLORVALUE?clearColor = null)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            if (clearColor.HasValue)
            {
                using (var cc = clearColor.Value.StructureToMemory())
                {
                    renderTarget.Clear(cc.Pointer);
                }
                return;
            }
            renderTarget.Clear(IntPtr.Zero);
        }
Example #18
0
        public static void DrawGeometry(this ID2D1RenderTarget renderTarget,
                                        ID2D1Geometry geometry,
                                        ID2D1Brush brush,
                                        float strokeWidth,
                                        ID2D1StrokeStyle strokeStyle = null)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

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

            renderTarget.DrawGeometry(geometry, brush, strokeWidth, strokeStyle);
        }
Example #19
0
        public static void DrawTextLayout(this ID2D1RenderTarget 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);
        }
Example #20
0
        public static void FillGeometry(this ID2D1RenderTarget context, ID2D1Geometry geometry, ID2D1Brush brush, ID2D1Brush opacityBrush = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

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

            context.FillGeometry(geometry, brush, opacityBrush);
        }
Example #21
0
        private ID2D1Bitmap LoadD2D1BitmapFromBitmap(ID2D1RenderTarget renderTarget, Bitmap origin)
        {
            Bitmap destBitmap;


            if (origin.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
            {
                destBitmap = new Bitmap(origin.Width, origin.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                using (Graphics g = Graphics.FromImage(destBitmap))
                {
                    g.DrawImage(origin, 0, 0);
                    g.Flush();
                }
            }
            else
            {
                destBitmap = (Bitmap)origin.Clone();
            }

            var bmpData = destBitmap.LockBits(new Rectangle(0, 0, destBitmap.Width, destBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, destBitmap.PixelFormat);

            int buffSize = bmpData.Stride * destBitmap.Height;

            byte[] byteData = new byte[buffSize];

            Marshal.Copy(bmpData.Scan0, byteData, 0, buffSize);

            destBitmap.UnlockBits(bmpData);


            var bmpProps = new BitmapProperties
            {
                PixelFormat = new Vortice.DCommon.PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                DpiX        = 96,
                DpiY        = 96
            };

            var d2d1Bmp = renderTarget.CreateBitmap(new Vortice.Mathematics.Size(destBitmap.Width, destBitmap.Height), IntPtr.Zero, bmpData.Stride, bmpProps);

            d2d1Bmp.CopyFromMemory(byteData, bmpData.Stride);

            return(d2d1Bmp);
        }
Example #22
0
        public static IComObject <T> CreateSolidColorBrush <T>(this ID2D1RenderTarget renderTarget, _D3DCOLORVALUE color, D2D1_BRUSH_PROPERTIES?properties = null) where T : ID2D1SolidColorBrush
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            if (properties.HasValue)
            {
                using (var props = properties.Value.StructureToMemory())
                {
                    renderTarget.CreateSolidColorBrush(ref color, props.Pointer, out var brush).ThrowOnError();
                    return(new ComObject <T>((T)brush));
                }
            }

            renderTarget.CreateSolidColorBrush(ref color, IntPtr.Zero, out var brush2).ThrowOnError();
            return(new ComObject <T>((T)brush2));
        }
Example #23
0
        public static void DrawBitmap(this ID2D1RenderTarget renderTarget,
                                      ID2D1Bitmap bitmap,
                                      float opacity = 1,
                                      D2D1_BITMAP_INTERPOLATION_MODE interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE.D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                                      D2D_RECT_F?destinationRectangle = null,
                                      D2D_RECT_F?sourceRectangle      = null)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            using (var drc = destinationRectangle.StructureToMemory())
            {
                using (var src = sourceRectangle.StructureToMemory())
                {
                    renderTarget.DrawBitmap(bitmap, drc.Pointer, opacity, interpolationMode, src.Pointer);
                }
            }
        }
Example #24
0
        public static void WithAntialiasMode(this ID2D1RenderTarget context, D2D1_ANTIALIAS_MODE mode, Action action)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            var old = context.GetAntialiasMode();

            try
            {
                context.SetAntialiasMode(mode);
                action();
            }
            finally
            {
                context.SetAntialiasMode(old);
            }
        }
Example #25
0
        public static IComObject <ID2D1BitmapRenderTarget> CreateCompatibleRenderTarget(this ID2D1RenderTarget context, D2D_SIZE_F?desiredSize = null, D2D_SIZE_U?desiredPixelSize = null, D2D1_PIXEL_FORMAT?desiredFormat = null, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS options = D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            using (var desiredSizeMem = new ComMemory(desiredSize))
            {
                using (var desiredPixelSizeMem = new ComMemory(desiredPixelSize))
                {
                    using (var desiredFormatMem = new ComMemory(desiredFormat))
                    {
                        context.CreateCompatibleRenderTarget(desiredSizeMem.Pointer, desiredPixelSizeMem.Pointer, desiredFormatMem.Pointer, options, out var target).ThrowOnError();
                        return(new ComObject <ID2D1BitmapRenderTarget>(target));
                    }
                }
            }
        }
Example #26
0
        public static IComObject <ID2D1RadialGradientBrush> CreateRadialGradientBrush(this ID2D1RenderTarget context, D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES gradientBrushProperties, ID2D1GradientStopCollection stops, D2D1_BRUSH_PROPERTIES?brushProperties = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            using (var props = brushProperties.StructureToMemory())
            {
                context.CreateRadialGradientBrush(gradientBrushProperties, props.Pointer, stops, out var brush).ThrowOnError();
                return(new ComObject <ID2D1RadialGradientBrush>(brush));
            }
        }
Example #27
0
 public static void BeginDraw(this ID2D1RenderTarget renderTarget) => renderTarget?.BeginDraw();
Example #28
0
 public static void EndDraw(this ID2D1RenderTarget renderTarget) => renderTarget?.EndDraw(IntPtr.Zero, IntPtr.Zero).ThrowOnError();
 internal D2D1RenderTargetBase(ID2D1RenderTarget renderTarget)
 {
     this.renderTarget = renderTarget;
 }
Example #30
0
        public static async Task <int> EndDrawAsync(this ID2D1RenderTarget obj)
        {
            int hr = await Task.Run(obj.EndDraw).ConfigureAwait(false);

            return(hr);
        }
 internal D2D1RenderTargetBase(ID2D1RenderTarget renderTarget)
 {
     this.renderTarget = renderTarget;
 }