# 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:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordan\_Harris](https://forum.juce.com/u/Jordan_Harris)\
**Post date:** [August 13, 2015, 10:46pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/1 "2015-08-13T22:46:49Z")

</div>

Is there any way to get a screenshot of the desktop and create&nbsp;an Image from it? I've been looking around trying to see how it's done with the Windows API and there are a lot of examples of how to do it in Windows, but I don't know anything about the Windows API and it might be hard figuring out how to create a JUCE Image from it. I haven't looked into the other OSs yet, but you have to start somewhere I guess. I'm hoping there's some mechanism in JUCE that can accomplish this though. It would be even cooler to also have the ability to record the screen, but that's probably much harder.&nbsp;

---

<div class="post-metadata">

**Author:** ![Jordan\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordan\_Harris](https://forum.juce.com/u/Jordan_Harris)\
**Post date:** [August 14, 2015, 5:38am UTC](https://forum.juce.com/t/screen-capture-desktop/15319/2 "2015-08-14T05:38:07Z")

</div>

So on Windows, I can take a screenshot just fine using HBITMAP. I've tested it by copying it to the system clipboard. The problem now is that I can't convert that to a JUCE Image object. Any ideas on how to do this? I've tried using the function&nbsp;  
createImageFromHBITMAP() from juce\_win32\_Windowing.cpp, but all I get is a white image.&nbsp;

---

<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;

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [August 14, 2015, 7:39am UTC](https://forum.juce.com/t/screen-capture-desktop/15319/4 "2015-08-14T07:39:20Z")

</div>

bro, use the Code style tag when you post that stuff lol

---

<div class="post-metadata">

**Author:** ![Jordan\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordan\_Harris](https://forum.juce.com/u/Jordan_Harris)\
**Post date:** [August 14, 2015, 5:59pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/5 "2015-08-14T17:59:00Z")

</div>

Actually, the&nbsp;**createImageFromHBITMAP()** function from _juce\_win32\_Windowing.cpp_ does actually work! I just had to make sure that I deleted and released the DC before I actualy called&nbsp;createImageFromHBITMAP(). Maybe because it creates it's own our something. I should try just passing it to that function and handle cleanup from there.

Thanks for your reply elli! I really appreciate you posting your code. I'm going to have to try your way and see which one performs better.

---

<div class="post-metadata">

**Author:** ![Jordan\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordan\_Harris](https://forum.juce.com/u/Jordan_Harris)\
**Post date:** [August 14, 2015, 6:44pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/6 "2015-08-14T18:44:18Z")

</div>

Does anyone have any idea about what I could use in Linux for doing this? On Windows, I can just use the Windows API. I'm not sure what to use for Linux though. I don't want add any extra dependencies that JUCE doesn't already use.&nbsp;

I'd really appreciate any help from you guys. If I can figure out how to do it on all of the desktop platforms, I'll be sure to share it with the community so anyone else in the future can use it if they need. I think this would be a good feature to add to JUCE too. It seems like most of the other popular graphics frameworks/libraries have a facility for taking screenshots.&nbsp;

---

<div class="post-metadata">

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

</div>

> It seems like most of the other popular graphics frameworks/libraries have a facility for taking screenshots.&nbsp;

Do they? Sounds to me like quite a rare thing for people to want to do in their app! (And surely modern OSes have security restrictions that would stop apps from snapshotting other windows?)

But just to be clear, are you really trying to take an image of the OS desktop, or just your own app's&nbsp;window? Because if it's your own window then the Component class has methods to capture it to an Image.

---

<div class="post-metadata">

**Author:** ![Jordan\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordan\_Harris](https://forum.juce.com/u/Jordan_Harris)\
**Post date:** [August 15, 2015, 5:55pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/8 "2015-08-15T17:55:29Z")

</div>

Yeah, I'm sure it is a rare thing to want. Surprisingly, it seems that they do. The ones I remember seeing mentioned were libraries like Qt, SDL, Cinder, etc.&nbsp;

Really, it's not a big deal if I figure this out or not. I'm just trying to experiment and make myself a "snipping tool" that lets you take an image of part of the screen. Yeah I know you can capture a component as an image (which is awesome by the way). I just realized that when I was looking into this.&nbsp;

---

<div class="post-metadata">

**Author:** ![hardyVeles](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hardyveles/32/7317_2.png) [@hardyVeles](https://forum.juce.com/u/hardyVeles)\
**Post date:** [October 17, 2019, 3:37pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/9 "2019-10-17T15:37:00Z")

</div>

I’m working in the same direction (purpose: streamlining user manual content creation for my VSTi). GUI libraries often have such capability not because its popularity but because, on the lowest level, rendering a component to a image memory container (later encoded to an image format, e.g. JPEG) or to screen - is ~same thing. I’ll dig around juce::LowLevelGraphicsContext, juce::Drawable … ang generally in juce::gui\_basics & juce::graphics namespaces. It shouldn’t be that hard to make a screenshot utility class. I’ll post here any progress.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [October 17, 2019, 3:57pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/10 "2019-10-17T15:57:12Z")

</div>

While the API wasn’t mentioned directly, are you aware of:

```auto
 Image Component::createComponentSnapshot(Rectangle< int > areaToGrab, bool clipImageToComponentBounds=true, float scaleFactor=1.0f)

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [October 17, 2019, 5:50pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/11 "2019-10-17T17:50:15Z")

</div>

OS X and Windows both have a really easy way to take screenshots of app windows. Why can’t you just use that? on OS X it’s `Cmd-Shift-3` to do the whole desktop, and `CMD-shift-4 + Spacebar` to take a screenshot of whatever app window is under the cursor. you can also do `Cmd-Shift-4` and then click-drag to take a screenshot of an area on screen.

---

<div class="post-metadata">

**Author:** ![hardyVeles](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hardyveles/32/7317_2.png) [@hardyVeles](https://forum.juce.com/u/hardyVeles)\
**Post date:** [October 22, 2019, 4:18pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/12 "2019-10-22T16:18:12Z")

</div>

Your reply is real answer to the question.  
Something like this would be implementation:

```
	auto editor = synthProcessor->getActiveEditor();
	auto editorRectangle = editor->getBounds();
	auto image = editor->createComponentSnapshot(editorRectangle, true);
	
	String filePath = File::getCurrentWorkingDirectory().getFullPathName();
	File outFile(filePath + "temp.jpg");

	JPEGImageFormat jpegFormat = JPEGImageFormat();
	juce::FileOutputStream * s = outFile.createOutputStream(500000);

	jpegFormat.writeImageToStream(image, *s);
	s->flush();

```

Edit: just tested - WORKS 🙂  
Edit 2: implemented @Achder’s memory leak warrning. Read his reply below for other important comments.

---

<div class="post-metadata">

**Author:** ![Achder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/achder/32/5897_2.png) [@Achder](https://forum.juce.com/u/Achder)\
**Post date:** [October 22, 2019, 4:56pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/13 "2019-10-22T16:56:09Z")

</div>

> [@hardyVeles](#):
>
> auto outFile = new File(filePath + “temp.jpg”);

That’s a memory leak right there.  
Just use:

```auto
File outFile(filePath + “temp.jpg”);

```

And with regards to this line:

> [@hardyVeles](#):
>
> juce::FileOutputStream \* s = outFile-\>createOutputStream(500000);

It says in the documentation:

> Note that this is an old method, and actually it’s usually best to avoid it and instead use an RAII pattern with an [FileOutputStream](https://docs.juce.com/master/classFileOutputStream.html) directly, e.g.

---

<div class="post-metadata">

**Author:** ![hardyVeles](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hardyveles/32/7317_2.png) [@hardyVeles](https://forum.juce.com/u/hardyVeles)\
**Post date:** [October 22, 2019, 5:00pm UTC](https://forum.juce.com/t/screen-capture-desktop/15319/14 "2019-10-22T17:00:32Z")

</div>

Oh! I’m coming from C# - that “new” wasn’t meant as C++ new 🙂 Thanks for tip, I’ll update my answer with your suggestion. THANKS!

---

<div class="post-metadata">

**Author:** ![jbyrd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jbyrd/32/3945_2.png) [@jbyrd](https://forum.juce.com/u/jbyrd)\
**Post date:** [November 23, 2019, 2:59am UTC](https://forum.juce.com/t/screen-capture-desktop/15319/15 "2019-11-23T02:59:48Z")

</div>

> **[smasherprog/screen\_capture\_lite](https://github.com/smasherprog/screen_capture_lite)**
>
> cross platform screen/window capturing library. Contribute to smasherprog/screen\_capture\_lite development by creating an account on GitHub.
