Capture screen image in monochrome using BitBlt

Hi,

I am using BitBlt to capture parts of my screen but I then (thanks to AlexFM's help) have to convert to Monochrome (see http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21824554.html#16524842)

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.

James.
JAMESAsked:
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.

AlexFMCommented:
Please show your capture code.
0
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
                  gfxBitmap.ReleaseHdc(hdcBitmap);                              
                  Win32.ReleaseDC(handle, hdcWindow);

                  // dispose of the bitmap's graphics object
                  gfxBitmap.Dispose();            

                  // return the bitmap of the window
                  return bitmap;                  
            }


Thanks.


0
AlexFMCommented:
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.
0

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.

Cheers.

James.
0
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
C#

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.