//
头部包含afxinet.h
2
#include
<
afxinet.h
>
3
CInternetSession::OpenURL函数原型为:
4
CStdioFile
*
OpenURL( LPCTSTR pstrURL,
//
文件URL地址
5
DWORD dwContext
=
1
,
//
上下文ID
6
DWORD dwFlags
=
INTERNET_FLAG_TRANSFER_ASCII,
//
标记
7
LPCTSTR pstrHeaders
=
NULL,
//
发送到服务器的数据头
8
DWORD dwHeadersLength
=
0
);
//
发送到服务器的数据头长度
9
dwFlags可以为:
10
INTERNET_FLAG_RELOAD 强制重读数据
11
INTERNET_FLAG_DONT_CACHE 不保存到缓存
12
INTERNET_FLAG_TRANSFER_ASCII 使用文本数据
13
INTERNET_FLAG_TRANSFER_BINARY 使用二进制数据
14
//
获取网页的方法代码如下
15
CString getURLContext(LPCTSTR pstrURL)
16
{
17
//
自定义http头信息
18
char
*
headers
=
"
Accept:*/*\r\n
"
19
"
Accept-Language:zh-cn\r\n
"
20
"
User-Agent:VCTestClient\r\n
"
;
21
CString m_strHTML;
22
try
23
{
24
CInternetSession sess;
//
建立会话
25
CHttpFile
*
pF
=
(CHttpFile
*
)sess.OpenURL(pstrURL,
1
,INTERNET_FLAG_TRANSFER_ASCII
||
INTERNET_FLAG_RELOAD,headers,strlen(headers));
//
打开文件
26
注意:OpenURL 方法的dwFlags参数 必须选择传输模式为INTERNET_FLAG_TRANSFER_ASCII 或者 INTERNET_FLAG_TRANSFER_BINARY
27
CString szData,szAllData;
28
while
(pF
->
ReadString(szData))
29
{
30
//
读取文件
31
szAllData
+=
"
\r\n
"
;
32
szAllData
+=
szData;
33
}
34
pF
->
Close();
35
sess.Close();
36
m_strHTML
=
szAllData;
37
}
38
catch
(CException
*
e)
39
{
40
AfxMessageBox(
"
获取数据失败
"
);
41
}
42
return
m_strHTML;
43
}
-------------------------------------------------------------------------------------------------------------------------------------
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT CBMPDlg::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
::GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
char
*
headers
=
"
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*
"
"
Accept-Encoding: gzip, deflate\r\n
"
"
Accept-Language: zh-CN\r\n
"
"
Content-Type: application/x-www-form-urlencoded\r\n
"
"
Cache-Control: no-cache\r\n
"
;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
DWORD dwFlags
=
INTERNET_FLAG_TRANSFER_BINARY
|
INTERNET_FLAG_RELOAD
|
INTERNET_FLAG_DONT_CACHE;
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl,
1
,dwFlags,headers,strlen(headers));
//
1,dwFlags
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
char
achQueryBuf[
32
];
DWORD dwQueryBufLen
=
sizeof
(achQueryBuf);
CHttpFile
*
pHF
=
(CHttpFile
*
)httpFile;
pHF
->
QueryInfo(HTTP_QUERY_CONTENT_LENGTH, achQueryBuf,
&
dwQueryBufLen, NULL);
dwFileSize
=
(DWORD)atol(achQueryBuf);
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
C中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
在VC中我们通常用Ado的Field 对象的GetChuck和AppendChunk来读写Blob对象,但是这样做要写很多的代码,其实ado给我们提供了一个更易操作的对象那就是Stream Object,通过它我们可以更容易的操作数据库中的Blob对象,而且可以直接把Blob对象从数据库保存到本地文件,或者直接读取文件写入到数据库中。下面就详细描述如何操作。
首先申明Stream对象
_ StreamPtr pStm;
pStm.CreateInstance( "ADODB.Stream ");
variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
pStm-> PutType(adTypeBinary);//类型为二进制
//得到字段内容的大小
// long lDataSize = m_pRecordset-> GetFields()-> GetItem( "photo ")-> ActualSize;
//m_pRecordset为一个打开的纪录集对象,含有photo这个blob字段
pStm-> Open( varOptional,
adModeUnknown, adOpenStreamUnspecified, _bstr_t(), _bstr_t());
//打开pStm
pStm-> Write(_variant_t(m_pRecordset-> GetFields()-> GetItem( "photo ")-> Value));
//把photo字段的内容写入pStm
pStm-> SaveToFile( "c:\\publogo.jpg ", adSaveCreateOverWrite);
pStm-> Close();
//直接保存为文件,如果是别的格式只要保存的时候改变后缀就可以了
下面是如果不保存成文件可以直接通过Com对象来绘制该图片,IPicture 可以显示多种格式图片.
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, pStm-> GetSize());
LPVOID pvData = NULL;
IStream *ps;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
char * m_pBMPBuffer = new char[pStm-> GetSize()+1];//分配必要的存储空间
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pvData,pBuf,pStm-> GetSize()); //复制数据到缓冲 区 m_pBMPBuffer
SafeArrayUnaccessData (varBLOB.parray);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &ps);
}
}
IPicture *pPic;
if(SUCCEEDED(OleLoadPicture(ps,pStm-> GetSize() ,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic-> get_Width(&hmWidth);
pPic-> get_Height(&hmHeight);
double fX,fY;
CDC *pDC = GetClientDC();//这里根据具体请况来取
fX=(double)pDC-> GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC-> GetDeviceCaps(HORZSIZE)*100.0);
fY =(double)pDC-> GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC-> GetDeviceCaps(VERTSIZE)*100.0);
if(FAILED(pPic-> Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
AfxMessageBox( "图像绘制失败! ");
pPic-> Release();
}
这样就完成了对图片文件从数据库读出和显示的过程。
对于从文件写入数据库就相对的容易多了,
_StreamPtr pStm;
pStm.CreateInstance( "ADODB.Stream ");
variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
// long lDataSize = m_pRecordset-> GetFields()-> GetItem( "photo ")-> ActualSize;
pStm-> PutType(adTypeBinary;
pStm-> Open( varOptional,
adModeUnknown, adOpenStreamUnspecified, _bstr_t(), _bstr_t());
pStm-> LoadFromFile( "c:\\book.gif ");//读入文件
variant_t varBLOB=pStm-> Read(adReadAll);
m_pRecordset-> GetFields()-> GetItem( "photo ")-> Value= varBLOB;//保存到数据集对象。
以上就是关于ADO的Stream对象操作数据库中的blob字段的具体方法,主要是对图片我们还可对它进行改造成其他的,比如读者可以改造成读取XMl文件,并保存等等。
转换为Bitmap
CComPtr spPic;
if (varImage.vt != (VT_ARRAY|VT_UI1))return FALSE;
SAFEARRAY *psa = varImage.parray;
//get size
long size=0;
_com_util::CheckError(SafeArrayGetUBound(psa, 1, &size));
size += 1;
//copy
char *pBuf = NULL;
_com_util::CheckError(SafeArrayAccessData(psa, (void **)&pBuf));
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, size);
void *pGlobal = GlobalLock(hGlobal);
if (!pGlobal)return FALSE;
memcpy(pGlobal, pBuf, size);
SafeArrayUnaccessData(psa);
GlobalUnlock(hGlobal);
CComPtr spStream;
_com_util::CheckError(CreateStreamOnHGlobal(hGlobal, true, &spStream));
_com_util::CheckError(OleLoadPicture(spStream, 0, TRUE, IID_IPicture, (void **)&spPic));
HBITMAP hBmp;
spPic-> get_Handle((OLE_HANDLE *)&hBmp);
CBitmap bmp;
bmp.Attach(hBmp);
BITMAP bm;
bmp.GetBitmap(&bm);
int x=bm.bmWidth;
int y=bm.bmHeight;
bmp.Detach();