SIGN UP MEMBER LOGIN:    
ARTICLE

Drawing Transparent Graphics Objects in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics February 21, 2010
In this article I will explain about Drawing Transparent Graphics Objects in GDI+.
Reader Level:

This article has been excerpted from book "Graphics Programming with GDI+".

Sometimes we need to draw objects on top of images- and these objects may need to be transparent. As we discussed earlier, color in GDI+ has four components: alpha, red, green, and blue. The value of each component varies from 0 to 255. The alpha component represents the transparency in GDI+ color. Zero represents a fully transparent color; 255, a fully opaque color.

An application must create transparent pens and brushed to draw transparent graphics objects. An application can use the Color.FromArgb method to specify the ratio of all four components in a color. For example, the following code snipped creates a fully opaque green pen and brush.

            Pen solidPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);

            SolidBrush SolidColorBrush =new SolidBrush(Color.FromArgb(255, 0, 255, 0));

The following code snippet creates semitransparent colors and brushes.

            Pen transPen = new Pen(Color.FromArgb(128, 0, 255, 0), 10);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(60, 0, 255, 0));

Listing 7.24 views an image and draws lines and a rectangle with different transparencies.

LISTING 7.24: Drawing transparent graphics objects

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

            //Create an image from a file
            Image curImage = Image.FromFile ("C:/Documents and Settings/Manu/Desktop/16864z.Bmp");

            //Draw image
            g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height);

            //Create pens with different opacity
            Pen opqPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
            Pen TransPen = new Pen(Color.FromArgb(128, 0, 255, 0), 10);
            Pen totTransPen = new Pen(Color.FromArgb(40, 0, 255, 0), 10);

            //Draw Graphics object using transparent pens
            g.DrawLine(opqPen, 10, 10, 200, 10);
            g.DrawLine(TransPen, 10, 30, 200, 30);
            g.DrawLine(totTransPen, 10, 50, 200, 50);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(60, 0, 255, 0));
            g.FillRectangle(semiTransBrush, 20, 100, 200, 100);
        }

Figure 7.39 shows the output from Listing 7.24.

Figure 7.39.jpg

FIGURE 7.39: Drawing transparent graphics objects

Conclusion

Hope the article would have helped you in understanding Drawing Transparent Graphics Objects in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor