This article has been excerpted from book "Graphics Programming with GDI+".
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
Rectangle rect = new Rectangle(20, 20, 200, 200);
Region rgn = new Region(rect);
// Create a RegionData object
RegionData rgnData = rgn.GetRegionData();
// Get data
byte[] btArry = 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
Member |
Description |
AntiAlias |
Anti-aliased rendering. |
Default |
No anti-aliasing (the default mode). |
HighQuality |
High-quality, low-speed rendering. |
HighSpeed |
High-Speed, low-quality rendering. |
Invalid |
Invalid mode. Raises exception. |
None |
Specifies no anti-aliasing. |
LISTING 9.36: Drawing with the default smoothing mode
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Miscellaneous_Advanced_2D_Topics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Create a graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
// Create three pens
Pen redPen = new Pen(Color.Red, 6);
Pen bluePen = new Pen(Color.Blue, 10);
Pen blackPen = 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
redPen.Dispose();
bluePen.Dispose();
blackPen.Dispose();
g.Dispose();
}
}
}
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
Member |
Description |
Default |
The default mode. |
Half |
Pixels are offset by -0.5 units, both horizontally and vertically, for high-speed anti-aliasing. |
HighQuality |
High-quality, low-speed rendering. |
HighSpeed |
High-speed, low-quality rendering. |
Invalid |
Invalid mode. |
None |
No pixel offset |
Conclusion
Hope the article would have helped you in understanding imiscellaneous Advanced 2D Topics in GDI+. Read other articles on GDI+ on the website.
|
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. |