Graphics in C# - Irregular Forms

CK1820 
Created at Sep 25, 2007 05:42:28 
53   0   0   0  
Quite frankly, I think this topic is a lot of fun...the thought of making an app look completely different from the usual rectangular-formed layout really appeals to me.

Drawing an irregular form, like drawing anything, requires four things : a surface, a stylus, a color and something to draw.

Surface

The surface upon which you draw is equivivalent to two different objects within the Framework: the Graphics object and the Region Object. The Graphics object provides the methods for drawing to the device context. A device context is analogous to a co-ordinate system laid on top of the form. Useful items such as Position can be extrapolated from this object.
The Graphics object uses, what is known as Vector Graphics, which are drawings that exist on a co-ordinate system and are ideal for 2-D creations. The bounds of the Graphics object are always defined by a rectangle. The available drawing surface though, may not be a rectangle.
The Region object describes the interior of a Graphics object. This region is the actual available drawing surface. It can be a rectangle or any polygonal area. This object is typically used to limit the drawing capabilities on a form.

Stylus

This is the tool with which you draw. The .NET Framework gives you two such tools : the pen and the brush.
The purpose of the pen is to draw lines and curves. Thus, the pen can be used to draw borders, graphs and outlines of regions or shapes.
The other stylus in the toolkit is the brush. There are five types of brushes : HatchBrush, LinearGradientBrush, PathGradientBrush, SolidBrush and TextureBrush.

Color

Colors available in the Framework are categorized under two headings : KnownColor and SystemColors.
A KnownColor is essentially a color with a warm and fuzzy name : for eg. , in this enumeration you can find colors named Bisque, AliceBlue, PapayaWhip, PeachPuff etc.
Under the SystemColors class, you find the system-level settings for items in the operating system such as WindowFrame, ActiveBorder, Desktop etc.
You can also specify new colors not already defined. This is commonly done using the FromARGB method. This method requires the Alpha component; and the Red, Green and Blue components.

Object to Draw

After everything is said and done, nothing in the previous section does you any good if you don't have something to draw. So, experiment and play around. If you find something that you are interested in depicting programmatically, take a cubist approach and see what happens. That's what I did.

Here's a Sample Code (the FormBorderStyle property of the Form is set to None)
 
using System.Drawing.Drawing2D;
using System.Drawing.Drawing2D;
 
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        
 {            
 GraphicsPath gp = new GraphicsPath();            
 Graphics gr = this.CreateGraphics();            
 Region r;            
 RectangleF rf;
        PointF[] p = new PointF[9];            
    Font f;            
    p[0] = new PointF(70, 0);            
    p[1] = new PointF(170, 0);            
    p[2] = new PointF(240, 70);            
    p[3] = new PointF(240, 170);
    p[4] = new PointF(170, 240);            
    p[5] = new PointF(70, 240);            
    p[6] = new PointF(0, 170);            
    p[7] = new PointF(0, 70);            
    p[8] = new PointF(70, 0);   
    gp.AddPolygon(p);            
    PathGradientBrush pgb = new PathGradientBrush(p);
    Color[] c = new Color[2];            
    c[0] = Color.Honeydew ;           
    c[1] = Color.SeaShell;            
    pgb.CenterColor = Color.Chocolate;
    pgb.SurroundColors = c;            
    gr.FillPolygon(pgb, p);            
    r = new Region(gp);            
    this.Region = r;            
    gr = this.CreateGraphics();
    gr.DrawPolygon(new Pen(Color.White, 5), p);
    gr.DrawPolygon(new Pen(Color.Black, 3), p);
    rf = new RectangleF(15, 105, 240, 115);
    f = new Font(FontFamily.GenericMonospace, 13, FontStyle.Bold);
    gr.DrawString("Irregular Form Demo", f, new LinearGradientBrush(gr.VisibleClipBounds,
        Color.Blue, Color.Black, LinearGradientMode.BackwardDiagonal), rf);
    f.Dispose();
    gr.Dispose();
    gp.Dispose();
    r.Dispose();                    
 }




Tags: Base64Decode C# CreateGraphics DrawPolygon LinearGradientBrush PathGradientBrush Share on Facebook Share on X

◀ PREVIOUS
Extracting the Country from IP Address
▶ NEXT
Compress and decompress strings in C#
  Comments 0
Login for comment
SIMILAR POSTS

Compress and decompress strings in C# (created at Sep 25, 2007)

Extracting the Country from IP Address (created at Sep 25, 2007)

How to restrict a program to a single instance (created at Sep 25, 2007)

How to exit from a console application (created at Sep 25, 2007)

The best way to enable and disable buttons, menu items and toolbar buttons (created at Sep 25, 2007)

How to detect resource leaks in a Form (created at Sep 25, 2007)

How to speed up access to the RichTextBox Lines property (created at Sep 25, 2007)

String Replace Function For C# (created at Aug 28, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

A Customizable Printing Text Class (created at Aug 27, 2007)

Simplified .NET Printing In C# (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

C Sharp Lists (created at Aug 27, 2007)

Simple Example Of IF/THEN Using C# (created at Aug 27, 2007)

Simple C# CGI working on Apache (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Interacting With TinyPic From C# (created at Aug 26, 2007)

Creating A Watched Folder With Assigned Events (created at Aug 26, 2007)

Calling Your Main Thread From A Worker Thread In C# (created at Aug 26, 2007)

HashTable Tutorial In C# (created at Aug 26, 2007)

Stack Tutorial In C# (created at Aug 26, 2007)

Queue Tutorial In C# (created at Aug 26, 2007)

Create PDF Files On the Fly In C Sharp (created at Aug 26, 2007)

How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport (created at Aug 26, 2007)

Send Email Using C# (created at Aug 26, 2007)

How To Read And Write A Cookie (created at Aug 26, 2007)

Using C# With Cookies (created at Aug 26, 2007)

Find Files in certain directory (created at Feb 25, 2008)

OTHER POSTS IN THE SAME CATEGORY

URL Encode, Decode function for MFC (updated at Dec 20, 2023)

How to load HTML resource on MFC ? (updated at Dec 17, 2023)

How to get GM Time as String ? (created at Mar 20, 2009)

Finding process name on OS (created at Sep 22, 2008)

Convert System.DateTime to UNIX timestamp (created at Sep 22, 2008)

Retrieve system information (created at Sep 22, 2008)

How to call SetTimer function on MFC CDialog class ? (created at Jul 30, 2008)

How to rename(change file name) in C# ? (created at Mar 12, 2008)

Find Files in certain directory (created at Feb 25, 2008)

Implementing C#'s foreach loop in Delphi 8 (created at Oct 03, 2007)

How to detect resource leaks in a Form (created at Sep 25, 2007)

The best way to enable and disable buttons, menu items and toolbar buttons (created at Sep 25, 2007)

How to exit from a console application (created at Sep 25, 2007)

How to restrict a program to a single instance (created at Sep 25, 2007)

Compress and decompress strings in C# (created at Sep 25, 2007)

Extracting the Country from IP Address (created at Sep 25, 2007)

How to run shell command by MFC ? (created at Sep 09, 2007)

Binary Search Sample Code (created at Sep 08, 2007)

Double Linked List based in C++ (created at Sep 08, 2007)

String Replace Function For C# (created at Aug 28, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

Creating and Using a CAsyncSocket Object to use CAsyncSocket (created at Aug 28, 2007)

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

A Customizable Printing Text Class (created at Aug 27, 2007)

Simplified .NET Printing In C# (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)