Solved

how do I save a screen shot?

Posted on 2007-11-26
45
415 Views
Last Modified: 2009-12-16
HDC hWindowDC = ::GetDC(hWnd);
(?)          COLORREF GetPixel(   HDC hWindowDC, int x, int y );


this is how I get a screenshot into a DC then I access it with getpixel
I have problem with the (?) part. i don't know what type of variable should be used there.


how do I save screen shot in this context?

I sometimes need to see what's in the dc visually.
0
Comment
Question by:Troudeloup
  • 24
  • 14
  • 5
  • +1
45 Comments
 

Author Comment

by:Troudeloup
Comment Utility
more precisely, how to save


HDC hWindowDC  to a screen shot.
0
 
LVL 7

Expert Comment

by:lucky_james
Comment Utility
in place of (?) you can use a DWORD.

check out :
http://msdn2.microsoft.com/en-us/library/aa383751.aspx
0
 

Author Comment

by:Troudeloup
Comment Utility
yeah actually that's solved in another thread already.



how do I save it to a .bmp?

HDC hWindowDC







0
 
LVL 7

Expert Comment

by:lucky_james
Comment Utility
let me know if you have any doubts.
0
 

Author Comment

by:Troudeloup
Comment Utility
how do I save it to a .bmp?

HDC hWindowDC
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Look the following link: http:Q_20740410.html

0
 

Author Comment

by:Troudeloup
Comment Utility
uhhhh
that's too much code and I know I can do with a lot less, i think it's a one liner i am looking for


something like

HDC hWindowDC = ::GetDC(hWnd);
hWindowDC.save ("bmp.bmp");
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> that's too much code and I know I can do with a lot less, i think it's a one liner i am looking for
Do ALT + PRINT in the DOS-Box  ;-)

>>>> I know I can do with a lot less
not if you consider all cases as the above prog does.

you may look for CXImage library. It is a free library and I think they have a function to grab the screen and store it to a file.
 
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> not if you consider all cases as the above prog does.
You may drop all cases which do not apply. But if you ever want to run your prog at an older system with a bad graphic card or a on-board graphic chip, your prog will fail (maybe crash).
0
 

Author Comment

by:Troudeloup
Comment Utility
that's taken into account.


just do the least complicated thing ;p



how do I save that same dc into a bmp and then pass the same thing to getpixel?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> how do I save that same dc into a bmp and then pass the same thing to getpixel?
A. To put it into a bmp you can take the code from the above link, throw off all things handling bitmaps not using RGB or less than 32 bits.

For the getPixel I already showed you code in another thread (how may questions did you ask? 100 or 1000?). You also could take the code of A and use the lpbits pointer after GetDIBits to retrieve some singular pixels.
0
 

Author Comment

by:Troudeloup
Comment Utility
can you show me the code of doing so?

I never learned how to save a screenshot.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Just adding the code from your previous Q:

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen("c;\\tmp.bmp", "wb");
        if(pFile == NULL)
        {
            AfxMessageBox(IDS_FILE_CREATE_ERROR_MESSAGE);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
0
 

Author Comment

by:Troudeloup
Comment Utility
do I make it a function by doing this?



void         RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen("c;\\tmp.bmp", "wb");
        if(pFile == NULL)
        {
            AfxMessageBox(IDS_FILE_CREATE_ERROR_MESSAGE);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);





// I need something to return

so I can do this after


GetPixel(   HDC hWindowDC, int x, int y )



}
0
 

Author Comment

by:Troudeloup
Comment Utility
for this part

I always need to get hWindowDC for getpixel

before that I optionally save a screenshot (or not, depending on my need, so I need to be able to know which part of code I can skip, if I don't need to save screenshot )
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> can you show me the code of doing so?
>>>> I never learned how to save a screenshot.

???
The link  http:Q_20740410.html I gave you has full code for a screenshot and I remember of at least two other threads where you had valid code for it.

In the prog there is a GetDIBits and a CreateFile statement. Between these statements it is a good chance to save the bits pointer so that the getPixel has access to it. You best would save it in a class member variable together with the width of the bitmap. The width is needed to calculate the index of the RGBQUAD array [x*width + y ] for a pixel(x, y).

Regards, Alex
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>do I make it a function by doing this?

Yes, pass the window handle and a filename, e.g.

#include <windows.h>
#include <stdio.h>

void SaveWindow(HWND hwnd, const char* pName)
{

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}
0
 

Author Comment

by:Troudeloup
Comment Utility
can you state that if -1 is given as file name, then it means for that run no bmp should be saved?

it returns this right?

HDC hWindowDC,
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, the name can directly correspond to a run number, you just need to format that correctly, e.g.

#include <sstream>
#include <string>
using namespace std;

for (int i = 0; i < 5; ++i)
{
  ss << "file" << i << ".bmp"; // create 'fileX.bmp'  
  SaveWindow(hwnd,ss.str().c_str());
}
0
 

Author Comment

by:Troudeloup
Comment Utility
i hit a problem:

void savewindow ( HWND hwnd1, string string1 )
{
      string ss;
      string1 << "1" << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
      
      




hdc.cpp: In function `void savewindow(HWND__*, std::string)':
hdc.cpp:33: error: no match for 'operator<<' in 'string1 << "1"'
hdc.cpp:34: error: `SaveWindow' was not declared in this scope
hdc.cpp:34: error: 'struct std::string' has no member named 'str'
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, you need to use a 'stringstream' here, i.e.

void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << "1" << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
     
0
 

Author Comment

by:Troudeloup
Comment Utility
I still get this

hdc.cpp:34: error: `SaveWindow' was not declared in this scope
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Be sure to add the declaration


void SaveWindow(HWND hwnd, const char* pName);

at the top of the file.
0
 

Author Comment

by:Troudeloup
Comment Utility
#include <sstream>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;


// save window and hdc hdc return are the same color?
// do they have the same picture?
// ie, what

void savewindow ( HWND hwnd1, string string1 );
// save the window given hwnd


int main ()
{
      
      
      
      
      
      return 0;
}



void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << string1 << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
      
      
      
      
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
If you have the 'SaveWindow()' function in a separate file, you need a prototype like

#include <sstream>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;


// save window and hdc hdc return are the same color?
// do they have the same picture?
// ie, what

void savewindow ( HWND hwnd1, string string1 );
// save the window given hwnd


void SaveWindow(HWND hwnd, const char* pName);


int main ()
{
     
     
     
     
     
      return 0;
}



void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << string1 << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}

#include <stdio.h>

void SaveWindow(HWND hwnd, const char* pName)
{

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}
0
 

Author Comment

by:Troudeloup
Comment Utility
#include <sstream>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>

using namespace std;


// save window and hdc hdc return are the same color?
// do they have the same picture?
// ie, what

void SaveWindow(HWND hwnd, const char* pName);
void savewindow ( HWND hwnd1, string string1 );
// save the window given hwnd


int main ()
{
      
      
      
      
      
      return 0;
}



void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << string1 << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
      
      
      
void SaveWindow(HWND hwnd, const char* pName)
{

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}










compile error:



hdc.cpp:54: error: 'struct RECT' has no member named 'width'
hdc.cpp:55: error: 'struct RECT' has no member named 'height'
hdc.cpp:58: error: `hdc' was not declared in this scope
hdc.cpp:60: error: `pDrawingSurfaceBits' was not declared in this scope
hdc.cpp:61: error: cannot convert `HDC__*' to `HWND__*' for argument `1' to `int ReleaseDC(HWND__*, HDC__*)'
0
 

Author Comment

by:Troudeloup
Comment Utility
HBITMAP hDrawingSurface



can I pass this to getpixel ?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Sorry, change

        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;

into

        BMIH.biWidth = rc.right - rc.left;
        BMIH.biHeight = rc.bottom - rc.top;

>>can I pass this to getpixel ?

No, you need a HDC for that.
0
 

Author Comment

by:Troudeloup
Comment Utility
hdc.cpp: In function `void SaveWindow(HWND__*, const char*)':
hdc.cpp:58: error: `hdc' was not declared in this scope
hdc.cpp:60: error: `pDrawingSurfaceBits' was not declared in this scope
hdc.cpp:61: error: cannot convert `HDC__*' to `HWND__*' for argument `1' to `int ReleaseDC(HWND__*, HDC__*)'
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Change

        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);

to

        HBITMAP hDrawingSurface = CreateDIBSection(hDC,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hwnd,hDC);

0
 

Author Comment

by:Troudeloup
Comment Utility
hdc.cpp: In function `void SaveWindow(HWND__*, const char*)':
hdc.cpp:60: error: `pDrawingSurfaceBits' was not declared in this scope
0
 

Author Comment

by:Troudeloup
Comment Utility
i have a question,  what is the standard way of c++ to screeshot with window.h?
0
 

Author Comment

by:Troudeloup
Comment Utility
btw, thank you for pointing me to this page

it's very good.

http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
I am sorry, make that

void SaveWindow(HWND hwnd, const char* pName)
{
        BYTE* pDrawingSurfaceBits;
        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.right - rc.left;
        BMIH.biHeight = rc.bottom - rc.top;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hDC,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hwnd,hDC);

       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}
0
 

Author Comment

