getting access to actual pixels in hbitmap object returned by GetHBitmap() using c#

Posted on 2004-11-12
Last Modified: 2012-06-21
I am programming in c#.
I need to change the alpha value of every pixel in a Bitmap.

I did this:

for(row = 1; row < myBitmap.Size.Height; row++)
     for(col = 1; col < myBitmap.Size.Width; col++)
      Color aColor = myBitmap.GetPixel(col, row);
      aColor = Color.FromArgb(alpha, aColor);
      myBitmap.SetPixel(col, row, aColor);

which works but it is too slow.  i want to access the actual pixels via the bmBits pointer in the HBitmap returned from myBitmap.GetHbitmap() but I do not know how to do this.  Can you show me how to at least get a bmBits pointer that I can increment over the range of the bitmap?  I can figure out the rest.
Question by:loring_vogel
    LVL 48

    Accepted Solution

    You can access bitmap bits directly using Bitmap.LockBits functopn and unsafe pointers.

    For example, this function inverts all pixels in the bitmap:

    public static bool Invert(Bitmap b)
        // GDI+ still lies to us - the return format is BGR, NOT RGB.
        BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        int stride = bmData.Stride;
        System.IntPtr Scan0 = bmData.Scan0;
            byte * p = (byte *)(void *)Scan0;
            int nOffset = stride - b.Width*3;
            int nWidth = b.Width * 3;
            for(int y=0;y < b.Height;++y)
                for(int x=0; x < nWidth; ++x )
                    p[0] = (byte)(255-p[0]);
                p += nOffset;


        return true;

    See details in the article.

    Author Comment

    Thanks that is excellent!

    I appreciate the completeness of the answer.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now