Capture screen image in monochrome using BitBlt


I am using BitBlt to capture parts of my screen but I then (thanks to AlexFM's help) have to convert to Monochrome (see

I wondered if there was a way to directly copy an area of screen directly into a monochrome bitmap to save the conversion processing time and to simplify my process.

Many thanks.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please show your capture code.
JAMESAuthor Commented:
This is the code :-

            public Bitmap GetWindowCaptureAsBitmap(IntPtr handle, int x, int y, int width, int height)
                  Win32.Rect rc = new Win32.Rect();
                  if (!Win32.GetWindowRect(handle, ref rc))
                        return null;
                  // create a bitmap from the visible clipping bounds of the graphics object from the window
                  // Bitmap bitmap = new Bitmap(rc.Width, rc.Height);
                  Bitmap bitmap = new Bitmap(width, height);
                  // create a graphics object from the bitmap
                  Graphics gfxBitmap = Graphics.FromImage(bitmap);
                  // get a device context for the bitmap
                  IntPtr hdcBitmap = gfxBitmap.GetHdc();

                  // get a device context for the window
                  IntPtr hdcWindow = Win32.GetWindowDC(handle);
                  // bitblt the window to the bitmap
                  Win32.BitBlt(hdcBitmap, 0, 0, width, height, hdcWindow, x, y, (int)Win32.TernaryRasterOperations.SRCCOPY);
                  // release the bitmap's device context
                  Win32.ReleaseDC(handle, hdcWindow);

                  // dispose of the bitmap's graphics object

                  // return the bitmap of the window
                  return bitmap;                  


No, I don't see a way to capture directly to monochrome bitmap. The line:
Bitmap bitmap = new Bitmap(width, height);
creates color bitmap. If you change this to monochrome (providing PixelFormat parameter), Graphics.FromImage(bitmap) will not work.

In your previous question I posted you link to article which decrribes how to access bitnap bits directly using unsafe pointers. You can use two unsafe pointers - one for reading RGB24 bitmap, another to write monochrome bitmap, and convert every RGB pixel to monochrome pixel. This can improve performance, because you save copying operation. Formula for transformation is in my previous post.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JAMESAuthor Commented:
Again, Alex, many thanks for your help.

I will capture in colour and use your previous routine to convert.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.