by:Troudeloup
Comment Utility
this pops

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccaAbaaa.o:hdc.cpp:(.text+0x39a): undefined reference to `CreateDIBSection@24'
collect2: ld returned 1 exit status












#include <sstream>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>

using namespace std;


// save window and hdc hdc return are the same color?
// do they have the same picture?
// ie, what

void SaveWindow(HWND hwnd, const char* pName);
void savewindow ( HWND hwnd1, string string1 );
// save the window given hwnd


int main ()
{
      
      
      
      
      
      return 0;
}



void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << string1 << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
      
      
void SaveWindow(HWND hwnd, const char* pName)
{
        BYTE* pDrawingSurfaceBits;
        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.right - rc.left;
        BMIH.biHeight = rc.bottom - rc.top;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hDC,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hwnd,hDC);

       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
You need to link with gdi32.lib - I thin, with MinGW, that is libgdi32.a
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Sorry, I was up to posting a sample, but a pal of mine died just two hours ago, I am just not able to do that - I tried, but cannot gather my mind :-(

It was his birthday today...
0
 

Author Comment

by:Troudeloup
Comment Utility
please do whatever that's good for you.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Thank you. If you have a drink, raise your glass for him, he'd like that. *bummer*
0
 

Author Comment

by:Troudeloup
Comment Utility
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

void SaveWindow(HWND hwnd, string name);

int main ()
{
      HWND hwnd1 = FindWindow(NULL, "window name");
      string name = "tmp.bmp";
      
      SaveWindow( hwnd1,  name);
      
      
      return 0;
}





void SaveWindow(HWND hwnd, string name)
{

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE pFile = fopen(name, "wb");



        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}
0
 

Author Comment

by:Troudeloup
Comment Utility
test2.cpp: In function `void SaveWindow(HWND__*, std::string)':
test2.cpp:39: error: 'struct RECT' has no member named 'width'
test2.cpp:40: error: 'struct RECT' has no member named 'height'
test2.cpp:43: error: `hdc' was not declared in this scope
test2.cpp:45: error: `pDrawingSurfaceBits' was not declared in this scope
test2.cpp:46: error: cannot convert `HDC__*' to `HWND__*' for argument `1' to `int ReleaseDC(HWND__*, HDC__*)'
test2.cpp:49: error: cannot convert `std::string' to `const char*' for argument `1' to `FILE* fopen(const char*, const char*)'
test2.cpp:63: error: cannot convert `FILE' to `FILE*' for argument `4' to `size_t fwrite(const void*, size_t, size_t, FILE*)'
test2.cpp:66: error: cannot convert `FILE' to `FILE*' for argument `4' to `size_t fwrite(const void*, size_t, size_t, FILE*)'
test2.cpp:72: error: cannot convert `FILE' to `FILE*' for argument `1' to `int fclose(FILE*)'
>Exit code: 1






this is the bug messages
0
 

Author Comment

by:Troudeloup
Comment Utility
or rather, can you suggest a code to save hwnd into .bmp

?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
I have a modified version here that "works", unfortunately the bitmap is black and I do not have too much time at the moment:

#include <sstream>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>

using namespace std;


// save window and hdc hdc return are the same color?
// do they have the same picture?
// ie, what

void SaveWindow(HWND hwnd, const char* pName);
void savewindow ( HWND hwnd1, string string1 );
// save the window given hwnd


int main ()
{
     
     
     
      HWND hwnd = FindWindow(NULL,"Experts Echange");

      SaveWindow(hwnd,"test.bmp");
     
      return 0;
}



void savewindow ( HWND hwnd1, string string1 )
{
      stringstream ss;
      ss << string1 << ".bmp";
      SaveWindow( hwnd1, ss.str().c_str());
}
     
     
void SaveWindow(HWND hwnd, const char* pName)
{
        BYTE* pDrawingSurfaceBits;
        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        HDC mDC;

        mDC = CreateCompatibleDC(hDC);

        HBITMAP hbmp = CreateCompatibleBitmap(hDC,rc.right - rc.left,rc.bottom - rc.top);
     
        BitBlt(mDC,rc.top,rc.bottom,rc.right - rc.left,rc.bottom - rc.top,hDC,rc.top,rc.bottom,SRCCOPY);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.right - rc.left;
        BMIH.biHeight = rc.bottom - rc.top;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
      //pDrawingSurfaceBits = new BYTE [BMIH.biSizeImage];
        HBITMAP hDrawingSurface = CreateDIBSection(mDC,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hwnd,hDC);

        if (!hDrawingSurface) cout << GetLastError() << endl;
       
        //Create a new file for writing
        FILE *pFile = fopen(pName, "wb");
        if(pFile == NULL)
        {
            MessageBox(NULL,"Cannot open file","Error",MB_OK);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
}


(Yes, cleanup code is missing also)
0
 

Author Comment

by:Troudeloup
Comment Utility
i haven't tried this one yet, since it's black anyway


it would be awsome and i can start my OCR portion of the project if you would kindly do your magic when you have time. :)
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Don't worry, I will when I have some more time on my hands - won't forget about you ;o)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now