c#winform圖片繪製和圖片驗證碼

來源:互聯網
上載者:User

標籤:rgs   gre   public   end   space   this   tpi   c#   initial   

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//一張紙 一根筆 一個人 兩個點

}
//畫直線
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gp = this.CreateGraphics();
Pen p = new Pen(Color.Blue);
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 300);
gp.DrawLine(p, p1, p2);
}

//畫多線直線
private void button1_Click(object sender, EventArgs e)
{
//建立一個GDI+ 對象
Graphics g = this.CreateGraphics();
//建立畫筆對象
Pen pen = new Pen(Color.Blue);
//建立兩個點
Point p1 = new Point(100, 100);
Point p2 = new Point(30, 400);
//在畫板中用pen在p1和p2兩個點之間畫一條直線
g.DrawLine(pen, p1, p2);
//pos為point數組,在畫板中畫多條直線
Point p3 = new Point(300, 120);
Point p4 = new Point(15, 200);
Point[] pos = { p1, p2, p3, p4 };
g.DrawLines(pen, pos);
}

//畫矩形
private void button2_Click(object sender, EventArgs e)
{
//建立GDI+對象
Graphics gp = this.CreateGraphics();
//建立矩形對象 左上方度座標 寬 高
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
gp.DrawRectangle(new Pen(Color.Blue), rec);
}

//填充矩形
private void button3_Click(object sender, EventArgs e)
{
//建立GDI+對象
Graphics gp = this.CreateGraphics();
//給定要填充的矩形對象
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
//填充顏色 擷取系統色彩 給定要填充的矩形
gp.FillRectangle(Brushes.DarkGreen, rec);
}

//畫扇形,並填充
private void button4_Click(object sender, EventArgs e)
{
//建立GDI+對象
Graphics gp = this.CreateGraphics();
//給定要填充的矩形對象
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
//繪製扇形 pen對象 矩形 右角 左角度
gp.DrawPie(new Pen(Color.Red), rec, 60, 60);
//填充扇形 擷取系統對象 矩形 右角 左角度
gp.FillPie(Brushes.DarkGreen, rec, 60, 60);
}

//畫文字
private void button5_Click(object sender, EventArgs e)
{
//建立GDI+對象
Graphics g = this.CreateGraphics();
//繪製文本 文本 字型樣式: 字型 字型大小 樣式粗?斜?... 擷取系統色彩 繪製到的座標點
g.DrawString("中國您好!", new Font("華文行楷", 20, FontStyle.Italic), Brushes.Red, new Point(300, 300));
}

//驗證碼
private void pictureBox1_Click(object sender, EventArgs e)
{
//產生1到9之間的4個隨機數
Random r = new Random();
string str = "";
for (int i = 0; i < 4; i++)
{
int rNumber = r.Next(0, 10);
//累加到Null 字元串中
str += rNumber;
}

//建立GDI+對象
//建立圖片對象, 指定 寬 和 高
Bitmap bm = new Bitmap(115,30);;
//從指定的Image對象建立新的Grapics畫板
Graphics g = Graphics.FromImage(bm);

//數組存放字型
string[] fonts = {"黑體","微軟雅黑","隸書","楷體"};
//數組存放顏色
Color[] colors = {Color.Red,Color.Yellow,Color.Pink,Color.Purple};
//畫字
for (int i = 0; i < 4; i++)
{
//指定座標
Point p = new Point(i*20,0);
//畫文字
g.DrawString(str[i].ToString(), new Font(fonts[i], 20, FontStyle.Italic), new SolidBrush(colors[i]),p);

}
//畫線
for (int j = 0; j < 16; j++)
{
Point p1 = new Point(r.Next(0,bm.Width),r.Next(bm.Height));
Point p2 = new Point(r.Next(0,bm.Width),r.Next(0,bm.Height));
g.DrawLine(new Pen(Color.Green), p1, p2);
}
//畫像素點
for (int i = 0; i < 100; i++)
{
Point p1 = new Point(r.Next(0,bm.Width),r.Next(bm.Height));
Point p2 = new Point(r.Next(0,bm.Width),r.Next(0,bm.Height));
bm.SetPixel(p1.X, p1.Y, Color.Black);
}

//將bm這個圖片直接鑲嵌到pictureBox上
pictureBox1.Image = bm;
}
}
}

c#winform圖片繪製和圖片驗證碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.