在后面的代码中使用绘图图像作为矩形

时间:2012-11-20 08:10:48

标签: wpf

我在XAML中创建了一个DrawingImage并将其设置为一个矩形

XAML:

<DrawingBrush.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Geometry="M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z ">
                        <GeometryDrawing.Brush>
                            <LinearGradientBrush StartPoint="0,0.0215053" EndPoint="1,1">
                                <GradientStop Color="#FFFFFFFF" Offset="0.00847458" />
                                <GradientStop Color="#FF949063" Offset="0.800847" />
                            </LinearGradientBrush>
                        </GeometryDrawing.Brush>
                    </GeometryDrawing>
                    <GeometryDrawing Brush="#FF85825F" Geometry="F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z " />
                    <GeometryDrawing Brush="#FF524F40" Geometry="F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z " />
                    <GeometryDrawing Brush="#FF6D6B51" Geometry="F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359" />
                    <GeometryDrawing Brush="#FF524F40" Geometry="F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847" />
                    <GeometryDrawing Brush="#FFFFFFFF" Geometry="M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z ">
                        <GeometryDrawing.Pen>
                            <Pen LineJoin="Round" Brush="#FFC0C1C3" />
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingGroup>
            </DrawingBrush.Drawing>

我想把它设置为后面的代码。

我使用此代码但不显示DrawingImage。

代码隐藏

 GradientStopCollection collection = new GradientStopCollection();
            collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFFFFFF"), 0.00847458));
            collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF949063"), 0.800847));
            LinearGradientBrush gbrush = new LinearGradientBrush(collection, new System.Windows.Point(0, 0.0215053), new System.Windows.Point(1, 1));

            var dGroup = new DrawingGroup();
            using (DrawingContext dc = dGroup.Open())
            {
                dc.DrawGeometry(gbrush, null, Geometry.Parse("M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z "));
                dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF85825F")), null, Geometry.Parse("F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z "));
                dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z "));
                dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF6D6B51")), null, Geometry.Parse("F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359"));
                dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847"));
                dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFFFF")), new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC0C1C3")), 0) { LineJoin = PenLineJoin.Round }, Geometry.Parse("M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z "));
            }

            return new DrawingImage(dGroup);

1 个答案:

答案 0 :(得分:0)

您可以执行类似

的操作
    private DrawingBrush GetDBrush()
    {
        GradientStopCollection collection = new GradientStopCollection();
        collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFFFFFF"), 0.00847458));
        collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF949063"), 0.800847));
        LinearGradientBrush gbrush = new LinearGradientBrush(collection, new System.Windows.Point(0, 0.0215053), new System.Windows.Point(1, 1));

        var dGroup = new DrawingGroup();
        using (DrawingContext dc = dGroup.Open())
        {
            dc.DrawGeometry(gbrush, null, Geometry.Parse("M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z "));
            dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF85825F")), null, Geometry.Parse("F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z "));
            dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z "));
            dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF6D6B51")), null, Geometry.Parse("F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359"));
            dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847"));
            dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFFFF")), new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC0C1C3")), 0) { LineJoin = PenLineJoin.Round }, Geometry.Parse("M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z "));
        }

        return new DrawingBrush(dGroup);
    }

要在Rectangle中显示它,请使用Fill-property:

Rectangle.Fill = GetDBrush();