[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use the Bitmap32 class to manipulate image pixels very quickly in C#

manipulate image pixels

The following posts explain how you can build a Bitmap24 class to manipulate the pixels in 24-bit images very quickly.

This example uses similar techniques to make a Bitmap32 class that lets you manipulate 32-bit images. In a 32-bit image, each pixel is represented by four bytes, one each for red, green, blue, and alpha (opacity) components.

The Bitmap32 class is similar to the Bitmap24 class described in the other posts, so I won't show its code here. Download the example to see the details.

When you click this example's Quarter button, the program executes the following code.

private void btnQuarter_Click(object sender, EventArgs e) { Bitmap bm = new Bitmap(picHidden.Image); Cursor = Cursors.WaitCursor; Stopwatch watch = new Stopwatch(); watch.Start(); // Make a Bitmap24 object. Bitmap32 bm32 = new Bitmap32(bm); // Lock the bitmap. bm32.LockBitmap(); // Invert the pixels. int xmid = bm32.Width / 2; int ymid = bm32.Height / 2; for (int y = 0; y < ymid; y++) { for (int x = 0; x < xmid; x++) { bm32.SetGreen(x, y, 0); bm32.SetBlue(x, y, 0); bm32.SetAlpha(x, y, 220); } } for (int y = ymid; y < bm32.Height; y++) { for (int x = 0; x < xmid; x++) { bm32.SetRed(x, y, 0); bm32.SetGreen(x, y, 0); bm32.SetAlpha(x, y, 220); } } for (int y = 0; y < ymid; y++) { for (int x = xmid; x < bm32.Width; x++) { bm32.SetRed(x, y, 0); bm32.SetBlue(x, y, 0); bm32.SetAlpha(x, y, 220); } } byte red, green, blue, alpha; for (int y = ymid; y < bm32.Height; y++) { for (int x = xmid; x < bm32.Width; x++) { red = (byte)(255 - bm32.GetRed(x, y)); green = (byte)(255 - bm32.GetGreen(x, y)); blue = (byte)(255 - bm32.GetBlue(x, y)); alpha = 220; bm32.SetPixel(x, y, red, green, blue, alpha); } } // Unlock the bitmap. bm32.UnlockBitmap(); // Make a new bitmap. Bitmap final_bm = new Bitmap(picHidden.Image); using (Graphics gr = Graphics.FromImage(final_bm)) { // Draw an ellispe in the center. gr.Clear(Color.Black); gr.FillEllipse(Brushes.White, new Rectangle((int)(bm.Width * 0.2), (int)(bm.Height * 0.2), (int)(bm.Width * 0.6), (int)(bm.Height * 0.6))); // Copy the quartered bitmap onto this one. gr.DrawImage(bm, 0, 0); } // Display the result. picVisible.Image = final_bm; watch.Stop(); Cursor = Cursors.Default; lblElapsed.Text = watch.Elapsed.TotalSeconds.ToString("0.000000") + " seconds"; }

This code uses the Bitmap32 class to process the image's four quarters much as the previous example did, except this example sets each pixel's alpha component to 220 so each pixel is translucent. The program then creates a new image, draws a white ellipse in the center, and draws the translucent image on top. If you look at the picture at the beginning of this post, you'll see the white ellipse showing through.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.