Click here to Skip to main content
14,693,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code to take the screenshot. And stores it in JPEG format. I want to read the JPEG file and need to save it in the database. How to do that??
Please help me?

using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	int c=0;
	HDC scrdc, memdc;
	HBITMAP membit;
	HBITMAP hOldBitmap;
	CString lpfilename;
	char buffer[1000];
		   
	
	for(;;)
	{
		
		
		scrdc = ::GetDC(0);
		int Height = GetSystemMetrics(SM_CYSCREEN);
		int Width = GetSystemMetrics(SM_CXSCREEN);
		memdc = CreateCompatibleDC(scrdc);
		membit = CreateCompatibleBitmap(scrdc, Width, Height);
		 hOldBitmap=(HBITMAP) SelectObject(memdc, membit);
		BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);

		Gdiplus::Bitmap bitmap(membit, NULL);
		CLSID clsid;
		GetEncoderClsid(L"image/jpeg", &clsid);

		sprintf_s(buffer,"D:\image%u.jpeg",c);
		lpfilename=buffer;
		
			bitmap.Save(lpfilename, &clsid);
			CFile file;
			BYTE buf[150];
			CStringA charstr(lpfilename);
	   const char *szSingle;
       szSingle=((const char *) charstr);
	   //czFile=(char*)szSingle;
	
      FILE * fp= NULL; 
	  fp = fopen(szSingle,"wb"); 
	  if(fp == NULL) return ;
	  fread(buf,150,150,fp);
	  StrByte=buf;

			

			CString strDateTime;
			SYSTEMTIME datetime;
			::GetLocalTime(&datetime);
			strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),
            datetime.wYear,
            datetime.wMonth,     
            datetime.wDay,       
			datetime.wHour,
            datetime.wMinute,
            datetime.wSecond);	

			CString SqlStr4;
			SqlStr4 = "INSERT INTO log VALUES grab_date='";
			SqlStr4 +=strDateTime;
			SqlStr4 += "',ip_address='";
			SqlStr4 +=SqlStr3;
			SqlStr4 += "',image='";
			SqlStr4 += StrByte;
			SqlStr4 +="';";


			 db->BeginTrans();
			 db->ExecuteSQL(SqlStr4);
			 db->CommitTrans();			
			 Sleep(10000);
			 c++;
		}
		
		SelectObject(memdc, hOldBitmap);

		DeleteObject(memdc);

		DeleteObject(membit);

		::ReleaseDC(0,scrdc);
	

	GdiplusShutdown(gdiplusToken);
}
Posted

1 solution

Why save it to a file, if you want to save it to a database? You get it as a bitmap in the first place, just use GetBitmapBits[^] and save them as an Image field in your DB...

Which part of this is causing a problem?


"fread function is not reading properly, so while executing the query it leads to exception. StrByte is empty."

Um. This one?
    FILE * fp= NULL;
fp = fopen(szSingle,"wb");
if(fp == NULL) return ;
fread(buf,150,150,fp);

Have a look at this: fopen specification[^]
Pay attention to where it says:
w or wb
Truncate to zero length or create file for writing.



"Still my buf contains only 4 characters. How to read the jpeg file fully in order to save it in the database. help me"


Now, (assuming you changed the "wb" to "r" or similar) you need to look at two things:
fread[^] with reference to the second and third parameters:
fread(buf,150,150,fp);
and
BYTE buf[150];
If you want to continue working with the actual data...
Why 150? Is this a Magic Number for you? It bears no relation to the bitmap size, at all...

How do you know it only has 4 characters? What should it have?
Are the first two by any chance #42 and #4D?
   
v4
Comments
Gokulnath007 7-May-11 3:41am
   
fread function is not reading properly, so while executing the query it leads to exception. StrByte is empty.
OriginalGriff 7-May-11 4:07am
   
Answer updated
Gokulnath007 7-May-11 5:27am
   
Thank you
OriginalGriff 7-May-11 5:34am
   
You're welcome!
Gokulnath007 7-May-11 6:05am
   
Still my buf contains only 4 characters. How to read the jpeg file fully in order to save it in the database. help me
OriginalGriff 7-May-11 6:59am
   
Answer Updated
Gokulnath007 7-May-11 7:11am
   
No while adding to the database, it is giving exception. How to get the JPEG bits as bytes to store in the blob field of the database?
OriginalGriff 7-May-11 7:17am
   
What exception?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900