c#使用GDI绘图

视屏教程C#基础 17、Winform应用程序_GDI绘图_哔哩哔哩_bilibili

 

代码如下

复制代码
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 GDI绘图
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();//new graphics
            Pen pen = new Pen(Brushes.Red);

            Point p1 = new Point(30, 30);
            Point p2 = new Point(250, 250);

            g.DrawLine(pen, p1, p2);

        }

        int i = 0;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            i++;
            label1.Text = i.ToString(); 

        }

     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, 80);
            Rectangle rec = new Rectangle(50, 50, 80, 80);
            g.DrawRectangle(pen, rec);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Brushes.Blue);
            Rectangle rec = new Rectangle(150, 150, 80, 80);
            g.DrawPie(pen, rec, 60, 60);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawString("fuck you bitch", new Font("Times New Roman", 20, FontStyle.Underline), Brushes.Black, new Point(300, 300));
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            string str = null;
            for (int i = 0; i < 5; i++)
            {
                int rNum = r.Next(0, 10);
                str += rNum;
            }
            //MessageBox.Show(str);
            Bitmap bmap = new Bitmap(150, 40);
            Graphics g = Graphics.FromImage(bmap);


            for (int i  = 0;  i< 5; i++)
            {
                Point p = new Point(i * 20, 0);
                string[] fonts = {"宋体", "黑体", "隶书", "仿宋", "微软雅黑" };
                Color[] colors = { Color.Yellow, Color.Blue, Color.Black, Color.Red, Color.Green };
                g.DrawString(str[i].ToString(),new Font(fonts[r.Next(0,5)],20,FontStyle.Bold),new SolidBrush(colors[r.Next(0,5)]),p);
            }
            for (int i = 0; i < 30; i++)
            {
                Point p1 = new Point(r.Next(0, bmap.Width), r.Next(0, bmap.Height));
                Point p2 = new Point(r.Next(0, bmap.Width), r.Next(0, bmap.Height));
                g.DrawLine(new Pen(Brushes.Green),p1,p2);
            }
            for (int i = 0; i < 50; i++)
            {
                Point p = new Point(r.Next(0,bmap.Width), r.Next(0,bmap.Height));
                bmap.SetPixel(p.X, p.Y, Color.Black);

            }
            pictureBox1.Image = bmap;
             
        }
    }
}
复制代码

 

posted on   风中狂笑  阅读(496)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示