Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube
Showing posts with label GDI. Show all posts
Showing posts with label GDI. Show all posts

Tuesday 10 December 2013

Draw Rounded Rectangle Using GDI+ in C#

In the below code, I created one method called: DrawRoundedRectangle() that makes rectangle with rounded corners. Call this method from form's Paint event. I used FillPath() method to fill rectangle, you can use DrawPath() method to just draw rectangle. FillPath() contains parameter like first object of Brush and second object of GraphicsPath. DrawPath() contains parameter like first object of Pen and second object of GraphicsPath.



Rounded Corner of Rectangle shape using GDI+ in C#

.cs file 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.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.DarkGray;
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            
            Brush myBrush = new SolidBrush(Color.Blue);
            // Blue Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(50, 50, 100, 100),
                                                       5, myBrush);

            // Red Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(160, 50, 100, 100), 
                                      30, new SolidBrush(Color.Red));

            // Yellow Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(270, 50, 100, 100),
                                   50, new SolidBrush(Color.Yellow));

            // Green Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(160, 160, 100, 100),
                                 80,new SolidBrush(Color.Green));
        }
        public static void DrawRoundedRectangle(Graphics g, 
                                   Rectangle r, int d, Brush myBrush)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddArc(r.X, r.Y, d, d, 180, 90);
            gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
            gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d,
                                                             0, 90);
            gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);
            gp.AddLine(r.X, r.Y + r.Height - d, r.X, r.Y + d / 2);

            g.FillPath(myBrush, gp);
        }
    }
}

Monday 9 December 2013

Ellipse Line and Rectangle Drawing using GDI+ in C#

Below Code Snippet Illustrates basic GDI+ shape like Line, Ellipse and Rectangle. Code Generates Following  Output.

Output of basic shape using gdi+ library
Output of below code


.cs File 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.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.Cornsilk;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics dc = e.Graphics;

            //200,10 is top,left, 50=width, 50=height
            // 3 pixels wide
            Pen bluePen = new Pen(Color.Blue, 3); 
            dc.DrawEllipse(bluePen, 200, 10, 50, 50);  
            
            Pen greenPen = new Pen(Color.Green,4);
            dc.DrawRectangle(greenPen, 200, 60, 50, 50);

            Pen redPen1 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen1, 210, 110, 210, 150);

            Pen redPen2 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen2, 240, 110, 240, 150);

            Pen redPen3 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen3, 200, 60, 150, 80);

            Pen redPen4 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen4, 250, 60, 300, 40);

        }
    }
}