How to Use Paint and the Graphics
Before you can draw lines and shapes, render text, or display and manipulate images with GDI+, you need to create a graphics object. The graphics object represents a GDI+ drawing surface, and is the object that is used to create graphical lines.
There are two steps in working with graphics:
1. Creating a graphics
2. object.
3. Using the graphics
4. object to draw lines and shapes, render text, or display and manipulate images.
C#(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;
using System.Threading.Tasks;
namespace How_to_Use_Paint_and_the__Graphics
{
public partial class Form1 : Form
{
Pen mypen = new Pen(Color.Black);
Graphics g = null;
static int center_1, center_y;
static int Start_x, Start_y;
static int end_x, end_y;
static int my_angel = 0;
static int My_Leanth = 0;
static int number_lines=0;
static int My_icrement = 0;
public Form1()
{
InitializeComponent();
Start_x = canvas.Width / 2;
Start_y = canvas.Height / 2;
}
private void canvas_Paint(object sender, PaintEventArgs e)
{
mypen.Width = 1;
My_Leanth=Int32.Parse(Leanth.Text);
g = canvas.CreateGraphics();
for (int i=0; i<Int32.Parse(Number_Of_lines.Text); i++)
DrawLine();
}
private void DrawLine()
{
Random randamgren = new Random();
mypen.Color = Color.FromArgb(randamgren.Next(255), randamgren.Next(255), randamgren.Next(255));
my_angel = my_angel + Int32.Parse(Angel.Text);
My_Leanth = My_Leanth + Int32.Parse(Leanth.Text);
end_x=(int)(Start_x + Math.Cos(my_angel *.017453292519) * My_Leanth);
end_y = (int)(Start_y + Math.Sin(my_angel * .017453292519) * My_Leanth);
Point[] points =
{
new Point(Start_x,Start_y),
new Point(end_x,end_y)
};
Start_x = end_x;
Start_y = end_y;
g.DrawLines(mypen,points);
}
private void button1_Click(object sender, EventArgs e)
{
My_Leanth = Int32.Parse(Leanth.Text);
My_icrement = Int32.Parse(Increment.Text);
my_angel = Int32.Parse(Leanth.Text);
Start_x = canvas.Width / 2;
Start_y = canvas.Height / 2;
canvas.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Comments
Post a Comment