Thread: How do I draw transparent polygons?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    50

    How do I draw transparent polygons?

    Hello,

    Suppose I have two Point / PointF arrays P1 and P2. How can I draw two polygons on a panel, one from P1 and the other from P2, such that the two are transparent? Meaning, in case they overlap, I can also see the one that's under.

    From what I've seen on searches I have to use DirectX directly or something like that, but I haven't seen any clear examples. Could someone provide an example?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what functions do you use to draw opaque polygons?

    Look through the documentation for say "alpha". If you're familiar with RGB for setting a colour, then RGBA is the advanced form.
    Eg.
    RGB=( 255, 0, 0 ); // red

    then
    RGBA=( 255, 0, 0, 255 ); // solid red
    RGBA=( 255, 0, 0, 128 ); // semi-transparent red
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Well a simple solution for you would be to alter the color's opacity by setting the color's alpha value to something lower than 255. Example:

    Code:
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
    
                g.FillPolygon(new SolidBrush(Color2Transparent(Color.Red)), new Point[] {
                    new Point(10, 10),
                    new Point(100, 100),
                    new Point(10, 110)
                });
    
                g.FillPolygon(new SolidBrush(Color2Transparent(Color.Blue)), new Point[] {
                    new Point(50, 10),
                    new Point(150, 100),
                    new Point(60, 110)
                });
            }
    
            private Color Color2Transparent(Color c)
            {
                return Color.FromArgb(128, c.R, c.G, c.B); // setting opacity to about 50%
            }
        }
    }
    The result will look like this: http://i45.tinypic.com/9lj3hx.jpg

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Sorry Salem, I didn't see your post when I starting writing mine.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    50
    I was using the FillPolygon function to draw opaque polygons. I didn't know about the FromArgb function, that is what I was looking for.

    Thank you both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transparent panels overlapped
    By AtomRiot in forum C# Programming
    Replies: 3
    Last Post: 04-21-2007, 09:16 PM
  2. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11:56 PM
  3. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  4. Draw Shapes.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-19-2002, 09:22 AM
  5. Transparent Draw Question
    By GodLike in forum Windows Programming
    Replies: 5
    Last Post: 05-07-2002, 06:56 AM

Tags for this Thread