Esempio n. 1
0
        /// <summary>
        /// 获取画刷
        /// </summary>
        /// <returns>画刷</returns>
        private Brush GetBrush()
        {
            double           x1            = this.GetAttributeValue <SVGDouble>("x1", false, SVGDouble.Zero).GetValue(1);
            double           y1            = this.GetAttributeValue <SVGDouble>("y1", false, SVGDouble.Zero).GetValue(1);
            double           x2            = this.GetAttributeValue <SVGDouble>("x2", false, SVGDouble.One).GetValue(1);
            double           y2            = this.GetAttributeValue <SVGDouble>("y2", false, SVGDouble.Zero).GetValue(1);
            SVGGradientUnits gradientUnits = this.GetAttributeValue <SVGGradientUnits>("gradientUnits");
            SVGTransform     transform     = this.GetAttributeValue <SVGTransform>("gradientTransform");
            SVGSpreadMethod  spreadMethod  = this.GetAttributeValue <SVGSpreadMethod>("spreadMethod");

            LinearGradientBrush brush = new LinearGradientBrush();

            brush.StartPoint = new System.Windows.Point(x1, y1);
            brush.EndPoint   = new System.Windows.Point(x2, y2);

            foreach (SVGElement item in this.Children)
            {
                Stop stop = item as Stop;
                if (stop == null)
                {
                    continue;
                }

                double   offset  = stop.GetAttributeValue <SVGDouble>("offset").GetValue(1);
                double   opacity = stop.GetAttributeValue <SVGDouble>("stop-opacity", false, SVGDouble.One).GetValue(1);
                SVGColor color   = stop.GetAttributeValue <SVGColor>("stop-color");

                GradientStop gs = new GradientStop();
                gs.Offset = offset;

                if (color != null)
                {
                    Color c = color.Value;
                    c.A = (byte)(opacity * 255);

                    gs.Color = c;
                }

                brush.GradientStops.Add(gs);
            }

            if (transform != null && transform.Value != null && transform.Value.Count > 0)
            {
                TransformGroup group = new TransformGroup();
                foreach (Transform item in transform.Value)
                {
                    group.Children.Add(item);
                }

                brush.Transform = group;
            }

            if (gradientUnits != null)
            {
                brush.MappingMode = gradientUnits.Value;
            }

            if (spreadMethod != null)
            {
                brush.SpreadMethod = spreadMethod.Value;
            }

            return(brush);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取画刷
        /// </summary>
        /// <returns>画刷</returns>
        private Brush GetBrush()
        {
            double           cx            = this.GetAttributeValue <SVGDouble>("cx", false, new SVGDouble(0.5)).GetValue(1);
            double           cy            = this.GetAttributeValue <SVGDouble>("cy", false, new SVGDouble(0.5)).GetValue(1);
            double           fx            = this.GetAttributeValue <SVGDouble>("fx", false, new SVGDouble(0.5)).GetValue(1);
            double           fy            = this.GetAttributeValue <SVGDouble>("fy", false, new SVGDouble(0.5)).GetValue(1);
            double           r             = this.GetAttributeValue <SVGDouble>("r", false, new SVGDouble(0.5)).GetValue(1);
            SVGGradientUnits gradientUnits = this.GetAttributeValue <SVGGradientUnits>("gradientUnits");
            SVGTransform     transform     = this.GetAttributeValue <SVGTransform>("gradientTransform");
            SVGSpreadMethod  spreadMethod  = this.GetAttributeValue <SVGSpreadMethod>("spreadMethod");

            RadialGradientBrush brush = new RadialGradientBrush();

            brush.Center         = new System.Windows.Point(cx, cy);
            brush.RadiusX        = r;
            brush.RadiusY        = r;
            brush.GradientOrigin = new System.Windows.Point(fx, fy);

            foreach (SVGElement item in this.Children)
            {
                Stop stop = item as Stop;
                if (stop == null)
                {
                    continue;
                }

                double   offset  = stop.GetAttributeValue <SVGDouble>("offset").GetValue(1);
                double   opacity = stop.GetAttributeValue <SVGDouble>("stop-opacity", false, SVGDouble.One).GetValue(1);
                SVGColor color   = stop.GetAttributeValue <SVGColor>("stop-color");

                GradientStop gs = new GradientStop();
                gs.Offset = offset;

                if (color != null)
                {
                    Color c = color.Value;
                    c.A = (byte)(opacity * 255);

                    gs.Color = c;
                }

                brush.GradientStops.Add(gs);
            }

            if (transform != null && transform.Value != null && transform.Value.Count > 0)
            {
                TransformGroup group = new TransformGroup();
                foreach (Transform item in transform.Value)
                {
                    group.Children.Add(item);
                }

                brush.Transform = group;
            }

            if (gradientUnits != null)
            {
                brush.MappingMode = gradientUnits.Value;
            }

            if (spreadMethod != null)
            {
                brush.SpreadMethod = spreadMethod.Value;
            }

            return(brush);
        }