C#winform picture Drawing and image verification code

Source: Internet
Author: User

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)
{
A piece of paper, a pen, a man, two points.

}
Draw a line
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);
}

Draw Multi-line lines
private void Button1_Click (object sender, EventArgs e)
{
Create a GDI + object
Graphics g = this. CreateGraphics ();
Create a Brush object
Pen pen = new Pen (color.blue);
Create two points
Point P1 = new Point (100, 100);
Point P2 = new Point (30, 400);
Draw a line between P1 and P2 two points in the artboard using a pen
G.drawline (pen, p1, p2);
POS is a point array, drawing multiple lines in the artboard
Point p3 = new Point (300, 120);
Point P4 = new Point (15, 200);
Point[] pos = {p1, p2, p3, P4};
G.drawlines (pen, POS);
}

Draw a rectangle
private void Button2_Click (object sender, EventArgs e)
{
Create GDI + objects
Graphics GP = this. CreateGraphics ();
Create a Rectangle object upper left angle seat width height
Rectangle rec = new Rectangle (new point), New Size (100, 300));
Gp. DrawRectangle (New Pen (Color.Blue), rec);
}

Fill Rectangle
private void Button3_Click (object sender, EventArgs e)
{
Create GDI + objects
Graphics GP = this. CreateGraphics ();
Given the rectangle object to fill
Rectangle rec = new Rectangle (new point), New Size (100, 300));
Fill color get system color given the rectangle to fill
Gp. FillRectangle (Brushes.darkgreen, rec);
}

Draw a fan, and fill
private void Button4_Click (object sender, EventArgs e)
{
Create GDI + objects
Graphics GP = this. CreateGraphics ();
Given the rectangle object to fill
Rectangle rec = new Rectangle (new point), New Size (100, 300));
Draw Sector Pen Object rectangle Right Angle left angle
Gp. Drawpie (New Pen (color.red), REC, 60, 60);
Fill sector get System object Rectangle right Angle left angle
Gp. FillPie (Brushes.darkgreen, REC, 60, 60);
}

Drawing text
private void Button5_click (object sender, EventArgs e)
{
Create GDI + objects
Graphics g = this. CreateGraphics ();
Draw text text font style: Font size style bold? Oblique? ... Gets the seat punctuation that the system color draws to
g.DrawString ("Hello China!", New Font ("Chinese Xingkai", "Fontstyle.italic"), Brushes.Red, New Point (300, 300));
}

Verification Code
private void PictureBox1_Click (object sender, EventArgs e)
{
Generate 4 random numbers from 1 to 9
Random r = new Random ();
String str = "";
for (int i = 0; i < 4; i++)
{
int rnumber = r.next (0, 10);
Accumulate into an empty string
str + = Rnumber;
}

Create GDI + objects
Create a Picture object, specify width and height
Bitmap BM = new Bitmap (115,30);
Creates a new grapics artboard from the specified image object
Graphics g = graphics.fromimage (BM);

Array Store Font
string[] fonts = {"Bold", "Microsoft Black", "Official script", "italics"};
Array storage Color
color[] colors = {Color.red,color.yellow,color.pink,color.purple};
Stooped
for (int i = 0; i < 4; i++)
{
Specify the coordinates
Point P = new Point (i*20,0);
Drawing text
g.DrawString (Str[i]. ToString (), New Font (Fonts[i], fontstyle.italic), New SolidBrush (Colors[i]), p);

}
Draw Line
for (int j = 0; J <; 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);
}
Portrait point
for (int i = 0; i <; 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);
}

Set the BM image directly onto the PictureBox.
pictureBox1.Image = BM;
}
}
}

C#winform picture Drawing and image verification code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.