Ejemplo n.º 1
0
        public static Canvas ToCanvas(this BookPage bookPage, PaperSize paperSize)
        {
            double width  = paperSize.GetWidthPixel();
            double height = paperSize.GetHeightPiexl();

            Canvas canvas = new Canvas()
            {
                Width  = width,
                Height = height
            };

            canvas.Background = bookPage.Background == null
                ? (Brush)Brushes.White
                : new ImageBrush((BitmapSource) new ImageSourceConverter().ConvertFrom(bookPage.Background))
            {
                Stretch = Stretch.Fill
            };


            foreach (UIElement element in bookPage.ToUIElementCollection())
            {
                canvas.Children.Add(element);
            }

            // Viewbox can render controls in code behind
            var viewbox = new Viewbox()
            {
                Child = canvas
            };

            viewbox.Measure(new Size(width, height));
            viewbox.Arrange(new Rect(0, 0, width, height));
            viewbox.UpdateLayout();
            viewbox.Child = null;
            return(canvas);
        }