C# 使用GDI画坐标图(支持负值)

因为项目需要,使用GDI画一个全坐标系统图,并嵌入PDF文件,方便打印。

效果图如下:

 

一般的坐标图仅有右上的四分之一,此图则是全坐标的,输入是四个点的坐标,可以是负值。

代码:

代码
Bitmap bitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format24bppRgb);
            Graphics g
=Graphics.FromImage(bitmap);
            
//Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Font font 
= new Font(Font.Name, 11);
            SolidBrush brush 
= new SolidBrush(Color.Black);
            Pen pen 
= new Pen(Color.Black);
            pen.EndCap 
= LineCap.ArrowAnchor;
            pen.DashStyle 
= DashStyle.Solid;
            
//坐标轴
            Point pCenter = new Point(300260);
            g.DrawLine(pen, 
new Point(pCenter.X - 200, pCenter.Y), new Point(pCenter.X + 200, pCenter.Y));//x
            g.DrawLine(pen, new Point(pCenter.X, pCenter.Y + 200), new Point(pCenter.X, pCenter.Y - 200));//y            
            
//轴标格
            for (int i = 0; i < 5; i++)
            {
                g.DrawLine(Pens.Black, 
new Point(pCenter.X - iX * i, pCenter.Y), new Point(pCenter.X - iX * i, pCenter.Y - 4));//x
                g.DrawString((-i).ToString(), font, brush, new PointF(pCenter.X - iX * i, pCenter.Y));
                g.DrawLine(Pens.Black, 
new Point(pCenter.X + iX * i, pCenter.Y), new Point(pCenter.X + iX * i, pCenter.Y - 4));//x
                g.DrawString(i.ToString(), font, brush, new PointF(pCenter.X + iX * i, pCenter.Y));
                g.DrawLine(Pens.Black, 
new Point(pCenter.X, pCenter.Y - iX * i), new Point(pCenter.X + 4, pCenter.Y - iX * i));//y
                g.DrawString(i.ToString(), font, brush, new PointF(pCenter.X, pCenter.Y - iX * i));
                g.DrawLine(Pens.Black, 
new Point(pCenter.X, pCenter.Y + iX * i), new Point(pCenter.X + 4, pCenter.Y + iX * i));//y
                g.DrawString((-i).ToString(), font, brush, new PointF(pCenter.X, pCenter.Y + iX * i));
            }
            
            StringFormat sf 
= new StringFormat();
            sf.Alignment 
= StringAlignment.Far;
            g.DrawString(
"x", font, brush, new PointF(pCenter.X + 200, pCenter.Y));
            g.DrawString(
"y", font, brush, new PointF(pCenter.X, pCenter.Y - 200));
            g.DrawString(
"0", font, brush, new PointF(pCenter.X, pCenter.Y));
            
//定义比例尺
            int BX = 4;
            
int BY = 4;
            Point new1 
= getNewPoint(new Point(200300), pCenter, BX, BY);
            Point new2 
= getNewPoint(new Point(-300400), pCenter, BX, BY);
            Point new3 
= getNewPoint(new Point(-400-500), pCenter, BX, BY);
            Point new4 
= getNewPoint(new Point(500-300), pCenter, BX, BY);
            
//g.DrawLine(Pens.Black, pCenter, new1);
            g.DrawArc(Pens.Black, new1.X, new1.Y, 1145.0F360.0F);
            g.DrawString(
"p1", font, brush, new PointF(new1.X, new1.Y));
            g.DrawArc(Pens.Black, new2.X, new2.Y, 
1145.0F360.0F);
            g.DrawString(
"p2", font, brush, new PointF(new2.X, new2.Y));
            g.DrawArc(Pens.Black, new3.X, new3.Y, 
1145.0F360.0F);
            g.DrawString(
"p3", font, brush, new PointF(new3.X, new3.Y));
            g.DrawArc(Pens.Black, new4.X, new4.Y, 
1145.0F360.0F);
            g.DrawString(
"p4", font, brush, new PointF(new4.X, new4.Y));
            g.DrawLine(Pens.Black, new1, new2);
            g.DrawLine(Pens.Black, new2, new3);
            g.DrawLine(Pens.Black, new3, new4);
            g.DrawLine(Pens.Black, new4, new1);
            bitmap.Save(
"c:\\aaa.bmp");
            g.Dispose();

最后输出为图片文件.

还有一个处理坐标的函数:

代码
Point getNewPoint(Point p, Point pZero, int bx, int by)
        {
            Point myp 
= new Point();
            myp.X 
= pZero.X + p.X / bx;
            
if (p.Y > 0)
                myp.Y 
= pZero.Y - Math.Abs(p.Y / by);
            
else
                myp.Y 
= pZero.Y + Math.Abs(p.Y / by);
            
return myp;
        }


 

posted on 2010-12-06 14:59  咖啡色  阅读(3119)  评论(0编辑  收藏  举报