Graphics in C# - Irregular Forms

CK CK 3788 
Created at  
906 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 Facebook X
Comments 0
Similar posts
  1. Extracting the Country from IP Address
    465
  2. How to restrict a program to a single instance
    727
  3. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  4. How to speed up access to the RichTextBox Lines property
    828
  5. How to print character in C# by ASCII code
    475
  6. How To Print Text In C#
    453
  7. How to send binary data through C# CGI app?
    993
  8. Calling Your Main Thread From A Worker Thread In C#
    544
  9. How To Read And Write A Cookie
    359
  10. Using C# With Cookies
    620
  11. To load image from file
    446
  12. How to convert bitmap image to jpeg image ?
    353
  13. How to resize and save PNG or Jpeg Image file ?
    629
  14. How to convert text string to integer ?
    608
  15. How to convert integer to text string ?
    696
  16. How to get process id in C# like windows task manager ?
    867
  17. How To Check If Your Printer Is Connected Using C#
    1,025
  18. How to download/grap web pages or XML document at internet website in Delphi XE?
    868
  1. How to load HTML resource on MFC ?
    839
  2. How to get GM Time as String ?
    422
  3. Makefile to automate the build process for C++
    276
  4. Finding process name on OS
    405
  5. Convert System.DateTime to UNIX timestamp
    380
  6. Retrieve system information
    494
  7. How to call SetTimer function on MFC CDialog class ?
    650
  8. How to rename(change file name) in C# ?
    1,072
  9. Find Files in certain directory
    924
  10. Implementing C#'s foreach loop in Delphi 8
    459
  11. How to detect resource leaks in a Form
    936
  12. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  13. How to exit from a console application
    809
  14. How to restrict a program to a single instance
    727
  15. Compress and decompress strings in C#
    970
  16. Extracting the Country from IP Address
    465
  17. How to run shell command by MFC ?
    434
  18. Binary Search Sample Code
    403
  19. Double Linked List based in C++
    796
  20. String Replace Function For C#
    1,542
  21. How to print character in C# by ASCII code
    475
  22. MFC based World Wide Web HTTP Server Source Code
    458
  23. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    470
  24. UDP Send and Receive Using CAsyncSocket
    1,204
  25. Using the shell to receive notification of removable media being inserted or removed
    680
  26. Adding Custom Paper Sizes To Named Printers
    644
  27. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    971
  28. A Customizable Printing Text Class
    427
  29. Simplified .NET Printing In C#
    689
  30. How To Print Text In C#
    453
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,711
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    327
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252