コード例 #1
0
        private Brush MakeBrushFromVisual(Visual visual, Rect bounds, int dpi)
        {
            var viewport3D = visual as Viewport3D;

            if (viewport3D == null)
            {
                Drawing drawing = VisualTreeHelper.GetDrawing(visual);

                if (this.drawOutlines)
                {
                    bounds.Inflate(outlinePen.Thickness / 2, outlinePen.Thickness / 2);
                }

                var offsetMatrix          = new Matrix(1, 0, 0, 1, -bounds.Left, -bounds.Top);
                var offsetMatrixTransform = new MatrixTransform(offsetMatrix);
                offsetMatrixTransform.FreezeIfPossible();

                var drawingVisual = new DrawingVisual();

                using (var drawingContext = drawingVisual.RenderOpen())
                {
                    drawingContext.PushTransform(offsetMatrixTransform);

                    if (this.drawOutlines)
                    {
                        drawingContext.DrawRectangle(null, outlinePen, bounds);
                    }

                    drawingContext.DrawDrawing(drawing);
                    drawingContext.Pop();
                }

                visual = drawingVisual;
            }

            var renderTargetBitmap = VisualCaptureUtil.RenderVisual(visual, bounds.Size, dpi, viewport3D: viewport3D);

            renderTargetBitmap.FreezeIfPossible();
            var imageBrush = new ImageBrush(renderTargetBitmap);

            imageBrush.FreezeIfPossible();

            return(imageBrush);
        }