C # winform Image Rendering and image verification code,

Source: Internet
Author: User

C # winform Image Rendering and image verification code,

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)
{
// One piece of paper, one pen, one person, two dots

}
// Draw a straight 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 multiple lines
Private void button#click (object sender, EventArgs e)
{
// Create a GDI + Object
Graphics g = this. CreateGraphics ();
// Create a paint 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 straight line between p1 and p2 using a pen on the drawing board
G. DrawLine (pen, p1, p2 );
// Pos is a point array and multiple straight lines are drawn in the drawing board.
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 a GDI + Object
Graphics gp = this. CreateGraphics ();
// Create the coordinate width and height of the upper left corner of the rectangle object
Rectangle rec = new Rectangle (new Point (100, 10), new Size (100,300 ));
Gp. DrawRectangle (new Pen (Color. Blue), rec );
}

// Fill the rectangle
Private void button3_Click (object sender, EventArgs e)
{
// Create a GDI + Object
Graphics gp = this. CreateGraphics ();
// Specify the rectangular object to be filled
Rectangle rec = new Rectangle (new Point (100, 10), new Size (100,300 ));
// Fill color to obtain the rectangle given by the system color to be filled
Gp. FillRectangle (Brushes. DarkGreen, rec );
}

// Draw slices and fill them
Private void button4_Click (object sender, EventArgs e)
{
// Create a GDI + Object
Graphics gp = this. CreateGraphics ();
// Specify the rectangular object to be filled
Rectangle rec = new Rectangle (new Point (100, 10), new Size (100,300 ));
// Draw the left angle of the right corner of the slice pen object rectangle
Gp. DrawPie (new Pen (Color. Red), rec, 60, 60 );
// Fill the slice to obtain the left angle of the right corner of the System Object rectangle
Gp. FillPie (Brushes. DarkGreen, rec, 60, 60 );
}

// Draw text
Private void button5_Click (object sender, EventArgs e)
{
// Create a GDI + Object
Graphics g = this. CreateGraphics ();
// Draw text font style: font size? Oblique ?... Obtains the coordinate point drawn by the system color.
G. DrawString ("Hello, China! ", New Font (" 文 ", 20, FontStyle. Italic), Brushes. Red, new Point (300,300 ));
}

// Verification Code
Private void picturebox#click (object sender, EventArgs e)
{
// Generate four random numbers between 1 and 9
Random r = new Random ();
String str = "";
For (int I = 0; I <4; I ++)
{
Int rNumber = r. Next (0, 10 );
// Accumulate to an empty string
Str + = rNumber;
}

// Create a GDI + Object
// Create an image object, specifying the width and height
Bitmap bm = new Bitmap (115,30 );;
// Create a new Grapics canvas from the specified Image object
Graphics g = Graphics. FromImage (bm );

// Font of the array
String [] fonts = {"", ""};
// Color of the array
Color [] colors = {Color. Red, Color. Yellow, Color. Pink, Color. Purple };
// Draw words
For (int I = 0; I <4; I ++)
{
// Specify coordinates
Point p = new Point (I * 20, 0 );
// Draw text
G. DrawString (str [I]. ToString (), new Font (fonts [I], 20, FontStyle. Italic), new SolidBrush (colors [I]), p );

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

// Set the bm image to pictureBox.
PictureBox1.Image = bm;
}
}
}

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.