使用Graphics、pen、brush、color、Font类和对应的方法属性

来源:互联网 发布:南宁软件培训 编辑:程序博客网 时间:2024/06/05 17:06

---------------后台:-----------------------
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawLine(Pens.Red, 0, 0, 100, 100);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Graphics g = this.CreateGraphics();
            //g.DrawLine(Pens.Red, 0, 0, 100, 100);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.DrawRectangle(Pens.Blue, 10, 10, 60, 100);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = this.CreateGraphics();
            //g.DrawLine(Pens.Red, 0, 0, 100, 100);
            Graphics g = this.pictureBox1.CreateGraphics();
            e.Graphics.DrawRectangle(Pens.Blue, 10, 10, 60, 100);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Image img = Image.FromFile(this.openFileDialog1.FileName);
                Graphics g = Graphics.FromImage(img);
                Pen pen = new Pen(Color.Black, 10);
                Color c1 = Color.FromArgb(255, 0, 250);
                Color c2 = Color.FromArgb(150, 200, 100, 210);
                Pen pen2 = new Pen(c1, 20);
                Pen pen3 = new Pen(c2, 30);

                g.DrawLine(Pens.Red, 0, 300, 800, 300);
                g.DrawLine(Pens.Red, 0, 1020, 800, 1020);

                g.DrawEllipse(pen2, 0, 200, 120, 60);
                g.DrawEllipse(pen3, 0, 300, 150, 100);
                for (int i = 0; i < 121; i = i + 40)
                {
                    g.DrawEllipse(Pens.Green, i, 40, 40, 40);
                }
                for (int n = 160; n < 361; n = n + 40)
                {
                    g.FillEllipse(Brushes.Green, n, 40, 40, 40);
                }

                g.DrawPie(pen3, 230, 40, 200, 200, 90, 270);//画扇形

                g.DrawArc(pen2, 30, 190, 200, 200, 90, 270);//画弧线
                #region 画刷的使用
                Brush b = Brushes.Gray;
                g.FillEllipse(b, 400, 40, 80, 80);
                SolidBrush b2 = new SolidBrush(c2);
                g.FillPie(b2, 530, 40, 100, 100, 90, 270);//画扇形

                Image img1 = Image.FromFile("E:\\wenli.jpg");
                TextureBrush b3 = new TextureBrush(img1);
                g.FillEllipse(b2, 300, 90, 180, 180);
                g.FillPie(b3, 0, 0, 400, 400, 0, 45);
                g.FillPie(b3, 0, 0, 400, 400, 90, 45);
                g.FillPie(b3, 0, 0, 400, 400, 180, 45);
                g.FillPie(b3, 0, 0, 400, 400, 270, 45);
                g.FillRectangle(b3, 350, 350, 100, 100);
                #endregion

                //字
                g.DrawString("HBSI", new Font("宋体", 40), b3, 0, 0);
                //图
                g.DrawImage(img1, img.Width - img1.Width - 20, img.Height - img.Height - 20);
                img.Save("E:\\11.jpg");
                this.pictureBox1.Image = img;
            }
        }
    }

原创粉丝点击