- Loading Bitmaps
- Rotating and Flipping
- Changing the Palette
- Graphics Made Easier
Changing the Palette
You might have noticed that the Image object appears a lot in this article and the Bitmap object is seemingly missing in action. The Bitmap object has a considerable number of uses as well. One of the more interesting uses is making a particular color transparent. The user can see through this color to the image below. The technique is used with icons, but you can also use it with other bitmaps to achieve special effects, such as lettering or images that appear to float above a dialog box. Here's a technique for making a portion of a bitmap transparent:
private void btnTransparent_Click(object sender, System.EventArgs e) { // Load the image. Bitmap Temp = new Bitmap("TestImage.gif"); // Make the background transparent. Temp.MakeTransparent(Color.Cyan); // Display the image onscreen. pbBitmap.Image = Temp; MessageBox.Show("Background is transparent"); // Reload the image and make a different color transparent. Temp = new Bitmap("TestImage.gif"); Temp.MakeTransparent(Color.FromArgb(255, 255, 0)); // Display the image onscreen. pbBitmap.Image = Temp; MessageBox.Show("Yellow is transparent"); }
You can use several techniques to define the transparent color. The only requirement is that the color actually exist in the image. The example shows two techniques. You can either choose a color constant such as Color.Cyan or define the color using the Color.FromArgb() method.
It's important to note that more than one color in a bitmap can be marked transparent. The only way to get rid of the transparency is to reload the image, as shown in the code. If the code had made yellow transparent without reloading the image, both the background and the yellow elements would be transparent.
Bitmaps also come in handy for color manipulation. For example, you might want to create a negative image from a positive one (or vice versa). The following example shows you how:
private void btnChangeColor_Click(object sender, System.EventArgs e) { Color CurrColor; // Current color. Color NewColor; // Modified color. // Load the image. pbBitmap.Image = Image.FromFile("TestImage.gif"); MessageBox.Show("Original Image"); // Get the bitmap from the image. Bitmap Temp = new Bitmap(pbBitmap.Image); // Process all the rows. for (Int32 Row = 0; Row < Temp.Height; Row++) // Process all the columns. for (Int32 Col = 0; Col < Temp.Width; Col++) { // Get the color of the current pixel. CurrColor = Temp.GetPixel(Col, Row); // Reverse the color to create a negative. NewColor = Color.FromArgb(255 - CurrColor.R, 255 - CurrColor.G, 255 - CurrColor.B); // Apply the new color to the pixel. Temp.SetPixel(Col, Row, NewColor); } // Display the image on screen. pbBitmap.Image = Temp; }
It's important to begin the process by loading the graphic into an Image object of some type so that the individual bits are available. Otherwise, you might receive an error message stating that the SetPixel() method doesn't work with indexed colors. In this case, the code uses the exercise to show the original image.
The .NET Framework makes it easy to reference a particular pixel in an image by viewing it as an array of pixel values. The values are arranged as rows and columns. To process the entire image, simply use loops to access each pixel in the array.
The GetPixel() method obtains the current color value of a pixel. To reverse this value, creating a negative of it, simply subtract the value from 255.
Once the color is converted, you can use the SetPixel() method to replace the current color. Any Color object will do, so you don't have to stick with just RGB colors, as shown in the example. You can also add Alpha effects to the image.