验证码的制作

来源:互联网 发布:win7安装linux双系统 编辑:程序博客网 时间:2024/05/16 06:29
 

1、form 的 paint事件:(在load里绘制无效)
Graphics 不能直接使用new来创建需要和一个设备关联
Graphics g=this.CreateGraphics();
g.DrawLine(Pens.Red,0,0,100,100);
或者在Paint事件里还可以:e.Graphics.DrawLine(Pens.Black,100,0,100,100);

在图像上绘制:
if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                Image img = Image.FromFile(this.openFileDialog1.FileName);
                Graphics g = Graphics.FromImage(img);
                Color c = Color.FromArgb(133,30,40,60);
                Pen p = new Pen(c, 10);
                g.DrawEllipse(p, 0, 0, 60, 80);//椭圆
                //g.DrawEllipse(Pens.Blue, 0, 0, 60, 80);//椭圆

                Color c1 = Color.FromArgb(200,30,40,60);
                Pen p1 = new Pen(c1,20);
                g.DrawEllipse(p1, 0, 0, 60, 80);//椭圆
                g.FillEllipse(Brushes.Green, 20, 20, 50, 40);//填充颜色
                this.pictureBox1.Image = img;
            }

画刷:
                //画刷的使用
                //Brush b = Brushes.Gray;
                //自己定义
                SolidBrush b = new SolidBrush(c1);
                //texturebrush纹理画刷
                TextureBrush tb = new TextureBrush(Image.FromFile("E:\\图片
\\1.jpg"));
                Pen pen = new Pen(tb);
                pen.Width = 20;
                g.DrawLine(pen,0,0,40,40);
                g.FillRectangle(b,50,50,20,20);
                this.pictureBox1.Image = img;

 

扇形、弧、图片水印:
                //  g.DrawPie(p1,0,0,40,40,90,270);//扇形
                TextureBrush tb = new TextureBrush(Image.FromFile("E:\\图片
\\1.jpg"));
                Image img1 = Image.FromFile("E:\\图片
\\1.jpg");
                // g.DrawPie(p1, 0, 0, 40, 40, 90, 270);//弧
                g.DrawString("HBSI",new Font("宋体",20),tb,0,0);//写字符串
                g.DrawImage(img1, img.Width - img1.Width, img.Height - img1.Height);
                img.Save("E:\\图片
\\2.jpg");
                this.pictureBox1.Image = img;

 

绘制验证码:

原创粉丝点击