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 Example   Follow me on Twitter   RSS feed




This entry was posted in graphics, image processing and tagged , , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Use the Bitmap32 class to manipulate image pixels very quickly in C#

  1. Pingback: Write a graphical floodfill method in C# -

  2. charles says:

    Little typo in the comment…

    // Make a Bitmap24 object.
    Bitmap32 bm32 = new Bitmap32(bm);

  3. pinco101 says:

    For two years I tried to do a flood fill function working !!!
    I could not because I obtained solutions are too slow or not interfaced with the file type * .bmp
    Bravissimo and thanks!
    Finally I can complete my personal game in c #.

    But where did you find the information to create the library “Bitmap32.cs” ???

Leave a Reply

Your email address will not be published. Required fields are marked *