示例#1
0
        public void Draw(DrawingContext gfx, TileMap map)
        {
            if (map.Sprite != null)
            {
                if (tiles == null)
                {
                    tiles = new RenderTargetBitmap(new PixelSize(map.Width, map.Height));
                    using DrawingContext mgfx = new DrawingContext(tiles.CreateDrawingContext(null));
                    //mgfx.Clip = new Region(new Rectangle(new Point(), new Size(map.Width, map.Height)));
                    int x = 0;
                    int y = 0;
                    foreach (int tile in map.Tiles)
                    {
                        using MemoryStream stream = new MemoryStream();
                        map.Image(tile).Save(stream, ImageFormat.Png);
                        stream.Position = 0;
                        mgfx.DrawImage(new Avalonia.Media.Imaging.Bitmap(stream), new Rect(0, 0, map.Sprite.Width, map.Sprite.Height), new Rect(x * map.Sprite.Width, y * map.Sprite.Height, map.Sprite.Width, map.Sprite.Height));
                        x++;
                        if (x >= map.Columns)
                        {
                            x = 0;
                            y++;
                        }
                    }
                }

                gfx?.DrawImage(tiles, new Rect(0, 0, tiles.Size.Width, tiles.Size.Height), new Rect(0, 0, tiles.Size.Width, tiles.Size.Height));
            }
        }
示例#2
0
        public void FramebufferRenderResultsShouldBeUsableAsBitmap(PixelFormat fmt)
        {
            var testName = nameof(FramebufferRenderResultsShouldBeUsableAsBitmap) + "_" + fmt;
            var fb       = new Framebuffer(fmt, 80, 80);
            var r        = Avalonia.AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();

            using (var target = r.CreateRenderTarget(new object[] { fb }))
                using (var ctx = target.CreateDrawingContext(null))
                {
                    ctx.PushOpacity(0.8);
                    ctx.FillRectangle(Brushes.Chartreuse, new Rect(0, 0, 20, 100));
                    ctx.FillRectangle(Brushes.Crimson, new Rect(20, 0, 20, 100));
                    ctx.FillRectangle(Brushes.Gold, new Rect(40, 0, 20, 100));
                }

            var bmp = new Bitmap(fmt, fb.Address, fb.Width, fb.Height, fb.RowBytes);

            fb.Deallocate();
            using (var rtb = new RenderTargetBitmap(100, 100))
            {
                using (var ctx = rtb.CreateDrawingContext(null))
                {
                    ctx.FillRectangle(Brushes.Blue, new Rect(0, 0, 100, 100));
                    ctx.FillRectangle(Brushes.Pink, new Rect(0, 20, 100, 10));

                    var rc = new Rect(0, 0, 60, 60);
                    ctx.DrawImage(bmp.PlatformImpl, 1, rc, rc);
                }
                rtb.Save(System.IO.Path.Combine(OutputPath, testName + ".out.png"));
            }
            CompareImages(testName);
        }
示例#3
0
        public void Draw(object context, double width, double height, double dx, double dy, double zx, double zy)
        {
            if (context is DrawingContext drawingContext && width > 0 && height > 0)
            {
                if (_renderTarget == null)
                {
                    _renderTarget = new RenderTargetBitmap(new PixelSize((int)width, (int)height), new Vector(96, 96));
                }
                else if (_renderTarget.PixelSize.Width != (int)width || _renderTarget.PixelSize.Height != (int)height)
                {
                    _renderTarget.Dispose();
                    _renderTarget = new RenderTargetBitmap(new PixelSize((int)width, (int)height), new Vector(96, 96));
                }

                using (var drawingContextImpl = _renderTarget.CreateDrawingContext(null))
                {
                    var skiaDrawingContextImpl = drawingContextImpl as ISkiaDrawingContextImpl;

                    _editorContainerPresenter.Draw(skiaDrawingContextImpl.SkCanvas, width, height, dx, dy, zx, zy);

                    drawingContext.DrawImage(_renderTarget, 1.0,
                                             new Rect(0, 0, _renderTarget.PixelSize.Width, _renderTarget.PixelSize.Height),
                                             new Rect(0, 0, width, height));
                }
            }
        }
