C#基础绘图

/*
 * Created by SharpDevelop.
 * User: Jason_PC
 * Date: 2018/5/6
 * Time: 20:48
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


namespace Windows
{
     /// <summary>
     /// Description of MainForm.
     /// </summary>
     public partial class MainForm : Form
    {
         //Graphics g;
         public MainForm()
        {
             //
             // The InitializeComponent() call is required for Windows Forms designer support.
             //
             InitializeComponent();
             //
             // TODO: Add constructor code after the InitializeComponent() call.
             //
        }
        
         void Form1_Paint( PaintEventArgs e)
        {            
             //g= e.Graphics;
             //清除整个绘图面并以指定背景色填充
             //g.Clear(Color.White);
             //随便绘制一条直线出来看看
             //g.DrawLine(SystemPens.ControlText, 10, 10, 100, 100);
        }
        
         void MainFormLoad( object sender, EventArgs e)
        {
    
        }
        
         void BtnDrawLine_Click( object sender, EventArgs e)
        { 
             Graphics g = this. CreateGraphics();
             Color red = Color. FromArgb( 255, 0, 0);
             Pen blackPen = new Pen(red, 3);
             Point point1 = new Point( 100, 100); //坐标(100,100)
             Point point2 = new Point( 500, 100); //坐标(500,100)
            g. DrawLine(blackPen, point1, point2); //两个坐标连成直线
    
        }
         void BtnInvalidate_Click( object sender, EventArgs e)
        {
    
             Rectangle r = new Rectangle( 10, 10, 200, 400); //从坐标(10,10)开始,定义一个宽度200,高度200的矩形区域
             this. Invalidate(r);
        }
         void BtnDrawPie_Click( object sender, EventArgs e)
        { 
             Graphics g = this. CreateGraphics();
             Rectangle r = new Rectangle( 10, 10, 200, 100);
             Pen pen1 = new Pen( Color.Black);
            g. DrawRectangle(pen1, 50, 50, 200, 200);   //起始坐标(50,50),宽度200,高度200
             //椭圆
            g. DrawPie(pen1, 50, 50, 200, 300, 225, 10);   //起始坐标(50,50),宽度200,高度200,射线1=225,射线2=90 
            //g.DrawPie(pen1, r, 225, 90);
            
             //填充椭圆
             //g.FillPie(new SolidBrush(Color.Blue), 50, 50, 200, 200, 225, 90);
             //g.FillPie(new SolidBrush(Color.DarkOrange), r, 225, 90);
        }
         void BtnRectangle_Click( object sender, EventArgs e)
        { 
             Graphics g = this. CreateGraphics();
             Rectangle r = new Rectangle( 10, 10, 200, 100);
             Pen pen1 = new Pen( Color.Black);
             //矩形
            g. DrawRectangle(pen1, 50, 50, 200, 200);   //起始坐标(50,50),宽度200,高度200
            g. DrawRectangle(pen1, r);
             //填充矩形
            g. FillRectangle( new SolidBrush( Color.Blue), 50, 50, 200, 200);
            g. FillRectangle( new SolidBrush( Color.DarkOrange), r);
    
        }
         void BtnDrawEllipse_Click( object sender, EventArgs e)
        {
               Graphics g = this. CreateGraphics();
             Rectangle r = new Rectangle( 310, 10, 500, 100);
             Pen pen1 = new Pen( Color.Black);
             //椭圆
            g. DrawEllipse(pen1, 350, 50, 200, 200);   //起始坐标(50,50),宽度200,高度200
            g. DrawEllipse(pen1, r);
             //填充椭圆
            g. FillEllipse( new SolidBrush( Color.Blue), 350, 50, 200, 200);
            g. FillEllipse( new SolidBrush( Color.DarkOrange), r);
        }
         void BtnDrawArc_Click( object sender, EventArgs e)
        {
               Graphics g = this. CreateGraphics();
             Rectangle r = new Rectangle( 10, 10, 200, 100);
             Pen pen1 = new Pen( Color.Black);
            g. DrawArc(pen1, 50, 50, 200, 200, 180, 90); //起始坐标(50,50),宽度200,高度200,弧线的起始角度180°,弧线经过角度90°
            g. DrawArc(pen1, r, 0, 135);
        }
         void BtnClear_Click( object sender, EventArgs e)
        {
    
             Graphics g = this. CreateGraphics();
            g. Clear( Color.Red);
        }
         void Ball_Click( object sender, EventArgs e)
        {
             Graphics g = this. CreateGraphics();
             Pen pen1 = new Pen( Color.Black);
             //椭圆
            g. DrawEllipse(pen1, 0, 0, 200, 200);  
            
             //g.DrawEllipse(pen1, 0, 80, 200, 30);  
             //g.DrawEllipse(pen1, 80, 0, 30, 200);  
            g. DrawArc(pen1, 0, 80, 200, 30, 0, 180);
            g. DrawArc(pen1, 80, 0, 30, 200, 90, 180);
            
             float[] dashValues = { 2, 3};
             Pen blackPen = new Pen( Color.Black, 1);
            blackPen.DashPattern = dashValues;
            g. DrawArc(blackPen, 0, 80, 200, 30, 180, 180);
            g. DrawArc(blackPen, 80, 0, 30, 200, 270, 180);
          
        }
         void DrawString_Click( object sender, EventArgs e)
        {
            System.Drawing. Graphics formGraphics = this. CreateGraphics();
             string drawString = "Sample 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();
            formGraphics. DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
            drawFont. Dispose();
            drawBrush. Dispose();
            formGraphics. Dispose();
    
        }
         void DrawVerticalString_Click( object sender, EventArgs e)
        {
            System.Drawing. Graphics formGraphics = this. CreateGraphics();
             string drawString = "Sample 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();
            drawFormat.FormatFlags = StringFormatFlags. DirectionVertical;
            formGraphics. DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
            drawFont. Dispose();
            drawBrush. Dispose();
            formGraphics. Dispose();
        }
         void DrawPath_Click( object sender, EventArgs e)
        {
             // Create an array of points for the curve in the second figure.
             Point[] points = {
             new Point( 40, 60),
             new Point( 50, 70),
             new Point( 30, 90)};
            
             GraphicsPath path = new GraphicsPath();
            
            path. StartFigure(); // Start the first figure.
            path. AddArc( 175, 50, 50, 50, 0, - 180);
            path. AddLine( 100, 0, 250, 20);
             // First figure is not closed.
            
            path. StartFigure(); // Start the second figure.
            path. AddLine( 50, 20, 5, 90);
            path. AddCurve(points, 3);
            path. AddLine( 50, 150, 150, 180);
            path. CloseFigure(); // Second figure is closed.
             Graphics g = this. CreateGraphics();
            
            g. DrawPath( new Pen( Color. FromArgb( 255, 255, 0, 0), 2), path);
        }
         void ThreedClick( object sender, EventArgs e)
        {
             Graphics g = this. CreateGraphics();
             Color red = Color. FromArgb( 255, 0, 0);
             Pen blackPen = new Pen( Color.Black, 3);
             Point point1 = new Point( 100, 100); //坐标(100,100)
             Point point2 = new Point( 500, 100); //坐标(500,100)
             //g.DrawLine(blackPen, point1, point2); //两个坐标连成直线
            g. DrawLine(blackPen, 250, 300, 700, 300); //两个坐标连成直线
            g. DrawLine(blackPen, 300, 250, 300, 700); //两个坐标连成直线
            g. DrawLine(blackPen, 300, 300, 700, 300); //两个坐标连成直线
    
        }
    }
}

猜你喜欢

转载自blog.csdn.net/r77683962/article/details/80232143