Thursday, 11 August 2016

GDI+ in Window Form Application


In window form you might want to create some shapes other graphical objects, In such situation you can use the graphic elements of window Forms which helps to create a visually application. In Win Forms GDI+ plus is used to create graphics, draw text, manipulate graphical images.





The core of GDI+ functionality is the Graphics class. GDI+ having set of base class from which it can use to make custom drawing on the screen. The set of such class is collectively called the GDI+ managed class interfaces. This class is the part of .NET Framework. It Provides and environment for creating, deploying, and running XML web services and other applications.


The GDI+ managed class interfaces consist of about 60 classes, 50 enumerations, and 8 structures. You can group these class to a various namespaces. Some of are:

  1. System.Drawing.
  2. System.Drawing.Drawing.2D
  3. System.Drawing.Imaging
  4. System.Drawing.Printing
  5. System.Drawing.Text
  6. System.Drawing.Design



Here is the simple window form application:


Make your design page as like this:


C:\Users\Nilesh\Desktop\gdi_design.jpg




Here in this case when user click on first button it will get the shape that we had made in the code behind file using GDI.


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 button1_Click(object sender, EventArgs e)
       {
           Graphics g1 = CreateGraphics();
           Pen p = new Pen(Color.Blue, 2);
           g1.DrawEllipse(p, 150, 20, 50, 50);
           g1.DrawEllipse(p, 160, 35, 10, 10);
           g1.DrawEllipse(p, 180, 35, 10, 10);
           g1.DrawArc(p, 150, 35, 50, 30, 45, 90);
           g1.DrawRectangle(p, 150, 75, 50, 100);
           g1.DrawLine(p, 175, 175, 150, 250);
           g1.DrawLine(p, 175, 175,200 , 250);
           
       }

       private void button2_Click(object sender, EventArgs e)
       {
           Graphics g2 = CreateGraphics();
           SolidBrush sbr = new SolidBrush(Color.Chocolate);
           g2.FillEllipse(sbr, 150, 25, 50, 50);
           sbr.Color = Color.Gray;
           g2.FillEllipse(sbr, 160, 35, 10, 10);
           g2.FillEllipse(sbr, 180, 35, 10, 10);
           sbr.Color = Color.Black;
           g2.FillPie(sbr, 150, 35, 50, 30, 45, 90);
           sbr.Color = Color.Bisque;
           g2.FillRectangle(sbr, 150, 75, 50, 100);


       }
   }
}




OUTPUT:







Hope you like it. Thank you for Reading. Have a good day.






No comments:

Post a Comment