GDI+最简单的理解就是用来绘图的。其中包括点、直线、矩形、字符串等等。

先简单来个例子,说明如何在winform窗体中绘制一条直线,并且这条直线不随着窗体的移动而消失。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GDIDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //一根笔 一张纸 两点 绘制直线的对象
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个GDI对象
            Graphics g = this.CreateGraphics();//new Graphics();
      
            //创建一支画笔对象
            Pen pen = new Pen(Brushes.Yellow);
            
            //创建两个点
            Point p1 = new Point(30, 50);
            Point p2 = new Point(250, 250);

            g.DrawLine(pen, p1, p2);
        }

        /// <summary>
        /// 重新绘制窗体的时候,直线也重新画一遍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建一个GDI对象
            Graphics g = this.CreateGraphics();//new Graphics();

            //创建一支画笔对象
            Pen pen = new Pen(Brushes.Yellow);

            //创建两个点
            Point p1 = new Point(30, 50);
            Point p2 = new Point(250, 250);

            g.DrawLine(pen, p1, p2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Brushes.Yellow);
            Size size=new System.Drawing.Size(80,120);
            Rectangle rec = new Rectangle(new Point(50,50),size);
            g.DrawRectangle(pen, rec);
        }
    }
}
GDI绘制直线

相关文章:

  • 2022-03-09
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
猜你喜欢
  • 2022-02-09
  • 2022-02-10
  • 2022-12-23
  • 2022-01-03
  • 2022-01-31
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案