如何读取jpeg图像并保存到数据库? [英] How to read the jpeg image and save to the database?

查看:165
本文介绍了如何读取jpeg图像并保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来截取屏幕截图.并以JPEG格式存储.我想读取JPEG文件并将其保存在数据库中.怎么做??
请帮帮我吗?

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);
}

推荐答案

为什么要将其保存到文件中,如果要将其保存到数据库中?首先,您可以将其作为位图使用,只需使用 GetBitmapBits [ ^ ]并将其另存为数据库中的图像"字段...

哪一部分引起了问题?


读取功能无法正确读取,因此在执行查询时会导致异常.StrByte为空."

嗯这个吗?
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.




我的buf仍然只包含4个字符.如何完全读取jpeg文件以便将其保存在数据库中.帮帮我"


现在,(假设您将"wb"更改为"r"或类似名称),您需要看两件事:
读取 [




"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);


BYTE buf[150];

如果您想继续使用实际数据...
为什么是150?这是您的魔幻数字吗?它根本与位图大小无关...

您怎么知道它只有4个字符?应该有什么?
头两个碰巧是#42和#4D吗?

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?


这篇关于如何读取jpeg图像并保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