Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2004-11-12
Medium Priority
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

AlexFM earned 2000 total points
ID: 12569207
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

ID: 12579481
Thanks that is excellent!

I appreciate the completeness of the answer.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

564 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