Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Printscreen in Windows  (Read 6055 times)

0 Members and 2 Guests are viewing this topic.

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Printscreen in Windows
« on: March 07, 2013, 08:41:06 am »
Does anybody have a solution to make screenshots in Windows in Fullescreen ? First of all you can't handle printscreen button (via sfml), second, by default you got black screen. So i think we need to handle it, and upload our picture ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Printscreen in Windows
« Reply #1 on: March 07, 2013, 08:57:21 am »
If you just want the content of the window: window.capture()
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: AW: Printscreen in Windows
« Reply #2 on: March 12, 2013, 10:38:12 am »
If you just want the content of the window: window.capture()
Your answers are SOOO "helpfull".

Solution to make a printscreen if fullscreen mode on windows :
void copyScreenshotToClipboard(const sf::Image & image)
{
        BITMAPINFOHEADER bmpInfoHeader = {0};
        bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmpInfoHeader.biBitCount = 24;
        bmpInfoHeader.biClrImportant = 0;
        bmpInfoHeader.biClrUsed = 0;
        bmpInfoHeader.biCompression = BI_RGB;
        bmpInfoHeader.biHeight = image.getSize().y;
        bmpInfoHeader.biWidth = image.getSize().x;
        bmpInfoHeader.biPlanes = 1;
        bmpInfoHeader.biSizeImage = image.getSize().x * image.getSize().y * 3;

        HGLOBAL hResult;
        if (!OpenClipboard(NULL)) return ;
        if (!EmptyClipboard()) return ;
       
        int size = sizeof(BITMAPINFOHEADER) + bmpInfoHeader.biSizeImage;
        hResult = GlobalAlloc(GMEM_MOVEABLE, size);
        if (hResult == NULL) return ;

        void * pointer = GlobalLock(hResult);
        memcpy(pointer, &bmpInfoHeader, sizeof(BITMAPINFOHEADER));
        pointer = (void*) ((int)pointer + sizeof(BITMAPINFOHEADER));
        const unsigned char * src = image.getPixelsPtr() + (image.getSize().y - 1) * image.getSize().x * 4;
        unsigned char * dest = (unsigned char*)pointer;
        unsigned int srcIndex = 0;
        unsigned int dstIndex = 0;
        for (unsigned int i = 0; i < image.getSize().y; i++) {
                for (unsigned int j = 0; j < image.getSize().x; j++) {                 
                        dest[dstIndex + 0] = src[srcIndex + 2];
                        dest[dstIndex + 1] = src[srcIndex + 1];
                        dest[dstIndex + 2] = src[srcIndex + 0];
                        dstIndex += 3;
                        srcIndex += 4;
                }
                srcIndex = 0;
                src -= image.getSize().x * 4;
                if (image.getSize().x % 4 != 0) dstIndex += 4 - image.getSize().x % 4;
        }

        GlobalUnlock(hResult);

        if (SetClipboardData(CF_DIB, hResult) == NULL) {
                CloseClipboard();
                return ;        // error
        }

        CloseClipboard();
        GlobalFree(hResult);
}
 

This will copy the content of image to clipbuffer, you can insert it to any other program (photoshop, paint, etc.)

To Handle print screen use :
GetAsyncKeyState(VK_SNAPSHOT)

call copyScreenshotToClipboard on the next iteration

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: AW: Printscreen in Windows
« Reply #3 on: March 12, 2013, 10:55:55 am »
Your answers are SOOO "helpfull".
Sorry, from your vague description it's impossible to know what you actually want to achieve. :-\

Besides the window.capture() function works quite well to get the content of the window and since you're in full screen mode your window size equals the screen size and thus a 'print screen' is the same as capturing the content of the window. ;)

Besides that you've never mentioned that you want the image in the clipboard...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LD_FLO

  • Newbie
  • *
  • Posts: 41
    • View Profile
    • Email
Re: Printscreen in Windows
« Reply #4 on: August 05, 2015, 04:25:34 pm »
Thanks Acrobat, very useful :)

Why SFML doesn't have such a feature ? It could be nice to be able to send input too.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Printscreen in Windows
« Reply #5 on: August 05, 2015, 06:29:43 pm »
Why SFML doesn't have such a feature ?
Capturing the window (fullscreen or not) is possible with SFML.
Sending to the clipboard is not, yet.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*