GDI练习

本文介绍如何在WinForms中使用GDI+进行绘图操作,并实现了一个包含验证码功能的登录界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建winform项目,在其中拖入2个button和1个 PictureBox,1Openfiledialog。使用Graphics\pen\brush\color\Font\等类和对应的方法属性,完成实现课上的功能。
namespace GDI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();//这里图像对象的出事胡,不能使用new的形式
            //在初始化一个图像对象的时候,必须关联一个图像输出的设备
            g.DrawLine(Pens.Red, 0, 0, 100, 100);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Graphics g = this.CreateGraphics();//这里图像对象的出事胡,不能使用new的形式
            ////在初始化一个图像对象的时候,必须关联一个图像输出的设备
            //g.DrawLine(Pens.Red, 0, 0, 100, 100);

            //g.DrawEllipse(Pens.Purple, 150, 150, 30, 30);
            //g.DrawRectangle(Pens.Green, 200, 200, 50, 50);
            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();//这里图像对象的出事胡,不能使用new的形式
            ////在初始化一个图像对象的时候,必须关联一个图像输出的设备
            //g.DrawLine(Pens.Red, 0, 0, 100, 100);
            //g.DrawEllipse(Pens.Purple, 150, 150, 30, 30);
            //g.DrawRectangle(Pens.Green, 200, 200, 50, 50);
            e.Graphics.DrawRectangle(Pens.Blue, 10, 10, 60, 100);//画出来的图是相对于form而言
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                Image img = Image.FromFile(this.openFileDialog1.FileName);
                #region
                Graphics g = Graphics.FromImage(img);//指定图像输出设备是img
                Pen pen = new Pen(Color.Black, 10);
                Color cl = Color.FromArgb(255, 0, 0);
                Color c2 = Color.FromArgb(150, 200, 100, 210);
                Pen pen3 = new Pen(c2, 30);

                Pen PEN2 = new Pen(cl, 20);
                g.DrawLine(pen, 0, 300, 800, 300);
                g.DrawLine(Pens.Red, 0, 1020, 800, 1020);

                g.DrawEllipse(PEN2, 0, 200, 120, 60);
                g.DrawEllipse(pen3, 10, 300, 120, 120);

                g.DrawEllipse(Pens.Purple, 0, 60, 60, 60);
                g.DrawEllipse(Pens.Purple, 60, 60, 60, 60);
                g.DrawEllipse(Pens.Purple, 120, 60, 60, 60);
                g.DrawEllipse(Pens.Purple, 180, 60, 60, 60);
                g.FillEllipse(Brushes.Purple, 240, 60, 60, 60);
                g.FillEllipse(Brushes.Purple, 300, 60, 60, 60);
                g.FillEllipse(Brushes.Purple, 360, 60, 60, 60);
                g.FillEllipse(Brushes.Purple, 420, 60, 60, 60);

                g.DrawPie(pen3, 30, 40, 400, 400, 90, 50);//扇形
                g.DrawArc(PEN2, 30, 90, 400, 400, 90, 270);//弧线
                #endregion
                #region 画刷的使用
                Brush b = Brushes.Gray;
                g.FillEllipse(b, 400, 40, 80, 80);
                SolidBrush b2 = new SolidBrush(c2);
                Image img1 = Image.FromFile("G:\\getCAX11HNS.jpg");
                TextureBrush b3 = new TextureBrush(img1);
                g.FillEllipse(b2, 300, 90, 180, 180);
                g.FillPie(b2, 830, 40, 400, 400, 90, 270);//扇形
                g.FillPie(b3, 0, 0, 400, 400, 0, 45);
                g.FillRectangle(b3, 100, 100, 200, 200);
                #endregion

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

 

2.添加登陆界面,增加验证码功能。
--------登陆界面 前台------
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td><asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label></td>
    <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
    <tr>
    <td><asp:Label ID="Label2" runat="server" Text="密    码:"></asp:Label></td>
    <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
   
    </tr>
    <tr>
    <td><asp:Label ID="Label3" runat="server" Text="验证码:"></asp:Label></td>
    <td>
    <img alt="" src="pic.aspx" onclick="this.src='pic.aspx?aaa='+new Date()"  /></td>
    </tr>
    <tr>
    <td><asp:Label ID="Label4" runat="server" Text="请输入验证码:"></asp:Label></td>
    <td> <asp:TextBox ID="TextBox3" runat="server" style="margin-bottom: 0px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td></td><td><asp:Button ID="Button1" runat="server" Text="登   陆"
            onclick="Button1_Click" /></td>
    </tr>
   
   
    </table>
    </div>
   
   
 
   
  
   
    </form>
</body>
</html>

-------后台---------

    public partial class 登陆界面aspx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "admin" && TextBox2.Text == "123")
            {
                Session["login"] = true;
                Response.Redirect("index.aspx");
            }
            else
            {
                Response.Write("用户名或密码或验证码不正确!");
            }
        }
    }

----pic.aspx.cs-------
namespace 验证码
{
    public partial class pic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Drawing.Image img = new Bitmap(150,50);
            Graphics g = Graphics.FromImage(img);
            //g.DrawString("hello", new Font("宋体", 12), Brushes.Gray, 0, 0);
            this.AddPoint(img,100);
            string code = this.GeneralCode();
            Font font1 = new Font("宋体",20,FontStyle.Italic);
            g.DrawString(code, font1, Brushes.Red, 0, 0);
            this.Response.Clear();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            this.Response.BinaryWrite(ms.ToArray());
            this.Response.Flush();
            this.Response.End();

        }

        private void AddPoint(System.Drawing.Image img, int nums)//加噪点背景
        {
            Bitmap b = img as Bitmap;
            Random ran = new Random();
            for (int i = 0; i < nums; i++)
            {
                b.SetPixel(ran.Next(0, img.Width), ran.Next(0, img.Height), Color.White);
            }
        }
        //生成随机的文字
        private string GeneralCode()
        {
            Random ran = new Random(DateTime.Now.Millisecond);
            StringBuilder sb = new StringBuilder(6);
            for (int i = 0; i < 6; i++)
            {
                sb.Append(ran.Next(0,9));
           
            }
            return sb.ToString();
       
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值