GDI: Basics
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
GDI: Basics
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Comparing Strings: For java programmers [next Lesson]  Error Handling: Catching Errors with ASP.Net and C# (Part 2)
GDI: Basics
  by: juliet

Simple GDI: Drawing a line on a form

by: juliet

This short and simple will teach you how to do simple graphics using GDI.NET

First you have have to build an empty windows form, if you don't know how to do this, please revise another tutorial: Hello World - Win Forms

After the form is build, you have to override the OnPaint event of the windows form by:
protected override void OnPaint(PaintEventArgs pe)  {

}
Then you will have to get a reference to the graphics object:
Graphics g = pe.Graphics;
Create an instance of the pen class using your favourite color:
Pen MyPen = new Pen(Color.Red);
Finally, create two points, and call the DrawLine() method of the graphics object, there you go !
Point a = new Point(50, 50);
Point b = new Point(250, 250);
g.DrawLine(MyPen, a, b);
Here is the complete code, remember to "using" System.Windows.Forms namespace and System.Drawing namespace:
using System;
using System.Windows.Forms;
using System.Drawing;


class myForm : Form {
    public myForm() {
        this.Text = "GDI Example";
    }
    
    protected override void OnPaint(PaintEventArgs pe) 
    { 
       Graphics g = pe.Graphics ; 
       
       Pen MyPen = new Pen(Color.Red);
       Point a = new Point(50, 50);
       Point b = new Point(250, 250);
       g.DrawLine(MyPen, a, b);
    } 
    
    public static void Main()
    {
        Application.Run(new myForm());
    }
}
You may compile this sample by:
csc /t:winexe /r:System.Windows.Forms.dll /r:System.Drawing.dll gdi.cs
(Type all things in one line, beware of line wrap)

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
GDI: Basics
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Comparing Strings: For java programmers [next Lesson]  Error Handling: Catching Errors with ASP.Net and C# (Part 2)


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
GazzaJ 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center