使用RadialGradientBrush绘图的DrawingBrush只能显示最后一个

时间:2011-08-20 03:02:17

标签: wpf drawingbrush

有问题。我构建了一个DrawingBrush,其Drawing是一个DrawingGroup,子组可以更改。所以我在后面的代码中构建它。 drawing.Brush是RadialGradientBrush。

我在组中添加了5张图纸,但它只能显示最后一张图纸。但是xaml可以显示所有这些。我发现这是我的RadialGradientBrush的问题,但我找不到我的代码的错误。你可以帮帮我吗 ?非常感谢,这是我的代码。

<Rectangle Name="rect">
                <Rectangle.Fill>
                    <DrawingBrush x:Name="totalBrush">
                        <DrawingBrush.Drawing>
                            <DrawingGroup>
                                <GeometryDrawing x:Name="geometry1">
                                    <GeometryDrawing.Brush>
                                        <RadialGradientBrush x:Name="Brush1" GradientOrigin="0.2,0.2"  Center="0.2,0.2" RadiusX="0.5" RadiusY="0.5">
                                            <RadialGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFF0000" Offset="0" />
                                                <GradientStop Color="#000000FF" Offset="1" />
                                            </RadialGradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </GeometryDrawing.Brush>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing>
                                    <GeometryDrawing.Brush>
                                        <RadialGradientBrush x:Name="Brush2" GradientOrigin="0.8,0.2"  Center="0.8,0.2" RadiusX="0.5" RadiusY="0.5">
                                            <RadialGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFF0000" Offset="0" />
                                                <GradientStop Color="#000000FF" Offset="1" />
                                            </RadialGradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </GeometryDrawing.Brush>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing>
                                    <GeometryDrawing.Brush>
                                        <RadialGradientBrush x:Name="Brush3" GradientOrigin="0.5,0.5"  Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
                                            <RadialGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFF0000" Offset="0" />
                                                <GradientStop Color="#000000FF" Offset="1" />
                                            </RadialGradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </GeometryDrawing.Brush>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing>
                                    <GeometryDrawing.Brush>
                                        <RadialGradientBrush x:Name="Brush4" GradientOrigin="0.2,0.8"  Center="0.2,0.8" RadiusX="0.5" RadiusY="0.5">
                                            <RadialGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFF0000" Offset="0" />
                                                <GradientStop Color="#000000FF" Offset="1" />
                                            </RadialGradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </GeometryDrawing.Brush>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing>
                                    <GeometryDrawing.Brush>
                                        <RadialGradientBrush x:Name="Brush5" GradientOrigin="0.8,0.8"  Center="0.8,0.8" RadiusX="0.5" RadiusY="0.5">
                                            <RadialGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFF0000" Offset="0" />
                                                <GradientStop Color="#000000FF" Offset="1" />
                                            </RadialGradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </GeometryDrawing.Brush>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                            </DrawingGroup>
                        </DrawingBrush.Drawing>
                    </DrawingBrush>
                </Rectangle.Fill>
            </Rectangle>



private void InitializeTemperaturePoints()
    {
        DrawingGroup group = new DrawingGroup();
        DrawingBrush drawingBrush = new DrawingBrush();
        var drawing1 = GenerateDrawing(0.2, 0.2);
        var drawing2 = GenerateDrawing(0.8, 0.2);
        var drawing3 = GenerateDrawing(0.5, 0.5);
        var drawing4 = GenerateDrawing(0.2, 0.8);
        var drawing5 = GenerateDrawing(0.8, 0.8);
        //var drawing1 = GenerateDrawing(1);
        //var drawing2 = GenerateDrawing(2);
        //var drawing3 = GenerateDrawing(3);
        //var drawing4 = GenerateDrawing(4);
        //var drawing5 = GenerateDrawing(5);
        group.Children.Add(drawing1);
        group.Children.Add(drawing2);
        group.Children.Add(drawing3);
        group.Children.Add(drawing4);
        group.Children.Add(drawing5);
        drawingBrush.Drawing = group;
        rect1.Fill = drawingBrush;
    }

    GeometryDrawing GenerateDrawing(double x, double y)
    {
        RadialGradientBrush brush = new RadialGradientBrush();
        brush.GradientOrigin = new Point(x, y);
        brush.Center = new Point(x, y);
        brush.RadiusX = 0.5;
        brush.RadiusY = 0.5;
        GradientStop stop1 = new GradientStop(Colors.Red, 0);
        GradientStop stop2 = new GradientStop(Colors.Blue, 1);
        if (brush.GradientStops == null)
        {
            brush.GradientStops = new GradientStopCollection();
        }
        brush.GradientStops.Add(stop1);
        brush.GradientStops.Add(stop2);
        brush.Opacity = 1;
        GeometryDrawing drawing = new GeometryDrawing();
        Rect rectangle = new Rect(0, 0, 1, 1);
        RectangleGeometry geometry = new RectangleGeometry(rectangle);
        drawing.Geometry = geometry;
        drawing.Brush = brush;
        return drawing;
    }

    GeometryDrawing GenerateDrawing(int index)
    {
        Brush brush = null;
        switch (index)
        {
            case 1: brush = Brush1; break;
            case 2: brush = Brush2; break;
            case 3: brush = Brush3; break;
            case 4: brush = Brush4; break;
            case 5: brush = Brush5; break;
        }
        GeometryDrawing drawing = new GeometryDrawing();
        Rect rectangle = new Rect(0, 0, 1, 1);
        RectangleGeometry geometry = new RectangleGeometry(rectangle);
        drawing.Geometry = geometry;
        drawing.Brush = brush;
        return drawing;
    }
}

1 个答案:

答案 0 :(得分:0)

画笔之间的区别在于XAML中的画笔部分是透明的:

<RadialGradientBrush x:Name="Brush1" 
                     GradientOrigin="0.2,0.2"
                     Center="0.2,0.2"
                     RadiusX="0.5"
                     RadiusY="0.5">
    <RadialGradientBrush.GradientStops>
        <GradientStop Color="#FFFF0000" Offset="0" />
        <GradientStop Color="#000000FF" Offset="1" />
    </RadialGradientBrush.GradientStops>
</RadialGradientBrush>

代码中的画笔不会导致最后一个矩形过度绘制其他矩形:

GradientStop stop1 = new GradientStop(Colors.Red, 0);
GradientStop stop2 = new GradientStop(Colors.Blue, 1);

解决这个问题:

GradientStop stop1 = new GradientStop(Colors.Red, 0);
GradientStop stop2 = new GradientStop(Color.FromArgb(0, 0, 0, 1), 1);