# Screen capture desktop

**URL:** <https://forum.juce.com/t/screen-capture-desktop/15319>\
**Category:** General JUCE discussion\
**Created:** [August 13, 2015, 10:46pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319 "2015-08-13T22:46:49Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![elli](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@elli](https://forum.juce.com/u/elli)\
**Post date:** [August 14, 2015, 9:26am UTC](https://forum.juce.com/t/screen-capture-desktop/15319/3 "2015-08-14T09:26:16Z")

</div>

Hello,

I have successfully captured some parts of the screen and converted to JPEG in Windows.

Here some code snippets from my project.&nbsp; Maybe not the best solution, but it works...

Anybody here to have some hints for OS-X ?.&nbsp; OS-X is a strange world for me.

elli

&nbsp;

&nbsp;

```
int mWidth = Desktop::getInstance().getDisplays().getMainDisplay().totalArea.getWidth();    
   int mHeight = Desktop::getInstance().getDisplays().getMainDisplay().totalArea.getHeight();

   HDC hDesktopDC = GetDC(0); // Desktop DC
   HDC mCaptureDC = CreateCompatibleDC(hDesktopDC);
   HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, width, height);   
   HBITMAP hBitmap = (HBITMAP) SelectObject(mCaptureDC, hCaptureBitmap);

   //
    // The BitBlt function performs a bit-block transfer of the color data corresponding
    // to a rectangle of pixels from the specified source device context into a
    // destination device context.
    //  

    BitBlt( mCaptureDC, // A handle to the destination device context.
            0, 0, // The x,y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
            width, height, // The width,height in logical units, of the source and destination rectangles.
            hDesktopDC, // A handle to the source device context.
            x, y, // The x,y-coordinate, in logical units, of the upper-left corner of the source rectangle.
            SRCCOPY | CAPTUREBLT ); // A raster-operation code. These codes define how the color data for the source rectangle
                                                       // is to be combined with the color data for the destination rectangle to achieve the final color.

  BITMAP bitma p;                                           
  GetObject(hBitmap,sizeof(BITMAP),&bitmap);

  BITMAPINFOHEADER bi;              
  ZeroMemory ((uint8*)&bi, sizeof(BITMAPINFOHEADER));

  bi.biSize = sizeof(BITMAPINFOHEADER);   
  bi.biWidth = mWidth;
  bi.biHeight = mHeight;
  bi.biPlanes = 1;
  bi.biBitCount = DAW_RAW_BIT_COUNT;  
  bi.biCompression = BI_RGB;

 uint8 buffer[MAX] ;                                      
                                      
  // The GetDIBits function retrieves the bits of the specified compatible bitmap and copies
  // them into a buffer as a DIB using the specified format.

  GetDIBits(hDestinationDC, // A handle to the device context
            hBitmap, // handle to the bitmap. This must be a compatible bitmap (DDB).
            0, // The first scan line to retrieve  
            mHeight, // The number of scan lines to retrieve   
            buffer, // A pointer to a buffer to receive the bitmap data
            (BITMAPINFO *)&bi, // A pointer to a BITMAPINFO structure that specifies the desired format for the DIB data.
            DIB_RGB_COLORS); // he color table should consist of literal red, green, blue (RGB) value                             
                                      

 createJpeg( buffer, bi.biSizeImage);
 

 
 
 void createJpeg(uint8 * pBuffer, uint32 dwLength)
 {
   ScopedPointer<JPEGImageFormat> jpeg = new JPEGImageFormat();
   if ( jpeg )
   {
           
     Image image = Image (Image::RGB, mWidth,mHeight, true);
     Image::BitmapData imageData (image, Image::BitmapData::writeOnly);

     // Remember th BMP format for 24 Bit: |B|G|R| |B|G|R| ....               

         
     uint8 * p = pBuffer;
     for (int y = mHeight; --y >= 0;)
     {
       memcpy(imageData.getLinePointer(y), p, imageData.lineStride );
       p += imageData.lineStride ;
     }
     
     MemoryOutputStream out(dwLength);      
     
     // jpeg->setQuality( ??? );

      jpeg->writeImageToStream(image,out) )

   }     
 }
```

&nbsp;

&nbsp;

---

_[View the full topic](https://forum.juce.com/t/screen-capture-desktop/15319)._
