Alpha Blending 2-D Graphics Programming with GDI+

172 Dim br As New SolidBrushSystemColors.InfoText

4.6.5 Alpha Blending

Alpha blending is a process that allows a Graphics object to appear transparent, causing Graphics objects beneath it to be seen through the object. The degree of transparency can be controlled in steps from completely transparent invisible to completely opaque obscuring any objects beneath it. To draw a transparent object, instantiate Pen and Brush objects having colors whose alpha component is less than the maximum value of 255. A colors alpha component is given by the A property of the Color structure. This property is a Byte, so it can take values from 0 invisible to 255 completely opaque. Ex am ple 4- 12 shows an OnPaint method that draws text and then draws two overlapping, transparent ellipses in the same space as the text. Normally, the ellipses would obscure the text, and the second ellipse would obscure the first. In this case, however, the text can be seen through the ellipses because the ellipses are transparent, and the first ellipse can be seen through the second. The result is shown in Figur e 4- 13 . Example 4-12. Drawing transparent figures Protected Overrides Sub OnPaintByVal e As PaintEventArgs Determine the text to display and its font. Dim str As String = Here is some text to display in a form. Dim fnt As New FontArial, 10, FontStyle.Regular, GraphicsUnit.Point Determine the X and Y coordinates at which to draw the text so that it is centered in the window. Dim szf As SizeF = e.Graphics.MeasureStringstr, fnt Dim xText As Single = Me.DisplayRectangle.Width - szf.Width 2 Dim yText As Single = Me.DisplayRectangle.Height - szf.Height 2 Draw the text. e.Graphics.DrawStringstr, fnt, Brushes.Black, xText, yText Create a blue brush that is mostly transparent. Dim br As New SolidBrushColor.FromArgb160, Color.Blue Determine the bounding rectangle for the first ellipse. Dim rect As Rectangle rect.X = Me.DisplayRectangle.X + Me.DisplayRectangle.Width \ 8 rect.Y = Me.DisplayRectangle.Y + Me.DisplayRectangle.Height \ 8 rect.Width = Me.DisplayRectangle.Width \ 2 rect.Height = Me.DisplayRectangle.Height \ 2 Draw the first ellipse. e.Graphics.FillEllipsebr, rect Release the brush. br.Dispose Create a red brush that is mostly transparent. br = New SolidBrushColor.FromArgb160, Color.Red Determine the bounding rectangle for the second ellipse. rect.X += Me.DisplayRectangle.Width \ 4 rect.Y += Me.DisplayRectangle.Height \ 4 Draw the second ellipse. e.Graphics.FillEllipsebr, rect 173 Release the brush. br.Dispose End Sub Figure 4-13. The display drawn by the code in Ex a m ple 4 - 1 2

4.6.6 Antialiasing