美文网首页通往成功之路
vs2010 c# graphics绘制坐标系

vs2010 c# graphics绘制坐标系

作者: 此十八 | 来源:发表于2018-03-02 16:15 被阅读3次

完整工程已经传到我的资源,需要可以去下载! 

在txt文件里写入坐标,绘制图形,例如x1,y1,x2,y2,x3,y3。 

鼠标位于坐标系内,显示2条红色十字线,和坐标值,离开坐标系,2者消失。 

源码:

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.IO;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;namespace TEST{publicpartialclassForm1 : Form    {publicForm1()        {            InitializeComponent();        }stringresultFile;privatePoint mouseOffset;boolflag=false;        ToolTip tooltip =newToolTip();int[] bufx=newint[1024];int[] bufy =newint[1024];intdrawflag=0;intreadlen =0;// x1,y1为鼠标移动的坐标,画十字线用,xbuf,ybuf绘制点坐标数组voiddraw(intx1,inty1,int[] xbuf,int[] ybuf)        {//定义画布大小intheight =350, width =800;//创建位图System.Drawing.Bitmap image =newSystem.Drawing.Bitmap(width, height);//创建Graphics类对象Graphics g = Graphics.FromImage(image);//清空图片背景色g.Clear(Color.White);            Font font =newSystem.Drawing.Font("Arial",9, FontStyle.Regular);            Font font1 =newSystem.Drawing.Font("宋体",20, FontStyle.Regular);            Font font2 =newSystem.Drawing.Font("Arial",8, FontStyle.Regular);            System.Drawing.Drawing2D.LinearGradientBrush brush =newSystem.Drawing.Drawing2D.LinearGradientBrush(newRectangle(0,0, image.Width, image.Height), Color.Blue, Color.Blue,1.2f,true);            g.FillRectangle(Brushes.SeaGreen,0,0, width, height);            Brush brush1 =newSolidBrush(Color.Blue);            Brush brush2 =newSolidBrush(Color.SaddleBrown);//画图片的边框线g.DrawRectangle(newPen(Color.Black),40,40,720,270);            g.DrawRectangle(newPen(Color.Black),40,35,720,275);            System.Drawing.Pen mypen =newPen(brush,1);            System.Drawing.Pen mypen2 =newPen(Color.Red,2);            System.Drawing.Pen mypen3 =newPen(Color.Yellow,1);//绘制线条//绘制纵向线条intx =80;for(inti =0; i <17; i++)            {                g.DrawLine(mypen, x,40, x,310);                x = x +40;            }//Pen mypen1 = new Pen(Color.Blue, 2);//g.DrawLine(mypen1, x - 480, 80, x - 480, 340);//绘制横向线条inty =70;for(inti =0; i <8; i++)            {                g.DrawLine(mypen,40, y,760, y);                y = y +30;            }//x轴上对应的标记String[] n = {" 10"," 20"," 30"," 40"," 50"," 60"," 70"," 80"," 90"," 100","110","120","130","140","150","160","170"};            x =70;for(inti =0; i <17; i++)            {                g.DrawString(n[i].ToString(), font, Brushes.Red, x,315);//设置文字内容及输出位置x = x +40;            }//y轴上对应的标记String[] m = {"40","35","30","25","20","15","10"," 5"};            y =60;for(inti =0; i <8; i++)            {                g.DrawString(m[i].ToString(), font, Brushes.Red,16, y);//设置文字内容及输出位置y = y +30;            }if(drawflag ==1)            {for(inti =0; i 40&& ex <760) && (ey >40&& ey <310))            {                flag =true;                label1.BackColor = Color.FromArgb(80, Color.White);                label1.Visible =true;                label1.Text = ("坐标值:"+"\n"+"x="+ mouseOffset.X +"\n"+"y="+ mouseOffset.Y);                label1.Location =newPoint(ex+20, ey+10);// this.Cursor = Cursors.Cross;  //tooltip.Show("x=" + e.X + ";y=" + e.Y, this, new Point(MousePosition.X, MousePosition.Y));}else{                flag =false;                label1.Visible =false;// this.Cursor = Cursors.Arrow;}            draw(ex, ey, bufx, bufy);        }privatevoidbuttonFile_Click(objectsender, EventArgs e)        {            OpenFileDialog OpenFile =newOpenFileDialog();// OpenFile.InitialDirectory = "E:\\1work\\FILE";//初始目录OpenFile.Filter ="All files(*.*)|*.*|txt files(*.txt)|*.txt";//文本筛选OpenFile.FilterIndex =1;//文本筛选器索引,选择第一项就是1OpenFile.RestoreDirectory =true;if(OpenFile.ShowDialog() == DialogResult.OK)                resultFile = OpenFile.FileName;            textBoxFile.Text = resultFile;        }privatevoidbuttonDraw_Click(objectsender, EventArgs e)        {if(resultFile ==null)            {                MessageBox.Show("请选取要绘图的文件!","错误",                          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);return;            }else{                StreamReader fileRead =newStreamReader(File.Open(textBoxFile.Text, FileMode.Open), System.Text.Encoding.Default);stringtxtLine = fileRead.ReadLine();                readlen =0;while(true)                {                    txtLine = fileRead.ReadLine();if(txtLine ==null)break;if(txtLine.Length >=3)                        {                            String[] date = txtLine.Split(',');intlen = date.Length;//label2.Text =Convert.ToString(date[11]);intj =0;for(inti =0; i < len /2; i++)                            {//x:70 ,4*  x=70+4*d;//y:310,*6  y=310-6*dbufx[readlen/2+ i] = Convert.ToInt32(date[j]) *4+40;                                bufy[readlen/2+ i] =310- Convert.ToInt32(date[j +1]) *6;                                j +=2;                            }                            readlen += len;                      }                }                fileRead.Close();                drawflag =1;                draw(1,1, bufx, bufy);                label2.Text = Convert.ToString("输出点个数:"+readlen/2);            }        }privatevoidbuttonClear_Click(objectsender, EventArgs e)        {            drawflag =0;            draw(1,1, bufx, bufy);            label2.Text = Convert.ToString("输出点个数:");        }    }}

相关文章

网友评论

    本文标题:vs2010 c# graphics绘制坐标系

    本文链接:https://www.haomeiwen.com/subject/hpbaxftx.html