Link to home
Start Free TrialLog in
Avatar of JAMES
JAMES

asked on

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 https://www.experts-exchange.com/questions/21824554/Convert-bitmap-from-24-bit-to-monochrome.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.
Avatar of AlexFM
AlexFM

Please show your capture code.
Avatar of JAMES

ASKER

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.


ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAMES

ASKER

Again, Alex, many thanks for your help.

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

Cheers.

James.