Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have just used lpBits to store the binary data. But in database i see only 24bytes. How to convert the find the binary data of the image. To store it in the database.

HDC hScrDC = ::GetDC(NULL);
	HDC hMemDC = NULL;
 
	BYTE *lpBitmapBits = NULL; 
 
	int nWidth = GetSystemMetrics(SM_CXSCREEN);
	int nHeight = GetSystemMetrics(SM_CYSCREEN); 
 
	hMemDC = ::CreateCompatibleDC(hScrDC); 
 
	BITMAPINFO bi; 
	ZeroMemory(&bi, sizeof(BITMAPINFO));
	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bi.bmiHeader.biWidth = nWidth;
	bi.bmiHeader.biHeight = nHeight;
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biBitCount = 24;
 
	HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
	HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap); 
 
	::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
 
	BITMAPFILEHEADER bh;
	ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
	bh.bfType = 0x4d42; //bitmap 
	bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);

         lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, bi.bmiHeader.biSizeImage);	
 
	CFile file;	
	char buffer[1000];
	DWORD threadId;
  int value = 10;
  //hThread = CreateThread( NULL, 0, runThread, &value, 0, &threadId);
	for(int c=0;c<10;c++)
	{
		 sprintf_s(buffer,"D:\image%u.jpg",c);
		 CString sName(buffer);  
         LPCTSTR lpszName = sName;  
		if(file.Open(lpszName, CFile::modeCreate | CFile::modeWrite))
		{ 
			file.Write(&bh, sizeof(BITMAPFILEHEADER));
			file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
			file.Write(lpBitmapBits, 3 * nWidth * nHeight);
			file.Close();
			Sleep(10000);
		}
		
	}
	::SelectObject(hMemDC, oldbmp);
	::DeleteObject(bitmap);
	::DeleteObject(hMemDC);
	::ReleaseDC(NULL, hScrDC);
Posted
Updated 2-May-11 23:57pm
v2

What's your question?
The string D:\image%u.jpg should be D:\\image%u.jpg
 
Share this answer
 
Comments
Gokulnath007 30-Apr-11 5:49am    
I have the following code to take the screenshots and storing it in the hard disk. I want to convert it to BLOB data. In this code, where is the blob data found and how to convert the image into the blob data??
For the first: bitmap (BMP) format is not automatically converted to jpeg (JPG).
BLOB stands for Binary Large OBject, and is used to store binary data into a database. This can be everything. So you can store the whole bitmap in a database.
You should open a stream to your BLOB in database and write to it like into a file.
Regards.
 
Share this answer
 
Comments
Gokulnath007 2-May-11 0:25am    
Could you add more points regarding "converting the image into blob data"
mbue 3-May-11 7:37am    
In your code i cant see accessing a database. How do you access the database?
Regards.
Gokulnath007 4-May-11 6:54am    
I have added below this code once the values from the image to blob is over. Need to save it in the database.

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