Пример #1
0
 private Matrix ParseToMatrix(string tranform)
 {
     if (!string.IsNullOrEmpty(tranform))
     {
         if (tranform.Contains("translate"))
         {
             int      begin = tranform.IndexOf("(") + 1, end = tranform.IndexOf(")");
             string[] values = tranform.Substring(begin, end - begin).Split(',');
             return(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, PaintObject.ParseToFloat(values[0]), PaintObject.ParseToFloat(values[1])));
         }
         if (tranform.Contains("matrix"))
         {
             int      begin = tranform.IndexOf("(") + 1, end = tranform.IndexOf(")");
             string[] values = tranform.Substring(begin, end - begin).Split(',');
             return(new System.Drawing.Drawing2D.Matrix(
                        PaintObject.ParseToFloat(values[0]),
                        PaintObject.ParseToFloat(values[1]),
                        PaintObject.ParseToFloat(values[2]),
                        PaintObject.ParseToFloat(values[3]),
                        PaintObject.ParseToFloat(values[4]),
                        PaintObject.ParseToFloat(values[5])));
         }
     }
     return(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0));
 }
Пример #2
0
        public Bitmap CreateBitmapFromSvg(float scale = 1F)
        {
            Size     bitmapSize = new SizeF((float)MapSize.Width / scale, (float)MapSize.Height / scale).ToSize();
            Bitmap   bitmap     = new Bitmap(bitmapSize.Width, bitmapSize.Height);
            Graphics g          = Graphics.FromImage(bitmap);

            g.Clear(Color.White);

            foreach (var obj in PaintObjectList)
            {
                Matrix matrix = obj.GetTransformMatrix();
                matrix.Scale(1.0F / scale, 1.0F / scale, MatrixOrder.Append);
                g.Transform = matrix;

                switch (obj.Name)
                {
                case "path":
                {
                    string data;
                    if (!obj.TryGetAttributeValue("data", out data))
                    {
                        continue;
                    }
                    string[]       val          = data.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    PointF         currentPoint = new PointF(0, 0);
                    SVSObjectStyle style        = obj.GetStyle();
                    GraphicsPath   path         = new GraphicsPath();

                    for (int i = 0; i < val.Length; i++)
                    {
                        if (val[i] == "m")
                        {
                            i++;
                            var p = StringToPoint(val[i]);
                            currentPoint = PointF.Add(currentPoint, new SizeF(p.X, p.Y));
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                nextPoint.X += currentPoint.X;
                                nextPoint.Y += currentPoint.Y;
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                            continue;
                        }
                        if (val[i] == "M")
                        {
                            i++;
                            currentPoint = StringToPoint(val[i]);
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                            continue;
                        }
                        if (val[i] == "l")
                        {
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                nextPoint.X += currentPoint.X;
                                nextPoint.Y += currentPoint.Y;
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                        }
                        if (val[i] == "L")
                        {
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                        }
                        if (val[i] == "c")
                        {
                            while (i < val.Length - 1)
                            {
                                PointF bez1   = new PointF(currentPoint.X + StringToPoint(val[i + 1]).X, currentPoint.Y + StringToPoint(val[i + 1]).Y);
                                PointF bez2   = new PointF(currentPoint.X + StringToPoint(val[i + 2]).X, currentPoint.Y + StringToPoint(val[i + 2]).Y);
                                PointF finish = new PointF(currentPoint.X + StringToPoint(val[i + 3]).X, currentPoint.Y + StringToPoint(val[i + 3]).Y);
                                path.AddBezier(currentPoint, bez1, bez2, finish);
                                currentPoint = finish;
                                i           += 3;
                                if (i < val.Length - 1)
                                {
                                    if (val[i + 1].Length == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        if (val[i] == "C")
                        {
                            while (i < val.Length - 1)
                            {
                                PointF bez1   = new PointF(StringToPoint(val[i + 1]).X, StringToPoint(val[i + 1]).Y);
                                PointF bez2   = new PointF(StringToPoint(val[i + 2]).X, StringToPoint(val[i + 2]).Y);
                                PointF finish = new PointF(StringToPoint(val[i + 3]).X, StringToPoint(val[i + 3]).Y);
                                path.AddBezier(currentPoint, bez1, bez2, finish);
                                currentPoint = finish;
                                i           += 3;
                                if (i < val.Length - 1)
                                {
                                    if (val[i + 1].Length == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        if (val[i].ToLower() == "a")
                        {
                            i++;
                            PointF point = StringToPoint(val[i]);
                            float  w     = point.X * 2;
                            float  h     = point.Y * 2;
                            float  x     = currentPoint.X - w;
                            float  y     = currentPoint.Y - h / 2;
                            path.AddEllipse(x, y, w, h);
                        }
                        if (val[i].ToLower() == "z")
                        {
                            if (val[i - 1].Length == 1 && val[1].Length == 1)
                            {
                                break;
                            }
                            path.CloseFigure();
                            break;
                        }
                    }

                    g.FillPath(style.GetBrush(), path);
                    g.DrawPath(style.GetPen(scale), path);

                    break;
                }

                case "rect":
                {
                    float          x     = PaintObject.ParseToFloat(obj.GetAttributeValue("x"));
                    float          y     = PaintObject.ParseToFloat(obj.GetAttributeValue("y"));
                    float          w     = PaintObject.ParseToFloat(obj.GetAttributeValue("width"));
                    float          h     = PaintObject.ParseToFloat(obj.GetAttributeValue("height"));
                    SVSObjectStyle style = obj.GetStyle();

                    g.FillRectangle(style.GetBrush(), x, y, w, h);
                    g.DrawRectangle(style.GetPen(scale), x, y, w, h);

                    break;
                }
                }
                g.ResetTransform();
            }
            return(bitmap);
        }