C++使用ADO實現(xiàn)存取圖片的方法
一般在網(wǎng)上查到的資料中向Server2000存儲圖片代碼比較多,從數(shù)據(jù)庫中讀取圖片并顯示也不少,但是把圖片從數(shù)據(jù)庫中二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為原圖片保存在本地,就很少有C++代碼了。本文就此問題一步一步地講一講解決的方法:
一、使用數(shù)據(jù)庫前的準(zhǔn)備
我們使用ADO,是用_ConnectionPtr,_RecordsetPtr來操縱數(shù)據(jù)庫的。還有一個_CommandPtr,本程序沒有使用它。
為了使用ADO,需要導(dǎo)入ADO動態(tài)鏈接庫。在工程的stdafx.h文件中,添加如下代碼:
//導(dǎo)入ADO
#import "C:\Program Files\Common Files\System\ado\msado15.dll"\
rename_namespace("ADOCG")rename("EOF","EndOfFile")
using namespace ADOCG;
這些代碼聲明,在這個工程中使用ADO但不使用ADO的名字空間,并且為了避免常數(shù)沖突,將常數(shù)EOF改名為adoEOF。
再有就是要建一個簡單的數(shù)據(jù)庫,名字叫TestImage,里面有一個表Images,這個表有三個字段,分別是ID,Name,ImageData。
二、連接數(shù)據(jù)庫
連接數(shù)據(jù)庫的代碼可以放入一個函數(shù)中,在想調(diào)用的地方調(diào)用。一般不推薦在CAPP類的Initalize()里連接數(shù)據(jù)庫,在退出程序時關(guān)閉數(shù)據(jù)庫連接。應(yīng)該是在使用時連接,使用完馬上關(guān)閉。項目中m_pConn是_ConnectionPtr類型的變量。
BOOL OpenConnection()
{
if(m_pConn == NULL)
{
m_pConn.CreateInstance("ADODB.Connection"); //創(chuàng)建_ConnectionPtr的一個實例
}
try
{
if(adStateClosed == m_pConn->State) //如果已關(guān)閉
{
m_pConn->Open("driver={SQL Server};Server=HP-CADD722B76A0;DATABASE=TestImage;UID=sa;PWD=sa","","",adModeUnknown); //因數(shù)據(jù)庫而異
return true;
}
}
catch(_com_error e)
{
AfxMessageBox(_T("連接數(shù)據(jù)庫失敗!"));
return false;
}
}
三、打開數(shù)據(jù)集,操縱數(shù)據(jù)庫
在使用_RecordSetPtr對象m_pRecord時,必須先創(chuàng)建這種對象的一個實例:
m_pRecord.CreateInstance( __uuidof(RecordSet) );
CString strSQL;
//獲取表中最大的id,下一次插入時就用id+1
strSQL.Format(_T("Select count(*) as num, Max(ID) as maxid from Images"));
try
{
m_pRecord->Open(strSQL.AllocSysString(), m_pConn.GetInterfacePtr(),
adOpenDynamic, adLockUnspecified, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox(_T("讀取最大的id異常"));
eturn;
}
//從RecordSet中獲取數(shù)據(jù)數(shù)目和當(dāng)前數(shù)據(jù)庫中最大的ID。
int num = m_pRecord->GetCollect("num");
int maxid;
if (num != 0)
{
maxid = m_pRecord->GetCollect("maxid");
}
else
{
maxid = 0;
}
strSQL.Format(_T("Select * from Images where ID = %d"), maxid);
//下面向數(shù)據(jù)庫中插入圖片等。
//首先從數(shù)據(jù)庫中讀id最大的那條數(shù)據(jù),主要目的是為了將RecordSet初始化
m_pRecord.CreateInstance(__uuidof(Recordset));
上面這句一定要注意,因為上一次把一些數(shù)據(jù)放入m_pRecord中,這一次再放的時候,要重新創(chuàng)建一次,否則數(shù)據(jù)格式要么不匹配,要么保留有上一次的數(shù)據(jù),定位困難。
m_pRecord->Open(strSQL.AllocSysString(), m_pConn.GetInterfacePtr(),
adOpenDynamic, adLockOptimistic, adCmdText); //這是AddNew方法要求的
CString imagepath = _T("F:/200713454/20090326.bmp");
CString imagename = imagepath.Right(12);
try
{
m_pRecord->AddNew(); //為記錄集添加新的一行,更新時就會把這條新紀(jì)錄放到數(shù)據(jù)庫中
}
catch (_com_error e)
{
AfxMessageBox(_T("不能插入一條新的記錄"));
return;
}
try
{
//使用putcollect插入非圖像數(shù)據(jù),使用SetImage2DB插入圖像數(shù)據(jù)
m_pRecord->PutCollect("ID", _variant_t(maxid+1));
m_pRecord->PutCollect("Name", _variant_t(imagename));
SetImage2DB(imagepath);
}
catch (_com_error e)
{
AfxMessageBox(_T("插入圖片有異常"));
return;
}
m_pRecord->Update();
//使用完畢,關(guān)閉m_pRecord,并設(shè)置為NULL,最后關(guān)閉數(shù)據(jù)庫連接
m_pRecord->Close();
m_pRecord = NULL;
CloseConnection();
四、讀取圖片并存儲到本地計算機
要將數(shù)據(jù)庫中的二進(jìn)制數(shù)據(jù)變?yōu)閳D片,最簡單的方法就是用GDI+。GDI+有一個類是Image,可以用stream來創(chuàng)建對象,還可以用Save方法保存到本地,所以這個類很符合需要。
要使用GDI+,需要做些設(shè)置。首先在VS2005的項目屬性中,加上gdiplus.lib。
然后在stdafx.h中添加代碼
#include <GdiPlus.h> using namespace Gdiplus;
在CApp類添加兩個變量:
GdiplusStartupInput m_gdiplusstartUpInput; ULONG_PTR m_GdiplusToken;
在CApp的InitInstance函數(shù)中添加
GdiplusStartup(&m_GdiplusToken, &m_gdiplusstartUpInput, NULL);
在ExitInstance函數(shù)中添加
GdiplusShutdown(m_GdiplusToken);
以下是讀取圖片數(shù)據(jù)并保存到本地的代碼實現(xiàn):
OpenConnection();
m_pRecord.CreateInstance(__uuidof(Recordset));
CString strSQL;
strSQL.Format(_T("Select * from Images where ID = 1"));
try
{
m_pRecord->Open(strSQL.AllocSysString(), m_pConn.GetInterfacePtr(),
adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox(_T("讀取圖片信息異常"));
return;
}
LPVOID Data;
char* pbuf = NULL;
long lDatasize = m_pRecord->GetFields()->GetItem("ImageData")->ActualSize; //數(shù)據(jù)庫中圖像數(shù)據(jù)長度
CString imagename = m_pRecord->GetCollect("Name").bstrVal;
if (lDatasize > 0)
{
_variant_t varBLOB;
varBLOB = m_pRecord->GetFields()->GetItem("ImageData")->GetChunk(lDatasize);
Data = new char[lDatasize+1];
if (varBLOB.vt == (VT_ARRAY|VT_UI1))
{
SafeArrayAccessData(varBLOB.parray, (void **)&pbuf);
memcpy(Data, pbuf, lDatasize);
SafeArrayUnaccessData(varBLOB.parray);
}
}
IStream* pStm;
LONGLONG cb = lDatasize;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData;
if (hGlobal != NULL)
{
pvData = GlobalLock(hGlobal);
memcpy(pvData, Data, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
else
{
AfxMessageBox(_T("Error"));
return;
}
CLSID encoderClsid;
GetEncoderClsid(L"image/bmp",&encoderClsid); //確定編碼格式是bmp格式
Image image(pStm, TRUE);
CString imagepath;
imagepath.Format(_T("F:/200713454/%s"), imagename);
image.Save(imagepath, &encoderClsid, NULL); //把image中的數(shù)據(jù)按照bmp編碼格式存到本地
m_pRecord->Close();
m_pRecord = NULL;
CloseConnection();
上面存儲和讀取數(shù)據(jù)的代碼中用到了兩個函數(shù),GetEncoderClsid和SetImage2DB。它們的實現(xiàn)如下:
這個函數(shù)和上面的存/取函數(shù)都是一個類的成員函數(shù),而m_pConn和m_pRecord是這個類的成員變量,所以
void CDlgTest::SetImage2DB(CString path)
{
VARIANT varChunk;
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
CFile f(path.operator LPCTSTR(),CFile::modeRead);
BYTE bval[ChunkSize+1];
long uIsRead=0;
while (1)
{
uIsRead=f.Read(bval,ChunkSize);
if (uIsRead==0) break;
rgsabound[0].cElements=uIsRead;
rgsabound[0].lLbound=0;
psa=SafeArrayCreate(VT_UI1,1,rgsabound);
for (long index=0;index<uIsRead;index++)
{
if (FAILED(SafeArrayPutElement(psa,&index,&bval[index])))
AfxMessageBox(_T("錯誤。"));
}
varChunk.vt =VT_ARRAY|VT_UI1;
varChunk.parray=psa;
try
{
m_pRecord->Fields->GetItem("ImageData")->AppendChunk(varChunk);
}
catch (_com_error e)
{
AfxMessageBox(_T("錯誤。"));
}
::VariantClear(&varChunk);
::SafeArrayDestroyData(psa);
if (uIsRead<ChunkSize)break;
}
f.Close();
}
INT CDlgTest::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
至此就實現(xiàn)了存儲圖片和從數(shù)據(jù)庫中把圖片下載到本地的功能。
欄 目:C語言
下一篇:Win32應(yīng)用程序(SDK)設(shè)計原理詳解
本文標(biāo)題:C++使用ADO實現(xiàn)存取圖片的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3550.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言沒有round函數(shù) round c語言
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?


