make red and blue pens : Pens : Graphics C# Source Code


Custom Search

C# Source Code » Graphics » Pens »

 

make red and blue pens









    

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class TestGDI1 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        
        //initialize Graphics 
        System.Drawing.Graphics g=pe.Graphics;
        
        //designate the area of the form where the drawing must take place
        //ClientRectangle is a member of Windows.Forms.Control class
        System.Drawing.Rectangle client_area=this.ClientRectangle;
        
        //point11 is at the top left corner of the client_area
        System.Drawing.Point point11=new System.Drawing.Point(client_area.X,client_area.Y); 
        //point12 is at the bottom right corner of the client area
        
        System.Drawing.Point point12=new System.Drawing.Point(client_area.Width,client_area.Height);
        
        //create a Brush object of white color
        //SolidBrush means that the color does not change from point to point
        System.Drawing.Brush background=new System.Drawing.SolidBrush(System.Drawing.Color.White);
        
        //color client_area with solid white brush
        g.FillRectangle(background,client_area);
        
        //make red and blue pens
        System.Drawing.Pen p=new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Pen p1=new System.Drawing.Pen(System.Drawing.Color.Blue);
        
        //create points and rectangles
        System.Drawing.SizeF size=new System.Drawing.SizeF();
        size.Height=160;
        size. Width=180;
        System.Drawing.PointF point=new System.Drawing.PointF();
        point.X=8;
        point.Y=40;
        System.Drawing.Point point1=new System.Drawing.Point();
        point1.X=300;
        point1.Y=300;
        System.Drawing.Point point2=new System.Drawing.Point();
        point2.X=0;
        point2.Y=0;
        System.Drawing.RectangleF rec =new System.Drawing.RectangleF(point,size);
        
        //draw an ellipse inscribed in the invisible rectangle rec
        //to change the size or shape of the ellipse change an invisible rectangle in which it is inscribed
        //to change the color of the ellipse, change the color of the pen p which is used to draw it
        
        g.DrawEllipse(p,rec);
    
        //draw a line between a pair of points point1 and point2 with pen p1
        g.DrawLine(p1,point1,point2);
    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI1());//display form
    }
}



           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Graphics
» Pens