• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

how do I save a screen shot?

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
Troudeloup
Asked:
Troudeloup
  • 24
  • 14
  • 5
  • +1
1 Solution
 
TroudeloupAuthor Commented:
more precisely, how to save


HDC hWindowDC  to a screen shot.
0
 
lucky_jamesCommented:
in place of (?) you can use a DWORD.

check out :
http://msdn2.microsoft.com/en-us/library/aa383751.aspx
0
 
TroudeloupAuthor Commented:
yeah actually that's solved in another thread already.



how do I save it to a .bmp?

HDC hWindowDC







0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
lucky_jamesCommented:
let me know if you have any doubts.
0
 
TroudeloupAuthor Commented:
how do I save it to a .bmp?

HDC hWindowDC
0
 
itsmeandnobodyelseCommented:
Look the following link: http:Q_20740410.html

0
 
TroudeloupAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
itsmeandnobodyelseCommented:
>>>> 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
 
TroudeloupAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
TroudeloupAuthor Commented:
can you show me the code of doing so?

I never learned how to save a screenshot.
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
TroudeloupAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
jkrCommented:
>>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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
I still get this

hdc.cpp:34: error: `SaveWindow' was not declared in this scope
0
 
jkrCommented:
Be sure to add the declaration


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

at the top of the file.
0
 
TroudeloupAuthor Commented:
#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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
#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
 
TroudeloupAuthor Commented:
HBITMAP hDrawingSurface



can I pass this to getpixel ?
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
hdc.cpp: In function `void SaveWindow(HWND__*, const char*)':
hdc.cpp:60: error: `pDrawingSurfaceBits' was not declared in this scope
0
 
TroudeloupAuthor Commented:
i have a question,  what is the standard way of c++ to screeshot with window.h?
0
 
TroudeloupAuthor Commented:
btw, thank you for pointing me to this page

it's very good.

http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
You need to link with gdi32.lib - I thin, with MinGW, that is libgdi32.a
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
please do whatever that's good for you.
0
 
jkrCommented:
Thank you. If you have a drink, raise your glass for him, he'd like that. *bummer*
0
 
TroudeloupAuthor Commented:
#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
 
TroudeloupAuthor Commented:
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
 
TroudeloupAuthor Commented:
or rather, can you suggest a code to save hwnd into .bmp

?
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
Don't worry, I will when I have some more time on my hands - won't forget about you ;o)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 24
  • 14
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now