public IRenderTargetBitmapImpl CreateLayer(Size size)
 {
     if (_layerFactory != null)
     {
         return(_layerFactory.CreateLayer(size));
     }
     else
     {
         var platform  = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();
         var dpi       = new Vector(_deviceContext.DotsPerInch.Width, _deviceContext.DotsPerInch.Height);
         var pixelSize = PixelSize.FromSizeWithDpi(size, dpi);
         return(platform.CreateRenderTargetBitmap(pixelSize, dpi));
     }
 }
예제 #2
0
        /// <summary>
        /// Create new render target compatible with this drawing context.
        /// </summary>
        /// <param name="size">The size of the render target in DIPs.</param>
        /// <param name="format">Pixel format.</param>
        /// <returns></returns>
        private SurfaceRenderTarget CreateRenderTarget(Size size, PixelFormat?format = null)
        {
            var pixelSize  = PixelSize.FromSizeWithDpi(size, _dpi);
            var createInfo = new SurfaceRenderTarget.CreateInfo
            {
                Width  = pixelSize.Width,
                Height = pixelSize.Height,
                Dpi    = _dpi,
                Format = format,
                DisableTextLcdRendering = !_canTextUseLcdRendering,
                GrContext = _grContext
            };

            return(new SurfaceRenderTarget(createInfo));
        }
예제 #3
0
        //public MetryHolder(CTextBlock ctxt, int width = 400, int height = 1000)
        //{
        //    var reqSz = new Size(width, height);
        //
        //    ctxt.Measure(reqSz);
        //    ctxt.Arrange(new Rect(0, 0, width, ctxt.DesiredSize.Height == 0 ? height : ctxt.DesiredSize.Height));
        //    ctxt.Measure(reqSz);
        //
        //    var newReqSz = new Size(
        //        ctxt.DesiredSize.Width == 0 ? reqSz.Width : ctxt.DesiredSize.Width,
        //        ctxt.DesiredSize.Height == 0 ? reqSz.Height : ctxt.DesiredSize.Height);
        //    ctxt.Arrange(new Rect(0, 0, newReqSz.Width, newReqSz.Height));
        //
        //    var bitmap = new RenderTargetBitmap(PixelSize.FromSizeWithDpi(newReqSz, Dpi), Dpi);
        //
        //    using (var icontext = bitmap.CreateDrawingContext(null))
        //    using (var context = new DrawingContext(icontext))
        //    {
        //        ctxt.Render(context);
        //    }
        //
        //    Image = bitmap;
        //}

        public MetryHolder(Control ctxt, int width = 400, int height = 1000)
        {
            var reqSz = new Size(width, height);

            ctxt.Measure(reqSz);
            ctxt.Arrange(new Rect(0, 0, width, ctxt.DesiredSize.Height == 0 ? height : ctxt.DesiredSize.Height));
            ctxt.Measure(reqSz);

            var newReqSz = new Size(
                ctxt.DesiredSize.Width == 0 ? reqSz.Width : ctxt.DesiredSize.Width,
                ctxt.DesiredSize.Height == 0 ? reqSz.Height : ctxt.DesiredSize.Height);

            ctxt.Arrange(new Rect(0, 0, newReqSz.Width, newReqSz.Height));

            var bitmap = new RenderTargetBitmap(PixelSize.FromSizeWithDpi(newReqSz, Dpi), Dpi);

            using (var icontext = bitmap.CreateDrawingContext(null))
                using (var context = new DrawingContext(icontext))
                {
                    RenderHelper(ctxt, context);
                }
            Image = bitmap;
        }
        /// <summary>
        /// Creates a Direct2D brush wrapper for a Avalonia brush.
        /// </summary>
        /// <param name="brush">The avalonia brush.</param>
        /// <param name="destinationSize">The size of the brush's target area.</param>
        /// <returns>The Direct2D brush wrapper.</returns>
        public BrushImpl CreateBrush(IBrush brush, Size destinationSize)
        {
            var solidColorBrush     = brush as ISolidColorBrush;
            var linearGradientBrush = brush as ILinearGradientBrush;
            var radialGradientBrush = brush as IRadialGradientBrush;
            var imageBrush          = brush as IImageBrush;
            var visualBrush         = brush as IVisualBrush;

            if (solidColorBrush != null)
            {
                return(new SolidColorBrushImpl(solidColorBrush, _deviceContext));
            }
            else if (linearGradientBrush != null)
            {
                return(new LinearGradientBrushImpl(linearGradientBrush, _deviceContext, destinationSize));
            }
            else if (radialGradientBrush != null)
            {
                return(new RadialGradientBrushImpl(radialGradientBrush, _deviceContext, destinationSize));
            }
            else if (imageBrush?.Source != null)
            {
                return(new ImageBrushImpl(
                           imageBrush,
                           _deviceContext,
                           (BitmapImpl)imageBrush.Source.PlatformImpl.Item,
                           destinationSize));
            }
            else if (visualBrush != null)
            {
                if (_visualBrushRenderer != null)
                {
                    var intermediateSize = _visualBrushRenderer.GetRenderTargetSize(visualBrush);

                    if (intermediateSize.Width >= 1 && intermediateSize.Height >= 1)
                    {
                        // We need to ensure the size we're requesting is an integer pixel size, otherwise
                        // D2D alters the DPI of the render target, which messes stuff up. PixelSize.FromSize
                        // will do the rounding for us.
                        var dpi       = new Vector(_deviceContext.DotsPerInch.Width, _deviceContext.DotsPerInch.Height);
                        var pixelSize = PixelSize.FromSizeWithDpi(intermediateSize, dpi);

                        using (var intermediate = new BitmapRenderTarget(
                                   _deviceContext,
                                   CompatibleRenderTargetOptions.None,
                                   pixelSize.ToSizeWithDpi(dpi).ToSharpDX()))
                        {
                            using (var ctx = new RenderTarget(intermediate).CreateDrawingContext(_visualBrushRenderer))
                            {
                                intermediate.Clear(null);
                                _visualBrushRenderer.RenderVisualBrush(ctx, visualBrush);
                            }

                            return(new ImageBrushImpl(
                                       visualBrush,
                                       _deviceContext,
                                       new D2DBitmapImpl(intermediate.Bitmap),
                                       destinationSize));
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("No IVisualBrushRenderer was supplied to DrawingContextImpl.");
                }
            }

            return(new SolidColorBrushImpl(null, _deviceContext));
        }