Example #1
0
        private IList getImages(bool update)
        {
            IList retlist = new ArrayList();
            int   nWidth  = mapview.PicWidth;
            Dictionary <string, ClassImage> table = new Dictionary <string, ClassImage>();

            foreach (IGraph graph in documentControl1.SVGDocument.SelectCollection)
            {
                if (graph != null)
                {
                    if (graph is ItopVector.Core.Figure.Line)
                    {
                        Line     ll  = (graph as Line);
                        PointF[] pts = (PointF[])ll.Points.Clone();
                        graph.Transform.Matrix.TransformPoints(pts);
                        documentControl1.DrawArea.PointToSystem(pts);
                        //PointF pf2 = ll.CenterPoint;
                        //pf2 = documentControl1.DrawArea.PointToSystem(pf2);
                        PointF pf3 = ll.CenterPoint;// new PointF((pts[0].X + pts[1].X) / 2, (pts[1].Y + pts[1].Y) / 2);

                        //this.documentControl1.SetToolTip(LineAndRect(pts[0],pts[1],new RectangleF(100,100,100,100)).ToString());
                        int width  = Convert.ToInt32(Math.Abs(pts[0].X - pts[1].X));
                        int height = Convert.ToInt32(Math.Abs(pts[0].Y - pts[1].Y));
                        IList <ClassImage> list = mapview.GetMapList(width, height, new Point((int)pf3.X, (int)pf3.Y));
                        StringBuilder      sb   = new StringBuilder();

                        Point off1 = new Point((int)Math.Min(pts[0].X, pts[1].X), (int)Math.Min(pts[0].Y, pts[1].Y));
                        pts[0].X -= off1.X;
                        pts[0].Y -= off1.Y;
                        pts[1].X -= off1.X;
                        pts[1].Y -= off1.Y;
                        //GraphicsPath gp = graph.GPath.Clone() as GraphicsPath;
                        //using (Matrix matrix1 = graph.Transform.Matrix.Clone()) {
                        //    matrix1.Multiply(graph.GraphTransform.Matrix);
                        //    matrix1.Translate(-off1.X, -off1.Y);

                        //    gp.Transform(matrix1);
                        //}
                        Graphics g = documentControl1.CreateGraphics();
                        foreach (ClassImage mc in list)
                        {
                            Rectangle rt = mc.Bounds;
                            //rt.Location = documentControl1.DrawArea.PointToView(rt.Location);
                            //rt.Location.Offset(off1);
                            string pic = mc.PicUrl;
                            if (LineAndRect(pts[0], pts[1], rt))
                            {
                                sb.AppendLine(string.Format("{0}_{1}_{2}", mc.PicUrl, rt.Left, rt.Top));
                                if (table.ContainsKey(pic))
                                {
                                    mc.PicImage = table[pic].PicImage;
                                    table[pic]  = mc;
                                }
                                else
                                {
                                    table.Add(pic, mc);
                                }
                                System.Drawing.Image image = new Bitmap(nWidth, nWidth);
                                Graphics             g1    = Graphics.FromImage(image);
                                g1.Clear(Color.White);
                                if (mc.PicImage == null)
                                {
                                }
                                else
                                {
                                    //g = Graphics.FromImage(mc.PicImage);
                                    g1.DrawImage(mc.PicImage, 0, 0, nWidth, nWidth);
                                }
                                Matrix matrix2 = graph.GraphTransform.Matrix;
                                PointF pf22    = off1;
                                pf22.X += mc.Left;
                                pf22.Y += mc.Top;

                                pf22 = documentControl1.DrawArea.PointToView(pf22);
                                matrix2.Translate(-matrix2.OffsetX, -matrix2.OffsetY, MatrixOrder.Append);
                                //Matrix matrix2
                                matrix2.Translate(-pf22.X * documentControl1.ScaleRatio, -pf22.Y * documentControl1.ScaleRatio, MatrixOrder.Append);
                                //matrix2.Translate(
                                //g1.TranslateTransform(pf22.X, pf22.Y);
                                //graph.GraphTransform.Matrix=
                                graph.Draw(g1, 0);
                                mc.PicImage = image;

                                g1.Dispose();

                                image.Save(Application.StartupPath + "\\png\\" + mc.PicUrl.Replace('/', '~'), ImageFormat.Png);
                            }
                        }
                        //this.documentControl1.SetToolTip(sb.ToString());
                    }
                    else
                    if (!(graph is SVG))
                    {
                        RectangleF   rf    = graph.GetBounds();
                        GraphicsPath path2 = new GraphicsPath();
                        path2.AddRectangle(rf);


                        if (!(graph is Use))
                        {
                            rf = graph.GPath.GetBounds(graph.Transform.Matrix);
                        }
                        //graph.Transform.Matrix.TransformPoints(pts);

                        PointF[] pts = new PointF[2] {
                            rf.Location, new PointF(rf.Right, rf.Bottom)
                        };

                        PointF[] pts2 = (PointF[])pts.Clone();
                        documentControl1.DrawArea.PointToSystem(pts);
                        PointF pf3 = graph.CenterPoint;

                        int width  = Convert.ToInt32(Math.Abs(pts[0].X - pts[1].X));
                        int height = Convert.ToInt32(Math.Abs(pts[0].Y - pts[1].Y));
                        IList <ClassImage> list = mapview.GetMapList(width, height, new Point((int)pf3.X, (int)pf3.Y));
                        StringBuilder      sb   = new StringBuilder();
                        PointF             off1 = pts[0];

                        GraphicsPath gp = graph.GPath.Clone() as GraphicsPath;
                        using (Matrix matrix1 = graph.Transform.Matrix.Clone()) {
                            matrix1.Multiply(documentControl1.DrawArea.CoordTransform, MatrixOrder.Append);
                            matrix1.Translate(-off1.X, -off1.Y, MatrixOrder.Append);
                            gp.Transform(matrix1);
                        }

                        Graphics g  = documentControl1.CreateGraphics();
                        Graphics g1 = null;
                        foreach (ClassImage mc in list)
                        {
                            string     pic = mc.PicUrl;
                            RectangleF rt  = mc.Bounds;
                            Region     r   = new Region(rt);
                            rt = r.GetBounds(g);
                            r.Intersect(gp);
                            if (!r.IsEmpty(g))
                            {
                                if (table.ContainsKey(pic))
                                {
                                    mc.PicImage = table[pic].PicImage;
                                    table[pic]  = mc;
                                }
                                else
                                {
                                    table.Add(pic, mc);
                                }
                                sb.AppendLine(string.Format("{0}_{1}_{2}", mc.PicUrl, rt.Left, rt.Top));
                                System.Drawing.Image image = new Bitmap(nWidth, nWidth);

                                g1 = Graphics.FromImage(image);
                                g1.Clear(Color.White);
                                if (mc.PicImage == null)
                                {
                                }
                                else
                                {
                                    //g = Graphics.FromImage(mc.PicImage);
                                    g1.DrawImage(mc.PicImage, 0, 0, nWidth, nWidth);
                                }
                                Matrix matrix2 = new Matrix();
                                matrix2.Multiply(documentControl1.DrawArea.CoordTransform);
                                PointF pf22 = off1;
                                if (graph is Text || graph is Use)      //文字特殊处理
                                {
                                    matrix2 = graph.GraphTransform.Matrix;
                                    if (graph.LimitSize)      //文字固定大小
                                    {
                                        float f1 = graph.Transform.Matrix.Elements[0] - 1;
                                        matrix2 = graph.Transform.Matrix.Clone();
                                        matrix2.Invert();
                                        graph.LimitSize = false;
                                        if (graph is Text)
                                        {
                                            matrix2.Translate(
                                                -mc.Left - pts2[0].X - rf.Width * f1 / 2 + graph.Transform.Matrix.OffsetX,
                                                -mc.Top - pts2[0].Y - rf.Height * f1 / 2 + graph.Transform.Matrix.OffsetY, MatrixOrder.Append);
                                        }
                                        else
                                        {
                                            matrix2.Translate(
                                                -mc.Left - pts2[0].X - rf.Width / 2 + graph.Transform.Matrix.OffsetX,
                                                -mc.Top - pts2[0].Y - rf.Width / 2 + graph.Transform.Matrix.OffsetY, MatrixOrder.Append);
                                        }
                                        graph.LimitSize             = true;
                                        graph.GraphTransform.Matrix = matrix2;
                                    }
                                    else
                                    {
                                        pf22.X += mc.Left;
                                        pf22.Y += mc.Top;
                                        pf22    = documentControl1.DrawArea.PointToView(pf22);
                                        matrix2.Translate(-matrix2.OffsetX, -matrix2.OffsetY, MatrixOrder.Append);
                                        matrix2.Translate(-pf22.X * documentControl1.ScaleRatio, -pf22.Y * documentControl1.ScaleRatio, MatrixOrder.Append);
                                    }
                                }
                                else
                                {
                                    pf22.X += mc.Left;
                                    pf22.Y += mc.Top;
                                    pf22    = documentControl1.DrawArea.PointToView(pf22);
                                    matrix2.Translate(-matrix2.OffsetX, -matrix2.OffsetY, MatrixOrder.Append);
                                    matrix2.Translate(-pf22.X * documentControl1.ScaleRatio, -pf22.Y * documentControl1.ScaleRatio, MatrixOrder.Append);
                                }
                                graph.GraphTransform.Matrix = matrix2;
                                graph.Draw(g1, 0);
                                mc.PicImage = image;
                                g1.Dispose();
                                if (update)
                                {
                                    mapview.SetImage(mc);
                                }
                                else
                                {
                                    image.Save(Application.StartupPath + "\\png\\" + mc.PicUrl.Replace('/', '~'), ImageFormat.Png);
                                }
                            }
                        }
                        g.Dispose();
                        GC.Collect();
                        //this.documentControl1.SetToolTip(sb.ToString());
                    }
                }
            }
            foreach (string key in table.Keys)
            {
                retlist.Add(table[key]);
            }

            return(retlist);
        }