示例#4
0
        public override void Render(DrawingContext context)
        {
            if (sketchDraw.Sketch.HasNewSize || currentRTB is null)
            {
                if (sketchDraw.Sketch.size.Width == 0 ||
                    sketchDraw.Sketch.size.Height == 0)
                {
                    sketchDraw.Sketch.size = new SKRect(0, 0, 1, 1);
                }

                var newRTB =
                    new RenderTargetBitmap(new PixelSize((int)sketchDraw.Sketch.size.Width, (int)sketchDraw.Sketch.size.Height));

                if (!(currentRTB is null))
                {
                    using (var currentRTBContext = currentRTB.CreateDrawingContext(null))
                        using (var newRTBContext = newRTB.CreateDrawingContext(null))
                        {
                            var src = new Rect(new Point(0, 0), currentRTB.Size);
                            var dst = new Rect(new Point(0, 0), newRTB.Size);

                            newRTBContext.DrawBitmap(currentRTB.PlatformImpl, 1, src, dst, BitmapInterpolationMode.Default);
                        }
                    currentRTB?.Dispose();
                }

                currentRTB = newRTB;
                sketchDraw.Sketch.HasNewSize = false;

                Dispatcher.UIThread.Post(() =>
                {
                    Width  = sketchDraw.Sketch.size.Width;
                    Height = sketchDraw.Sketch.size.Height;
                });
            }

            using (var currentRTBContext = currentRTB.CreateDrawingContext(null))
            {
                currentRTBContext.Custom(sketchDraw);
            }

            var srcdst = new Rect(new Point(0, 0), currentRTB.Size);

            context.PlatformImpl.DrawBitmap(currentRTB.PlatformImpl, 1, srcdst, srcdst, BitmapInterpolationMode.Default);
            base.Render(context);
        }
