GDI+

画图

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

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

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
        // 方式一、使用point属性
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // 画图
            Graphics g = e.Graphics;  // 获取绘图工具
            g.DrawLine(Pens.Blue, 0, 0, 400, 400);  // 画一条线(从右上角到左下角)
        }
        // 方式二、通过点击button按钮来画图
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.pictureBox1.CreateGraphics();  // 通过button创建Graphics
            // 使用默认笔画图
            /*
            g.DrawLine(Pens.Red, 0, 400, 400, 0);  // 画一条线(从右下角到左上角)
            */
            //
            // 自定义笔画图
            /*
            Pen p = new Pen(Color.Yellow, 10);  // 定义画笔颜色和宽度
            g.DrawLine(p, 0, 400, 400, 0);
            */
            //
            // 使用Fill...填充
            //g.FillRectangle(Brushes.Yellow, 0, 0, 400, 300);  // 填充一个矩形,前两个0表示从(0,0)坐标开始,定义一个宽400,高300的矩形
            //
            //使用纹理刷子TextureBrush
            /*
            TextureBrush b = new TextureBrush(Image.FromFile("abc.jpg"));
            g.FillRectangle(b, 0, 0, 1200, 1200);
            */
            //
            // 线性渐变
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(400, 400), Color.Yellow, Color.Green);  // 从那个点到那个点,从什么颜色到什么颜色渐变
            g.FillEllipse(b, new Rectangle(0, 0, 400, 400));  // 画一个椭圆
        }
        //  方式三、在现有图像上创建新的画布
        private void button2_Click(object sender, EventArgs e)
        {
            Image img = Image.FromFile("abc.jpg");  // 创建一个图像属性(图片路径放在:ApplicationPro1ApplicationPro1inDebugabc.jpg)
            Graphics g = Graphics.FromImage(img);  // 想当添加背景图
            g.DrawLine(Pens.Black, 200, 0, 200, 400);  // 画线
            this.pictureBox1.Image = img;  // 放到pictureBox中去
        }
        // 字体使用
        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.pictureBox1.CreateGraphics();  // 创建绘图
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(40, 40), Color.Yellow, Color.Green);  // 线性渐变
            Font f = new Font("宋体", 80);  // 创建字体
            PointF p = new PointF(0, 0);  // 从(0,0)位置开始画
            g.DrawString("傻逼,滚", f, b, p);  // 写字
        }
    }
}
原文地址:https://www.cnblogs.com/namejr/p/10560604.html