16,472
社区成员
发帖
与我相关
我的任务
分享
typedef struct
{
UINT uImageLen; unsigned char *pbyImage;
}PIC,*PPIC;
FILE* fp = fopen("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cc.jpg", "wb");
if (fp)
{
PPIC pPic;
fwrite(pPic->pbyImage, 1, pPic->uImageLen, fp);
fclose(fp);
}else
AfxMessageBox("error");
typedef struct
{
UINT uImageLen;
unsigned char *pbyImage;
}PIC,*PPIC;
FILE* fp = fopen("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cc.jpg", "wb");
if (fp)
{
PPIC pPic;
fseek(fp, 0, 2);
pPic->uImageLen = ftell(fp);
pPic->pbyImage = new unsigned char(pPic->uImageLen);
fwrite(pPic->pbyImage, 1, pPic->uImageLen, fp);
fclose(fp);
}else
AfxMessageBox("error");
读取:
1、从数据库字段中取得大二进制位图数据;
2、在system的temp目录创建改位图文件;
3、在对话框中利用StretchBit显示位图;
存储:
1、利用文件选择对话框选择位图文件;
2、显示;
3、点击插入后存储到数据库中
以下是取和存的函数,具体操作大二进制文件的方法,可具体参照一些文档,vckbase有的
//取出位图文件并保存于临时文件
CString CStlibPage3::OperateBitmap(CString m_strname)
{
CStandardSet* m_pStandard = new CStandardSet();
m_pStandard->Open();
if (m_pStandard->GetRecordCount()!=0)
m_pStandard->MoveFirst();
int tempnumber = 0;
CByteArray Array1;
while (!m_pStandard->IsEOF())
{
if(m_pStandard->m_StandardName == m_standard_name)
{
Array1.Copy(m_pStandard->m_StabitmapFile);
tempnumber = Array1.GetSize();
break;
}
m_pStandard->MoveNext();
}
m_pStandard->Close();
delete m_pStandard;
DWORD dwWritten;
//**********************************************
char temp_name[_MAX_PATH];
GetTempPath(_MAX_PATH,temp_name);
strcat(temp_name,"bitmap.tmp");
HANDLE fh;
for (int nIndex = 0; nIndex < tempnumber; nIndex++)
{
if (Array1.GetAt(nIndex) == 'B' && Array1.GetAt(nIndex + 1) == 'M'
&& Array1.GetAt(nIndex + 2) != 'P')
{
int HeaderLen = nIndex;// + sizeof(BITMAPFILEHEADER);
Array1.RemoveAt( 0, HeaderLen );
break;
}
}
LPBITMAPINFOHEADER bmiHeader = (LPBITMAPINFOHEADER)Array1.GetData() ;
fh = CreateFile(temp_name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
WriteFile(fh,(LPBYTE)bmiHeader,(DWORD)tempnumber,&dwWritten,NULL);
Array1.RemoveAll(); //Relese Memory
CloseHandle(fh);
return temp_name;
}
void CStlibPage3::Savebmp_Data()
{
CStandardSet* m_pStandard = new CStandardSet();
m_pStandard->Open();
CString tempid = "StandardName = '" + m_standard_name+"'";
if(m_pStandard->FindLast(tempid))
{
if (strBitmapPath == "")
{
if (!m_pStandard->CanUpdate()) return;
m_pStandard->Edit();
CString strupdate = "NULL";
m_pStandard->SetFieldValue(_T("[StabitmapFile]"),COleVariant(strupdate));
m_pStandard->Update();
goto ip;
}
CFile tempfile;
tempfile.Open(strBitmapPath,CFile::modeRead);
DWORD dwBufSize;
dwBufSize = tempfile.GetLength();
Array.SetSize(dwBufSize);
tempfile.ReadHuge((LPSTR)(Array.GetData()), dwBufSize);
tempfile.Close();
(m_pStandard->m_StabitmapFile).Copy(Array);
if (!m_pStandard->CanUpdate()) return;
m_pStandard->Edit();
m_pStandard->SetFieldValue(_T("[StabitmapFile]"),COleVariant(Array));
m_pStandard->Update();
}
ip:
m_pStandard->Close();
delete m_pStandard;
}
void CStlibPage3::DrawBitmap()
{
UpdateData(TRUE);
//draw bitmap
if (CStlibPage3::hpreview)
DeleteObject(CStlibPage3::hpreview);
CStlibPage3::hpreview = (HBITMAP)LoadImage(AfxGetInstanceHandle(),strBitmapPath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if (CStlibPage3::hpreview)
{
GetObject(CStlibPage3::hpreview,sizeof(BITMAP),&bm);
CSize m_sizeSource(bm.bmWidth,bm.bmHeight);
GetDlgItem(IDC_PIC_PREVIEW)->GetClientRect(&m_rectDest);
CClientDC pDC(GetDlgItem(IDC_PIC_PREVIEW));
HDC dcmem = CreateCompatibleDC(pDC);
HBITMAP old = (HBITMAP)SelectObject(dcmem,CStlibPage3::hpreview);
StretchBlt(pDC.m_hDC,m_rectDest.left, m_rectDest.top, m_rectDest.Width(), m_rectDest.Height(),dcmem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
//GetDlgItem(IDC_PIC_PREVIEW)->GetDC()
SelectObject(dcmem,old);
DeleteDC(dcmem);
}
}