Converted to decimal, each one of the red, green, and blue numbers would produce: 27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. The alpha section is reserved for the operating system. The other three numbers are combined to produce a single value as follows:
Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available. The question that comes to mind is how we use these colors, to produce what effect. You computer monitor has a surface that resembles a series of tinny horizontal and vertical lines. The intersection of a horizontal line and a vertical line is called a pixel. This pixel holds, carries, or displays one color:
As the pixels close to each other have different colors, the effect is a wonderful distortion that creates an aesthetic picture. It is by changing the colors of pixels that you produce the effect of color variances seen on pictures and other graphics.
To make color selection easier, the Color structure is equipped with various properties that each represents a name for a color. Therefore, to use any of these colors, call the Color structure followed by the "." operator, followed by the desired color. All the popular names of colors are recognized and they are represented in the Color structure by static properties. These include Red, Green, Blue, Black, White, Yellow, Fuchsia, Silver, Gray, Brown, and Khaki, etc, just to name a few. There are many other colors that are not necessarily popular. Here is an example: private void btnBackColor_Click(object sender, EventArgs e)
{
panel1.BackColor = Color.Turquoise;
}
If none of the pre-defined colors suits you, you can define your own color as a combination of red, green, and blue values. To create a color using this approach, you can declare a variable of type Color. To specify the characters of the color, the Color structure provides the FromArgb() static method overloaded in four versions as follows: public static Color FromArgb(int argb); public static Color FromArgb(int alpha, Color baseColor); public static Color FromArgb(int red, int green, int blue); public static Color FromArgb(int alpha, int red, int green, int blue); The third version, which is the most used allows you to specify three values that each ranges from 0 to 255. Here is an example: private void btnBackColor_Click(object sender, EventArgs e)
{
panel1.BackColor = Color.FromArgb(26, 69, 174);
}
Instead of defined a color by its RGB composition, if you know the name of the color you want to use, the Color structure proposes a method named FromName that you can use. Its syntax is: public static Color FromName(string name); This method expects as argument the name of the color. Here is an example: private void btnBackColor_Click(object sender, EventArgs e)
{
panel1.BackColor = Color.FromName("LightBlue");
}
When calling this method, make sure you know the name of the color you want to use. If you provide an unregnized name, the compiler does not throw an exception but sets the values of the red, green, and blue so that the object may become transparent. Therefore, you should know the color to use but you cannot realistically know the names of all available colors. To assist you with identifying a color, the Color structure provides a method named FromKnownColor and its syntax is: public static Color FromKnownColor(KnownColor name); This method takes as argument a member of an enumeration named KnownColor. The KnownColor enumeration holds the names of common colors (such as Red, Green, Blue, Yellow, Violet, et), the colors used on web pages (such as LightBlue or DarkGreen), the colors defined in Microsoft Windows (such as ActiveBorder, AppWorkspace, or ButtonFace, etc), and many others. Here is an example of calling this method: private void btnBackColor_Click(object sender, EventArgs e)
{
panel1.BackColor = Color.FromKnownColor(KnownColor.DarkTurquoise);
}
Whether a color was initialized with one of the Color pre-defined color properties, using the FromArgb(), the FromName(), or the FromKnownColor() methods, if you want to retrieve the red, green, and blue components of a color, you can use the R, the G, or the B properties to extract the value of each. Each one of these properties is of a byte type. Alternatively, you can call the Color.ToArgb() method. Its syntax is: public int ToArgb(); This method returns an integer. We mentioned that the colors are commonly know by their names. While the ToArgb() method produces an integer that represents a color, if you instead want to get the color by its common or known name, the Color structure provides a method named ToKnownColor and whose syntax is: public KnownColor ToKnownColor(); This method returns a value that is based on the KnownColor enumeration.
To provide the selection of colors on Microsoft Windows applications, the operating system provides a common dialog box appropriate for such tasks. You can use the Color dialog box for various reasons such as letting the user set or change a color of an object or specifying the background color of a control or the color used to paint an object. When it displays, by default, the dialog box appears as follows:
This displays a constant list of colors to the user. If none of the available colors is appropriate for the task at hand, the user can click the Define Custom Colors >> button to expand the dialog box:
The expanded Color dialog box allows the user either to select one of the preset colors or to custom create a color by specifying its red, green, and blue values. The user can change the color in four different areas. The top left section displays a list of 48 predefined colors. If the desired color is not in that section, the user can click and drag the mouse in the multi-colored palette. The user can also drag the right bar that displays a range based on the color of the palette; the user can scroll up and down by dragging the arrow. For more precision, the user can type the Red, Green and Blue values. Each uses an integral value that ranges from 1 to 255. To provide the Color dialog box to your application, from the Dialogs section of the Toolbox, you can click the ColorDialog button and click anywhere on the form. The Color dialog box is implemented through the ColorDialog class, which is based on the CommonDialog class that is the ancestor to all Windows common dialog boxes of the .NET Framework. To display the dialog box to the user, call the CommonDialog.ShowDialog() method. Here is an example: private void btnBackColor_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.ShowDialog();
}
The most important and most obvious property of the Color dialog box is the color the user would have selected after using it. This selected color is represented by the ColorDialog.Color property. When you are setting up a ColorDialog control for your application, if you want to specify the default color, in the Properties windows, you can click the arrow of the Color property. This would give you the option to select a color from three available tabs:
At run time, you can set the color programmatically by assigning it a valid known name of a color: private void btnBackColor_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Color.Red;
dlg.ShowDialog();
}
When the user has finished using the Color dialog box and clicks OK, you can find out what color was selected by retrieving the value of the ColorDialog.Color property. Here is an example: private void btnBackColor_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Color.FromKnownColor(KnownColor.DarkTurquoise);
if( dlg.ShowDialog() == DialogResult.OK )
panel1.BackColor = dlg.Color;
}
By default, the Color dialog box comes up in its regular (small) size. This allows the user to select one of the preset colors. If the desired color is not available, as mentioned already, the user can click the Define Custom Colors >> button. If you want to control the user's ability to expand the dialog box, use the Boolean AllowFullOpen property. When this property is set to True, which is its default value, the dialog box appears in its regular size but with the Define Custom Colors >> button enabled. If you want the user to be able to select only one of the preset colors and not have the ability to expand the dialog box, set the AllowFullOpen property to False. With this value, when the Color dialog box comes up, it is in its regular size but the Define Custom Colors >> button is disabled:
As mentioned already, by default, the Color dialog box displays in its regular size. You can control the regular or full size of the dialog using the Boolean FullOpen property. When its value is False, which is the default, the dialog appears regularly. If you want it to appear in its full size, set this property to True.
In our introduction to GDI+, we saw that the screen of a computer monitor uses a series of horizontal and vertical lines, the intersection of two perpendicular lines is a pixel. Each pixel holds one color. Of course, two adjacent pixels can hold the same color or each can hold a different color. A bitmap is a series of colored adjacent pixels. Put another way, for a group of pixels to be considered a bitmap, these pixels must constitute a group. A bitmap is made by specifying the color of each pixel. This means that the pictures we have used so far were simply made of pixels and each pixel held an appropriate color. If you decide to create or design a picture using the tool resources in Microsoft Visual C++ available from the Resource View (or the Solution Explorer), you would experiment, on a large scale the ability to specify the color of each individual pixel, using (a limited list of) colors:
Actually, when you have a bitmap, you can access any pixel of the picture and then you can either specify its color or get its current color. To allow you to specify the color of a pixel, the Bitmap class provides a method named SetPixel and its syntax is: public void SetPixel(int x, int y, Color color); The x and y arguments represent the left and top values of the location of the pixel. The 3rd argument specifies the new color that the pixel will hold. Here is an example: private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bgDrawingArea = new Bitmap(Width, Height);
e.Graphics.DrawImage(bgDrawingArea, 0, 0);
for (int i = 0; i < Width; i += 20)
for (int j = 0; j < Height; j += 20)
{
bgDrawingArea.SetPixel(i, j, Color.White);
Graphics painter = Graphics.FromHwnd(Handle);
painter.DrawImage(bgDrawingArea, 0, 0);
}
}
We previously mentioned that a picture was a series of pixels with each pixel holding a color. As opposed to specifying the color of a pixel, you can retrieve its color. To support this, the Bitmap class is equipped with a method named GetPixel. Its syntax is: public Color GetPixel(int x, int y); The x and y arguments represent the left and top values of the location of the pixel whose color you want to get. This method returns the color of that pixel.
In some application that deals with graphic, you may use only one picture but in various, if not most other applications, such as those used to display, exchange, or switch pictures, you may deal with different pictures. In such a type of application, sometimes the pictures are of different sizes, even including unpredictable sizes. In another type of application, some of the pictures you use may have the (exact) same size. For this type, instead of using each picture individually, you can store them in a collection, then access each picture when needed. An image list is a collection of icons or pictures that are stored so that each icon or picture can be located by an index. The icons or pictures must be of the same size and they should be the same type. This means that the collection can be made of only icons of 16x16 sizes, only icons of 32x32 sizes, only icons of 48x48 sizes, or only bitmaps (but of the same size each). When it comes to design, there are two types of image lists. Each picture can be added individually to the collection, or all of the pictures can be designed in a longer picture and, inside of that long picture, you would use a technique to locate each picture. Once you get an image list, there is no pre-defined way you must use it. Some Windows controls use an image list to use the pictures they need but there are various other unpredictable ways you can use an image list.
To support image lists, the .NET Framework provides a class called ImageList that is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll library. At design time, to create an image list, from the Components section of the Toolbox, you can click the ImageList button and click the form. To programmatically create an image list, declare a variable of type ImageList and initialize it using one of its two constructors. The default constructor is used to simply signal that you are going to use an image list. Here is an example: using System;
using System.Windows.Forms;
public class Exercise : Form
{
private ImageList lstImages;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
lstImages = new ImageList();
}
}
public class Program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
The ImageList class is equipped with another constructor whose syntax is: public ImageList(IContainer container); This constructor expects as argument the control container that will be responsible for getting rid of the image list when the application exits.
Before creating an image list, you must prepare the image(s). As mentioned previously, you can use individual images that would be made into a set. When creating the images, your primary concern is the size you give them. The default size of a picture is 16x16. If you are creating a list of icons, you can create each with the default size of 16x16. In some cases (for example if you intend to use the images for a list view), you can create or design a second set of icons whose size are 32x32 (and/or 48x48) each. You can use a larger size:
To create or design your bitmaps, you can use either the bitmap designer of Microsoft Visual Studio or you can use a more advanced external application to prepare the pictures. Although the pictures used in an image list are usually square (16x16, 32x32, 48x48, 96x96, etc), you can use a different length and a different height but, if you are creating individual pictures, they should each have the same size (same length and same height). Instead of using different pictures, you can create one long picture whose sections would be used to identify each picture. There is no particular way you must design the long picture and there is no real rule about its dimensions: the size would become an issue when it is time to use the picture(s). Alternatively, when creating the picture, if it will include the different pictures that would be considered in the list, make sure you know where each sectional picture starts and where it ends. Here is an example:
In this cases, there is one long picture made of different sections and each section holds a particular picture. When creating this type, you must know the size that holds each particular picture. After preparing the pictures, you can get ready to create the image list. If you want, you can specify the size of each picture before actually creating the list. To support this, the ImageList class is equipped with a property named ImageSize. The ImageSize property is of type Size. You can use it to specify the size of each picture. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private ImageList lstImages;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
lstImages = new ImageList();
lstImages.ImageSize = new Size(256, 256);
}
}
If the image list has been created already, to find out the size of the picture, you can get the value of the ImageSize property.
Before creating the list of images, you must specify the number of bits that will be used to specify the color of each pixel. This information is referred to as the color depth. To support it, the ImageList class is equipped with a property named ColorDepth. The possible values are Depth4Bit, Depth8Bit, Depth16Bit, Depth24Bit, and Depth32Bit. The default value is Depth8Bit.
After preparing the pictures, to visually create the list of images, under the form, click the image list control to select it. Then:
This would open the Images Collection Editor. To add a picture, you can click the Add button, locate a picture and select it. You can continue doing this until you have selected all necessary pictures. Here is an example:
After selecting the pictures, you can click OK. To assist you with creating the list of images, the ImageList class is equipped with a property named Images, which is a collection. The ImageList.Images property is of type ImageCollection. The ImageCollection class implements the IList, the ICollection, and the IEnumerable interfaces. Using the ImageCollection class through the Images property, you can add the necessary icons or pictures one at a time. To help you with this, the ImageCollection class implements the Add() method. If you are creating a list of icons, you can call the following version of the Add() method: public void Add(Icon value); If you are creating a list of pictures, you can call the following version of the Add() method: public void Add(Image value); Here is an example: public class Exercise : Form
{
private ImageList lstImages;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
lstImages = new ImageList();
lstImages.ImageSize = new Size(256, 256);
Image img1 = Image.FromFile("E:\\Programs\\image1.jpg");
lstImages.Images.Add(img1);
Image img2 = Image.FromFile("E:\\Programs\\Image2.jpg");
lstImages.Images.Add(img2);
}
}
Instead of adding one picture at a time, you can first store them in an array. To add an array of pictures, the ImageCollection class provides a method named AddRange and whose syntax is: public void AddRange(Image[] images); Here is an example: void InitializeComponent()
{
lstImages = new ImageList();
lstImages.ImageSize = new Size(256, 256);
Image img1 = Image.FromFile(@"E:\Programs\image1.jpg");
lstImages.Images.Add(img1);
Image img2 = Image.FromFile(@"E:\Programs\Image2.jpg");
lstImages.Images.Add(img2);
Image[] images =
{
Image.FromFile(@"E:\Programs\image3.jpg"),
Image.FromFile(@"E:\Programs\Image4.jpg"),
Image.FromFile(@"E:\Programs\Image5.jpg")
};
lstImages.Images.AddRange(images);
}
After creating an image list, you can use it in your application. As it name implies, an image list is just a collection of images. The control that holds this collection does not define why or when the images would be used. Some Windows controls, such as the list view, the tab control or the toolbar, use them. Most of the time, you will know when a certain control needs an image list and how to use it. Otherwise, you can use the pictures in an image list in various ways. For example, you can use those pictures like any others, such as to display one on a form. To support the ability to display the pictures of an image list, the ImageList class is equipped with a method named Draw. When calling this method, because the image list doesn't know where it would be used, and because the target control would not define where the picture is coming from, the primary argument to this method is the platform on which to display the picture. This platform is a Graphics object. Once you have decided on the receiving graphics, you must specified the location where the picture would be positioned. You have two primary options. You can specify the location by the left and top coordinates. In this case, you would use the following version of the Draw() method: public void Draw(Graphics g, int x, int y, int index); Alternatively, you can specify the location by a Point value. In this case, you would use the following version of the method: public void Draw(Graphics g, Point pt, int index); In both cases, the last argument is the index of the desired picture within the collection.
When drawing the pictures of an image list, you can designate one color that the compiler will ignore or "see through". This is referred to as the transparent color. To support this, the ImageList class is equipped with a property named TransparentColor, which is of type Color.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||