您的位置:首页 > 编程语言 > C#

C#画图

2013-09-28 16:25 411 查看
System.Drawing.Image MyImage = new System.Drawing.Bitmap(500, 500);
            //申请位图对象
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(MyImage); //申请画图对象
            //用C清除出
            Color C = Color.FromArgb(0, 255, 255);
            g.Clear(C);
            //定义钢笔
            Pen myPen = new Pen(Color.Blue, 6);
            //设置钢笔的开始端结束端是否有逗点和箭头
            // Set the StartCap property.
            myPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
            // Set the EndCap property.
            myPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            //画线
            g.DrawLine(myPen, 0, 0, 200, 200);
            //通过两点画线
            Point myStartPoint = new Point(4, 2);
            Point myEndPoint = new Point(120, 60);
            g.DrawLine(myPen, myStartPoint, myEndPoint);
            //设置钢笔画出的线为虚线
            myPen.DashStyle = DashStyle.Dash;
            g.DrawLine(myPen, 100, 50, 300, 80);
            //画矩形1
            g.DrawRectangle(myPen, 100, 50, 80, 40);
            //画矩形2
            Rectangle myRectangle = new Rectangle(200,150, 80, 40);
            g.DrawRectangle(myPen, myRectangle);

            //绘制贝塞尔样条

            g.DrawBezier(myPen, 200, 200, 150, 1500, 80, 150, 100, 100);

            //基数样条
            // Create points that define curve.
            Point point1 = new Point(50, 50);
            Point point2 = new Point(100, 25);
            Point point3 = new Point(200, 5);
            Point point4 = new Point(250, 50);
            Point point5 = new Point(300, 100);
            Point point6 = new Point(350, 200);
            Point point7 = new Point(250, 250);
            Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 };

            g.DrawCurve(myPen, curvePoints, 1.5f);

             //pictureBox1在(0,0)到(h,w)范围画点
            g.DrawImage(MyImage, 0, 0, 20, 20);
            pictureBox1.Image = MyImage;     //这个图片贴到pictureBox1控件上


System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔
            System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷

            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            formGraphics.FillEllipse(myBrush, new Rectangle(0, 0, 100, 200));//画实心椭圆
            formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 100, 200));//空心圆
            formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 100, 200));//画实心方
            formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 100, 200));//空心矩形
            formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
            formGraphics.DrawPie(myPen, 90, 80, 140, 40, 120, 100); //画馅饼图形 
           //画多边形 
           formGraphics.DrawPolygon(myPen, new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point

(200,170), new Point(70,350), new Point(50,200)});
            //清理使用的资源
            myPen.Dispose();
            myBrush.Dispose();
            formGraphics.Dispose();


//在窗体上绘制竖向文本 
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本

//清理使用的资源
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();


System.Drawing.Graphics g = this.CreateGraphics();
HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs, Color.Blue, Color.Red);
g.FillRectangle(sb, 50, 50, 150, 150);

Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
  //PathGradientBrush路径渐变,LinearGradientBrush线性渐变
Brush brush;
Image bgimage = new Bitmap(@"D:\PIC\1.bmp");
brush = new TextureBrush(bgimage); //用一幅图作椭圆的背景 
g.FillEllipse(brush, 50, 50, 500, 300);
            //TextureBrush  类的每个属性都是 Brush 对象,这种对象使用图像来填充形状的内部。  此类不能被继承。

效果如图:



//颜色对话框
ColorDialog c = new ColorDialog(); 
c.ShowDialog(); 
textBox1.BackColor = c.Color; 
//字体对话框
FontDialog f = new FontDialog(); 
f.ShowDialog(); 
textBox1.Font   = f.Font;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: