Troudeloup
asked on
linking gdi
this pops
C:\DOCUME~1\ADMINI~1\LOCAL S~1\Temp/c caAbaaa.o: hdc.cpp:(. text+0x39a ): undefined reference to `CreateDIBSection@24'
collect2: ld returned 1 exit status
when I try to compile this, and it's been suggested that the way to solving it is to
"You need to link with gdi32.lib - I thin, with MinGW, that is libgdi32.a"
how do I do that?
#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**)&pDrawingSurfaceBi ts, 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);
}
C:\DOCUME~1\ADMINI~1\LOCAL
collect2: ld returned 1 exit status
when I try to compile this, and it's been suggested that the way to solving it is to
"You need to link with gdi32.lib - I thin, with MinGW, that is libgdi32.a"
how do I do that?
#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**)&pDrawingSurfaceBi
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
fclose(pFile);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so for this specific compile, I need to add -lgdi32 ?
Yup, give that a try...
ASKER
yes it works :)
ASKER
however, it's black alright.
g++ mycode.cpp -lgdi32