GDI+ Miscellaneous Advanced 2D Topics in VB.NET

In this article I will explain about miscellaneous Advanced 2D Topics in GDI+.
  • 3344

So far in this article, we have covered line caps and line styles, graphics paths, graphics containers, graphics container states, color blending and alpha blending, and the use of linear and path gradient brushes. The System.Drawing.Advance2D namespace contains topics that don't fall into any of these categories. In this section we will cover a few of these topics:

  • Region data
  • The SmoothingMode enumeration
  • The PixelOffsetMode enumeration
Region Data

Sometimes we need to get and set a region's
data or create a Region object from an array of bytes. A region's data is an array of bytes that specify the region. The RegionData class can be used to read or write the array. This class has only one property, Data, which returns an array of bytes that describe the region.

Listing 9.35 Using RegionData to read the data of a region.

LISTING 9.35: Using RegionData to read the data of a region

        ' Create a rectangle
        Dim rect As New Rectangle(20, 20, 200, 200)
        Dim rgn As New Region(rect)

        ' Create a RegionData object
        Dim rgnData As RegionData = rgn.GetRegionData()

        ' Get data
        Dim btArry As Byte() = rgnData.Data
        MessageBox.Show("Number of bytes:" + rgnData.Data.Length.ToString())

The SmoothingMode and PixelOffsetMode Enumerations

SmoothingMode and PixelOffsetMode are two enumerations defined in the Drawing.Drawing2D namespace. In this section we will take a quick look at these enumerations.

The SmoothingMode Enumeration

The Smoothing mode specifies the rendering quality of graphics drawn on a surface. The SmoothingMode property is used to get and set the smoothing mode of a graphics surface, and it takes a value of SmoothingMode enumeration.

SmoothingMode defines anti-aliasing for lines, curves, and images. This property doesn't affect text; the TextRenderingHint property is used for text. SmoothingMode has six members, which are defined in Table 9.13.

To see SmoothingMode in action, let's draw a few graphics shapes. Listing 9.36 draws a rectangle, an ellipse, and a line. The line that sets the smoothing mode of the Graphics object is commented out.

TABLE 9.13: SmoothingMode members




Anti-aliased rendering.


No anti-aliasing (the default mode).


High-quality, low-speed rendering.


High-Speed, low-quality rendering.


Invalid mode. Raises exception.


Specifies no anti-aliasing.

LISTING 9.36: Drawing with the default smoothing mode

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' Create a graphics object
        Dim g As Graphics = Me.CreateGraphics()
        ' Create three pens
        Dim redPen As New Pen(Color.Red, 6)
        Dim bluePen As New Pen(Color.Blue, 10)
        Dim blackPen As New Pen(Color.Black, 5)
        ' Set smoothing mode
        ' g.SmoothingMode = SmoothingMode.AntiAlias;
        ' Draw a rectangle, an ellipse and a line
        g.DrawRectangle(bluePen, 10, 20, 100, 50)
        g.DrawEllipse(redPen, 10, 150, 100, 50)
        g.DrawLine(blackPen, 150, 100, 250, 200)
        ' Dispose of objects
    End Sub
End Class

Figure 9.47 shows the output from Listing 9.36. The outer edges of the shapes are not smooth.


FIGURE 9.47: Drawing with SmoothingMode set to default

Now let's uncomment the SmoothingMode line in Listing 9.36 and run the program again:

g.SmoothMode = SmoothMode.AntiAlias

Figure 9.48 shows the new output. The shapes have smooth outer edges and look better overall.

The PixelOffsetMode Enumeration

PixelOffsetMode determines how pixels are offset during rendering. By offsetting pixels during rendering, we can improve rendering quality, but at the expense of speed. The PixelOffsetMode property of the Graphics class, with the help of SmoothingMode, is used to draw enhanced anti-aliasing images. The PixelOffsetMode enumeration is defined in Table 9.14.

The PixelOffsetMode property helps when we want to enhance anti-aliased graphics. Here's how to set this property:

g.SmoothingMode = SmoothingMode.AntiAlias
g.PixelOffsetMode = PixelOffsetMode.HighQuality

FIGURE 9.48: Drawing with SmoothingMode set to AntiAlias TABLE 9.14: PixelOffsetMode members




The default mode.


Pixels are offset by -0.5 units, both horizontally and vertically, for high-speed anti-aliasing.


High-quality, low-speed rendering.


High-speed, low-quality rendering.


Invalid mode.


No pixel offset


Hope the article would have helped you in understanding imiscellaneous Advanced 2D Topics in GDI+. Read other articles on GDI+ on the website.


More Articles

© 2020 DotNetHeaven. All rights reserved.