Drawing Things with GDI

Lardmeister 0 Tallied Votes 319 Views Share

There are plenty of things you can code in C# without having to rely on the ever so omnipresent MS-VCS. Here is an example of some typical drawings like lines, arcs, circles, text, curves and gradients you can create with C# and actually retain some control over the program. This program has been around, I just improved it a lot.

// exploring C# GDI+ graphics
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
// also works well (uses csc.exe) with SnippetCompiler.exe 
// from: http://www.sliver.com/dotnet/SnippetCompiler/

using System;
using System.Drawing;            // GDI+ resides here
using System.Windows.Forms;
using System.Drawing.Drawing2D;  // more GDI+ here

namespace UsingGDIPlus
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    private void InitializeComponent() 
    {
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(392, 349);
      this.Name = "Form1";
      this.Text = "Exploring GDI+ Graphics";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }

    static void Main() 
    {
      Application.Run(new Form1());
    }

    // the paint event
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics ; 
      Rectangle rect = this.ClientRectangle; 
 
      // neat, paints a gradiant background for the window form 
      LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Blue, Color.Yellow, 
        LinearGradientMode.BackwardDiagonal); 
      g.FillRectangle(lBrush, rect);
 
      // draws an arc 
      Pen pn = new Pen(Color.Lime, 4); 
      rect = new Rectangle(10, 50, 300, 150); 
      g.DrawArc(pn, rect, 10, 80); 
  
      // draws a line with a black pen 
      Pen pn2 = new Pen(Color.Black, 3); 
      Point pt1 = new Point(30, 30); 
      Point pt2 = new Point(150, 30); 
      g.DrawLine(pn2, pt1, pt2);
 
      // draw white graphic text 
      g.DrawString("Lard is better than War!", this.Font, new SolidBrush(Color.White), 30,10);
 
      // draws an ellipse/circle
      Pen pn3 = new Pen(Color.Aqua , 3); 
      rect = new Rectangle(ClientRectangle.Right - 85, ClientRectangle.Bottom - 200, 50, 50); 
      g.DrawEllipse(pn3, rect);
 
      // draw with bezier curves 
      GraphicsPath path = new GraphicsPath( 
        new Point[]
        {
          new Point(40, 140), new Point(275, 200), 
          new Point(105, 225), new Point(190, ClientRectangle.Bottom), 
          new Point(50, ClientRectangle.Bottom), new Point(20, 180), 
        },
        new byte[] 
        {
          (byte)PathPointType.Start, 
          (byte)PathPointType.Bezier, 
          (byte)PathPointType.Bezier, 
          (byte)PathPointType.Bezier, 
          (byte)PathPointType.Line, 
          (byte)PathPointType.Line, 
        }
      );
      
      // more gradient stuff
      PathGradientBrush pgb = new PathGradientBrush(path); 
      pgb.SurroundColors = new Color[] { 
        Color.Green,Color.Yellow,Color.Red, Color.Blue, 
        Color.Orange, Color.Thistle, 
      };
      
      g.FillPath(pgb, path); 
    }
  }
}
Ali.M.Habib 0 Newbie Poster

what if we want to draw rectangle using mouse and how can we move it please

waterfall 0 Newbie Poster

How can we connect circles dynamically while taking imput from the user.e.g if user asks to connect circle 4 with circle 2 and circle2?

nido 0 Newbie Poster

just follow the mouse down event and where the mouse x and y direction is moving

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.