求助:如何用C++输出一张图片

如题所述

C++是在C语言的基础上开发的一种通用编程语言,应用广泛。C++支持多种编程范式 --面向对象编程、泛型编程和过程化编程。 办法:CPaintDC dc(this);//建立设备描述表 HBITMAP hbitmap= ::LoadBitmap(::AfxGetInstanceHandle(); MAKEINTRESOURCE
温馨提示:内容为网友见解,仅供参考
第1个回答  2017-02-05
什么平台下? MFC?
第2个回答  2017-02-06
这是一段我的从数据库读取图片并保存到本地的代码,给你参考下
void CTalkDlg::OnButtonTalkSm1(UINT nFlags, CPoint point) //从数据库读取图片数据
{

//-------------------ado-------------------//
extern _ConnectionPtr pConnection;//ADO连接数据库
extern void Connect_DB(int i);
//
if(pConnection->State==0)//连接是关闭的
{
//MessageBox(_T("0"));
Connect_DB(2);
}
else if(pConnection->State != 1)//连接正在被使用
{
while(pConnection->State != 1)
{
Sleep(500);
}
}

//-----------------------------------------//

//从数据库里取图像文件
try
{
_variant_t vUsername,vID,vname; //变量声明

_RecordsetPtr pRecordset;//记录集
pRecordset.CreateInstance(__uuidof(Recordset));//创建实例
char sSQL[129];
sprintf(sSQL,"SELECT *FROM lt WHERE ltfu=0");

pRecordset->Open(sSQL,_variant_t((IDispatch*)pConnection),adOpenStatic,adLockOptimistic,adCmdText);

CString stryh,strhy;
while (!pRecordset->adoEOF)
{
vUsername = pRecordset->GetCollect(_T("lthy"));//获得好友id
strhy=vUsername.bstrVal;
vUsername = pRecordset->GetCollect(_T("ltyh"));//获得用户id
stryh=vUsername.bstrVal;
if(strhy==m_grxx_zhanghao && stryh==ltname)//////////////////判断是否是发给自己的
{
_variant_t pvList ;
long lDataSize = pRecordset->GetFields()->GetItem("lttp")->ActualSize;
m_nFileLen = (DWORD)lDataSize;
if(lDataSize > 0)
{
_variant_t varBLOB;
varBLOB = pRecordset->GetFields()->GetItem("lttp")->GetChunk(lDataSize);
//把二进制格式的图片转为图片格式
char * m_pJPGBuffer;
IStream * pStream;
IPicture * pPicture;
try
{
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(m_pJPGBuffer = new char[lDataSize+1])
{
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(m_pJPGBuffer,pBuf,lDataSize);
SafeArrayUnaccessData (varBLOB.parray);
m_nFileLen = lDataSize;
//m_pJPGBuffer -> pPicture
HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, m_nFileLen );
LPVOID lpBuf = ::GlobalLock( hMem );
memcpy(lpBuf,m_pJPGBuffer,m_nFileLen);
::GlobalUnlock( hMem );
CreateStreamOnHGlobal( hMem, TRUE, &pStream );
OleLoadPicture( pStream, m_nFileLen, TRUE, IID_IPicture, ( LPVOID * )&pPicture );
////////////////////////图片另存为//////////////////////////////////
CString ljname;//路径名
//提取文件名
CFileDialog opendlg (FALSE,_T("*"),jsnr.Right(jsnr.GetLength()-jsnr.ReverseFind(':')-1),OFN_OVERWRITEPROMPT, _T("所有文件(*.*;)|*.*||"),this);
if (opendlg.DoModal()==IDOK)
{
ljname=opendlg.GetPathName();
CFile destFile(ljname, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
//写文件
destFile.Write(pBuf, m_nFileLen); //Write it
destFile.Close();
//MessageBox(_T("图片已保存!"));
/////////////////////
}
catch(...)
{
AfxMessageBox(_T("从数据库中读取jpg图像出错!"));

return;
}

}
/////////////////////////////////
}
pRecordset->MoveNext();///移到下一条记录
}
}
catch(...)
{
AfxMessageBox(_T("数据库读取失败"));

}
相似回答