示例#5
0
 public override void Render(DrawingContext context)
 {
     using (var ctxi = _bitmap.CreateDrawingContext(null))
         using (var ctx = new DrawingContext(ctxi, false))
             using (ctx.PushPostTransform(Matrix.CreateTranslation(-100, -100)
                                          * Matrix.CreateRotation(_st.Elapsed.TotalSeconds)
                                          * Matrix.CreateTranslation(100, 100)))
             {
                 ctxi.Clear(default);
示例#6
0
        static void SaveDrawing(Drawing drawing, int size, Stream pngStream)
        {
            var target = new RenderTargetBitmap(new PixelSize(size, size));

            var(drawingsize, transform) = CalculateSizeAndTransform(target.Size, drawing.GetBounds(), Stretch.Uniform);

            using (var ctxi = target.CreateDrawingContext(null))
                using (var ctx = new DrawingContext(ctxi, false))
                {
                    using (ctx.PushPreTransform(transform))
                        drawing.Draw(ctx);
                }
            target.Save(pngStream);
        }
示例#7
0
    private RenderTargetBitmap GetSelectedBitmap()
    {
        if (_selectionRect.Width < MinClipboardSize || _selectionRect.Height < MinClipboardSize)
        {
            return(null);
        }

        var destRect = new Rect(0, 0, _selectionRect.Width, _selectionRect.Height);

        var bitmap = new RenderTargetBitmap(new PixelSize((int)destRect.Width, (int)destRect.Height), new Vector(96, 96));

        using (var ctx = bitmap.CreateDrawingContext(null))
        {
            ctx.DrawBitmap(_originalBitmap.PlatformImpl, 1, _selectionRect, destRect);
        };
        return(bitmap);
    }
示例#8
0
        public Form1()
        {
            InitializeComponent();

            _render = new PlatformRenderInterface();
            PerspexLocator.CurrentMutable.Bind <IPlatformRenderInterface>().ToConstant(_render);
            _geometry     = new StreamGeometry();
            _rbitmap      = new RenderTargetBitmap(50, 50);
            _renderTarget = _render.CreateRenderer(new PlatformHandle(Handle, "HWND"), ClientSize.Width,
                                                   ClientSize.Height);
            var timer = new Timer()
            {
                Interval = 20
            };

            timer.Tick += delegate { Invalidate(); };
            timer.Start();
            components.Add(timer);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            using (var ctx = _geometry.Open())
            {
                ctx.BeginFigure(new Point(10, 10), true);
                ctx.LineTo(new Point(40, 25));
                ctx.BezierTo(new Point(50, 45), new Point(43, 48), new Point(20, 90));
                ctx.LineTo(new Point(10, 60));
                ctx.EndFigure(true);
            }

            _text =
                new FormattedText(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
                    "Arial", 25, FontStyle.Normal, TextAlignment.Left, FontWeight.Normal);

            _text.Constraint = new Size(400, double.PositiveInfinity);

            using (var ctx = _rbitmap.CreateDrawingContext())
                ctx.DrawRectangle(new Pen(new SolidColorBrush(Colors.Aqua)), new Rect(10, 10, 30, 30), 5);

            _bitmap = new Bitmap(@"C:\Users\keks\Desktop\phoenix.png");
        }
示例#9
0
        public void Draw(object context, double width, double height, double dx, double dy, double zx, double zy, double renderScaling)
        {
            if (context is DrawingContext drawingContext && width > 0 && height > 0)
            {
                if (_renderTarget == null)
                {
                    _renderScaling = renderScaling;
                    _renderTarget  = new RenderTargetBitmap(
                        new PixelSize((int)(width / _renderScaling), (int)(height / _renderScaling)),
                        new Vector(96, 96));
                }
                else if (_renderTarget.PixelSize.Width != (int)width || _renderTarget.PixelSize.Height != (int)height || Math.Abs(_renderScaling - renderScaling) > double.Epsilon)
                {
                    _renderScaling = renderScaling;
                    _renderTarget.Dispose();
                    _renderTarget = new RenderTargetBitmap(
                        new PixelSize((int)(width / _renderScaling), (int)(height / _renderScaling)),
                        new Vector(96, 96));
                }

                using var drawingContextImpl = _renderTarget.CreateDrawingContext(null);
                var skiaDrawingContextImpl = drawingContextImpl as ISkiaDrawingContextImpl;

                if (skiaDrawingContextImpl?.SkCanvas is { } skCanvas)
                {
                    var skMatrix = SKMatrix.CreateScale((float)(1.0 / renderScaling), (float)(1.0 / renderScaling));

                    skCanvas.Save();
                    skCanvas.SetMatrix(skMatrix);

                    _editorContainerPresenter.Draw(skCanvas, width, height, dx, dy, zx, zy, renderScaling);

                    skCanvas.Restore();
                }

                drawingContext.DrawImage(_renderTarget,
                                         new Rect(0, 0, _renderTarget.PixelSize.Width, _renderTarget.PixelSize.Height),
                                         new Rect(0, 0, _renderTarget.PixelSize.Width, _renderTarget.PixelSize.Height));
            }
        }
示例#10
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;
        }
示例#11
0
    private void AddBackgroundImage(IVisual visual)
    {
        var bounds = visual.Bounds;

        _originalBitmap = new RenderTargetBitmap(new PixelSize((int)bounds.Right, (int)bounds.Bottom), new Vector(96, 96));
        _originalBitmap.Render(visual);

        _backgroundBitmap = new RenderTargetBitmap(new PixelSize((int)bounds.Width, (int)bounds.Height), new Vector(96, 96));

        using (var ctx = _backgroundBitmap.CreateDrawingContext(null))
        {
            ctx.DrawBitmap(_originalBitmap.PlatformImpl, 0.5, bounds, bounds);
        }

        _backgroundImage = new Image()
        {
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment   = VerticalAlignment.Top,
            Stretch             = Stretch.None,
            Source = _backgroundBitmap,
        };
        _contentGrid.Children.Add(_backgroundImage);
    }
示例#12
0
    private void UpdateSelectionImage()
    {
        Size sourceSize = _originalBitmap.Size;

        _selectionBitmap = new RenderTargetBitmap(new PixelSize((int)sourceSize.Width, (int)sourceSize.Height), new Vector(96, 96));

        var borderRect = new Rect(
            new Point(
                Math.Max(2, _selectionRect.Left),
                Math.Max(2, _selectionRect.Top)),
            _selectionRect.BottomRight);

        var brush    = Theme.ToolbarLabelForeground;
        var innerPen = new Pen(Brushes.Black, 2, lineCap: PenLineCap.Square);
        var outerPen = new Pen(brush, 4, lineCap: PenLineCap.Square);

        using (var ctx = _selectionBitmap.CreateDrawingContext(null))
        {
            ctx.DrawBitmap(_originalBitmap.PlatformImpl, 1, _selectionRect, _selectionRect);
            ctx.DrawRectangle(null, outerPen, borderRect.Inflate(1));
            ctx.DrawRectangle(null, innerPen, borderRect);
        }
        _selectionImage.Source = _selectionBitmap;
    }
 public override void Render(DrawingContext context)
 {
     using (var ctxi = _bitmap.CreateDrawingContext(null))
         using (var bitmapCtx = new DrawingContext(ctxi, false))
         {
             ctxi.Clear(default);
示例#14
0
 public AvaloniaBitmap(RenderTargetBitmap bmp)
 {
     this.bmp = bmp;
     gfx      = new DrawingContext(bmp.CreateDrawingContext(null));
 }