2011年5月5日

WPF TileBrush

WPF 的 TileBrush 有 BrushMappingMode.Absolute 與 BrushMappingMode.RelativeToBoundingBox 兩種模式:

Absolute 採用固定的像數,Tile 圖案不會隨著物件的伸展而改變大小與形狀:



RelativeToBoundingBox 則相對於物件的長寬比,因此數量是不變的:



除了 TileBrush.ViewportUnits 設定的差異外,若 Tile 的圖案採用向量而不是圖檔時,要特別注意 Absolute 的用法。Absolute 以像數計算,DrawingBrush 會畫不出東西來,必須轉成 ImageBrush 才能正常運作,以下是這兩段程式的差異。

BrushMappingMode.RelativeToBoundingBox:
 
        public MainWindow()
        {
            InitializeComponent();

            Rectangle exampleRectangle = new Rectangle();

            GeometryDrawing backgroundSquare =
                new GeometryDrawing(Brushes.White, null, new RectangleGeometry(new Rect(0, 0, 100, 100)));

            GeometryGroup aGeometryGroup = new GeometryGroup();
            aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 50, 50)));
            aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, 50, 50, 50)));

            LinearGradientBrush checkerBrush = new LinearGradientBrush();
            checkerBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
            checkerBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1.0));

            GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);

            DrawingGroup checkersDrawingGroup = new DrawingGroup();
            checkersDrawingGroup.Children.Add(backgroundSquare);
            checkersDrawingGroup.Children.Add(checkers);

            DrawingBrush myBrush = new DrawingBrush();
            myBrush.Drawing = checkersDrawingGroup;
            myBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
            myBrush.Viewport = new Rect(0, 0, 0.25, 0.25);
            myBrush.TileMode = TileMode.Tile;
            exampleRectangle.Fill = myBrush;

            TilePanel.Children.Add(exampleRectangle);
        }



BrushMappingMode.Absolute:
 
        public MainWindow()
        {
            InitializeComponent();

            Rectangle exampleRectangle = new Rectangle();

            GeometryDrawing backgroundSquare =
                new GeometryDrawing(Brushes.White, null, new RectangleGeometry(new Rect(0, 0, 100, 100)));

            GeometryGroup aGeometryGroup = new GeometryGroup();
            aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 50, 50)));
            aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, 50, 50, 50)));

            LinearGradientBrush checkerBrush = new LinearGradientBrush();
            checkerBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
            checkerBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1.0));

            GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);

            DrawingGroup checkersDrawingGroup = new DrawingGroup();
            checkersDrawingGroup.Children.Add(backgroundSquare);
            checkersDrawingGroup.Children.Add(checkers);

            DrawingImage image = new DrawingImage(checkersDrawingGroup);
            ImageBrush myBrush = new ImageBrush(image);
            myBrush.Stretch = Stretch.None;
            myBrush.TileMode = TileMode.Tile;
            myBrush.Viewport = new Rect(0, 0, 100, 100);
            myBrush.ViewportUnits = BrushMappingMode.Absolute;
            exampleRectangle.Fill = myBrush;

            TilePanel.Children.Add(exampleRectangle);
        }

[Reference]
WPF Brushes Overview
ImageBrush for a BackGround grid in WPF

1 則留言:

匿名 提到...

Unquestionably believe that which you said. Your favourite
justification appeared to be at the internet the simplest thing to remember of.
I say to you, I definitely get irked whilst people consider worries that they plainly don't understand about. You controlled to hit the nail upon the highest and defined out the whole thing without having side effect , other folks could take a signal. Will likely be again to get more. Thank you

my web page :: GFI Norte

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...