how to draw polyline by SimpleDisplayClass?

279
2
12-28-2010 07:32 PM
tanyg
by
New Contributor
hi all:
    i want to draw some polyline in Bitmap using SimpleDisplay,but the bitmap is blank,what's wrong ,
thx!
the code is:


        static void Main(string[] args)
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine },
            new esriLicenseExtensionCode[] { });
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()


            System.Drawing.Point p1 = new System.Drawing.Point(20, 20);
            System.Drawing.Point p2 = new System.Drawing.Point(80, 80);

            IPolyline pc = new PolylineClass();
            pc.FromPoint = ConvertToIPoint(p1);
            pc.ToPoint = ConvertToIPoint(p2);

            IDisplay display = new SimpleDisplayClass();
            IDisplayTransformation tran = display.DisplayTransformation;

            Rectangle r = new Rectangle(0,0,100,100);

            IEnvelope env = (pc as IGeometry).Envelope;
            tran.Bounds = env;
            tran.VisibleBounds = env;

            tagRECT rect = new tagRECT();
            rect.left = 0;
            rect.right = 100;
            rect.top = 0;
            rect.bottom = 100;

            tran.set_DeviceFrame(ref rect);

            Bitmap bmp = new Bitmap(100, 100);

            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);

            display.StartDrawing(g.GetHdc().ToInt32(), (short)esriScreenCache.esriNoScreenCache);


            ISimpleLineSymbol s = new SimpleLineSymbol();
            s.Style = esriSimpleLineStyle.esriSLSSolid;
            s.Width = 5;

            IRgbColor c = new RgbColorClass();
            c.Red = 255;
            c.Blue = 0;
            c.Green = 0;

            s.Color = c;

            display.SetSymbol(s as ISymbol);
            display.DrawPolyline(pc as IPolyline);

            display.FinishDrawing();

            bmp.Save(@"c:\t.png",ImageFormat.Png);
            g.ReleaseHdc();

            m_AOLicenseInitializer.ShutdownApplication();
        }

        public static IPoint ConvertToIPoint(System.Drawing.Point pt)
        {
            IPoint p = new PointClass();

            IZAware z = p as IZAware;
            z.ZAware = true;

            IMAware m = p as IMAware;
            m.MAware = true;

            p.X = pt.X;
            p.Y = pt.Y;
            p.Z = 0;
            p.M = 0;
            p.SpatialReference = GetSpatialReference();


            return p;
        }

        public static IEnvelope ConvertToEnvelope(Rectangle bound)
        {
            IEnvelope env = new EnvelopeClass();

            IZAware z = env as IZAware;
            z.ZAware = true;

            IMAware m = env as IMAware;
            m.MAware = true;

            env.XMin = bound.Left;
            env.YMin = bound.Top;
            env.Width = bound.Width;
            env.Height = bound.Height;
            env.ZMin = 0;
            env.ZMax = 0;
            env.MMax = 0;
            env.MMin = 0;
            env.SpatialReference = GetSpatialReference();

            return env;
        }
        public static ISpatialReference GetSpatialReference()
        {
            ISpatialReference spatial_ref = null;

            ISpatialReferenceFactory env = new SpatialReferenceEnvironmentClass();

            //Beijing_1954_3_Degree_GK_CM_108E.prj(PCS)
            string prj_string =
                "PROJCS[\"Beijing_1954_3_Degree_GK_CM_108E\",GEOGCS[\"GCS_Beijing_1954\",DATUM[\"D_Beijing_1954\",SPHEROID[\"Krasovsky_1940\",6378245.0,298.3]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",108.0],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";

            int read_bytes;
            env.CreateESRISpatialReference(prj_string, out spatial_ref, out read_bytes);

            double len = 5000 * 1000;
            spatial_ref.SetFalseOriginAndUnits(-len, -len, 1000);
            spatial_ref.SetZFalseOriginAndUnits(-len, 1000);
            spatial_ref.SetMFalseOriginAndUnits(-len, 1000);

            return spatial_ref;
        }
0 Kudos
2 Replies
tanyg
by
New Contributor
Anyone know why?
0 Kudos
tanyg
by
New Contributor
i find my mistake:
......

            display.FinishDrawing();
            g.ReleaseHdc();
            bmp.Save(@"c:\t.png", ImageFormat.Png);
0 Kudos