SIGN UP MEMBER LOGIN:    
ARTICLE

Adding Colors, Pens, and Brushes to the GDI+Painter Application

Posted by Mahesh Chand Articles | GDI+ & Graphics February 11, 2010
In this article I will explain about working with brushes and pens in GDI+.
Reader Level:

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

In this section we will extend the functionality of GDI+Painter by adding support for brushes and pens. After completing this section, you will be able to select a pen color and its width, color transparency, and brush color.

Figure 4.32 shows the modified version of GDI+Painter without any objects.

Transparency is a component of the color in GDI+. In the .NET Framework library; the Color structure represents a color. It has four components: alpha (A), red(R), green (G), and blue (B). The alpha component of the Color structure represents the transparency of a color. The alpha component values vary from 0 to 255, where 0 is fully transparent and 255 is fully opaque. Tocreate a transparent brush or pen, we create a color using the alpha value and use the color to create a pen or a brush. 

Figure-4.32.jpg

FIGURE 4.32: GDI+ Painter with pen and brush support

The following code snippet shows how to create a color with transparency. We use the same method to add transparency to our application.

            Color clr = Color.FromArgb(Convert.ToInt16 (TransCounter.Value.ToString()), PenBtn.BackColor.R, PenBtn.BackColor.G, PenBtn.BackColor.B);

In our modified version of GDI+Painter, the width selector numeric up-down control allows you to select the width of the pen. A pen is used when we draw the outlines of graphics shapes. A brush is used when we draw filled graphics shapes.

The Pen color and Brush color buttons launch ColorDialog, which lets us select a color and set the color of the button itself, which later is used by the program when creating a Pen or Brush object. Listing 4.27 shows the code for these two button click event handlers. This code also sets the background color of the respective buttons to set the current selected color of our brush and pen.

LISTING 4.27: Selecting pen and brush colors

        private void PenSetting_click(object sender, System.EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.ShowDialog();
            PenBtn.BackColor = colorDlg.Color;
        }

        private void BrushSettings_click(object sender, System.EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.ShowDialog();
            BrushBtn.BackColor = colorDlg.Color;
        }

When we draw a graphics shape, we set the color, width and transparency of the pen and brush according to the selection. The last two changes in our revised version of GDI+Painter are on the mouse-up event handler and form's paint event handler, respectively.

The modified mouse-up event handler is shown in listing 4.28.In it; we use the color buttons to create our current pen and brush from the selected colors.

LISTING 4.28: The mouse-up event handler

        private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //Set the pen's color
            curPen.Color = Color.FromArgb(Convert.ToInt16(TransCounter.Value.ToString()),
            PenBtn.BackColor.R, PenBtn.BackColor.G, PenBtn.Color.B);

            //Set the pen's width
            curPen.Width = (float)PenWidthCounter.Value;

            //Set the brush's color
            curBrush.Color = Color.FromArgb(Convert.ToInt16(TransCounter.Value.ToString()), BrushBtn.BackColor.R, BrushBtn.BackColor.G, BrushBtn.BackColor.B);
            diffX = x - curX;
            diffY = y - curY;

            switch (drawIndex)
            {
                case 1:
                    {
                        //Draw a line
                        curGraphics.DrawLine(curPen, curX, CurY, x, y);
                        break;
                    }
                case 2:
                    {
                        //Draw an ellipse
                        curGraphics.DrawEllipse(curPen, curX, curY, diffX, diffY);
                        break;
                    }
                case 3:
                    {
                        //Draw a rectangle
                        curGraphics.DrawRectangle(curPen, curX, curY, diffX, diffY);
                        break;
                    }
                case 4:
                    {
                        //Fill rectangle
                        curGraphics.FillRectangle(curBrush, curX, curY, diffX, diffY);
                        break;
                    }
                case 5:
                    {
                        //Fill ellipse
                        curGraphics.FillEllipse(curBrush, curX, curY, diffX, diffY);
                        break;
                    }
            }

            //Refresh
            RefreshFormBackground();

            //Set dragMode to false
            dragMode = false;
        }

The same properties are applied to the form's paint event handler, shown in Listing 4.29. This Code sets the Color and width properties of our pen and color property of our brush according to the current values.

LISTING 4.29: The form's paint event handler

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Set current pen's color
            curPen.Color = Color.FromArgb(Convert.ToInt16(TransCounter.Value.ToString()), PenBtn.BackColor.R, PenBtn.BackColor.G, PenBtn.BackColor.B);

            //Set pen's width
            curPen.Width = (float)PenWidthCounter.Value;

            //Set current brush's color
            curBrush.Color = Color.FromArgb(Convert.ToInt16(TransCounter.Value.ToString()), BrushBtn.BackColor.R, BrushBtn.BackColor.G, BrushBtn.BackColor.B);
            Graphics g = e.Graphics;

            //If dragMode is true , draw selected
            //graphics shape
            if (dragMode)
            {
                switch (drawIndex)
                {
                    case 1:
                        {
                            g.DrawLine(curPen, curX, curY, x, y);
                            break;
                        }
                    case 2:
                        {
                            g.DrawEllipse(curPen, curX, curY, diffX, diffY);
                            break;
                        }
                    case 3:
                        {
                            g.DrawRectangle(curPen, curX, curY, diffX, diffY);
                            break;
                        }
                    case 4:
                        {
                            g.FillRectangle(curBrush, curX, curY, diffX, diffY);
                            break;
                        }
                    case 5:
                        {
                            g.FillEllipse(curBrush, curX, curY, diffX, diffY);
                            break;
                        }
                }
            }
        }

If you run the revised GDI+Painter application, you can set the colors of the brush and the pen, the pen's width, and the transparency of both the pen and the brush. Figure 4.33 shows lines, rectangles, and ellipse drawn with different sizes and transparency.

Improvements in GDI+Painter

You can improve the functionality of the GDI+Painter application (or your own applications) even more: As we discussed in our examples, you can add a brush selection feature. You can allow users to select a brush type, style, and other properties. If user picks a gradient brush, they can select colors. You can also allow user to select cap and line styles. For solid brushes, users should be able to pick an image; and for hatch and gradient brushes, they should be able to pick styles, background, foreground, and other color properties. You can even add transformation and other options all of which we've discussed in this article.

On the basis of this example, you can write your own graphics tool library with support for many more option than the standard Windows PaintBrush application!

Figure-4.33.jpg

FIGURE 4.33: GDI+Painter in action

Conclusion

Hope the article would have helped you in understanding how to draw a line chart 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
 

i need code projec "The GDI+Painter Application" help me

Posted by xuan truong nguyen May 24, 2011

Mr.Puran Mehra


I like your idea of Painter Application, can you provide the source code of this application or you can email it to me jrasulch@gmail.com.

Thanks

Jamil
 

Posted by jamil ch Nov 17, 2010
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.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor