www.pudn.com > vc存取jpg图片到数据库.rar > oneDlg.cpp


// oneDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "one.h" 
#include "oneDlg.h" 
#include "Picture.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
extern COneApp theApp;//james??? 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// COneDlg dialog 
 
COneDlg::COneDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(COneDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(COneDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void COneDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(COneDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(COneDlg, CDialog) 
	//{{AFX_MSG_MAP(COneDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// COneDlg message handlers 
 
BOOL COneDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	//---------------------------------------------------- 
	try 
	{ 
		m_pRecordset.CreateInstance("ADODB.Recordset"); 
		m_pRecordset->Open("SELECT * FROM jlab ORDER BY id",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	} 
 
	catch(_com_error e)///捕捉异常 
	{ 
		AfxMessageBox("读取数据库失败!");///显示错误信息 
	} 
	//----------------------------------------------------- 
 
 
 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void COneDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void COneDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR COneDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void COneDlg::OnButton1()   //-----------jpg存入库并显示------------- 
{ 
// TODO: Add your control notification handler code here 
	  CFile f; 
	  CString  FilePathName; 
	  CFileException e; 
 
      CFileDialog dlg(TRUE,NULL,NULL,0,"jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
      if(dlg.DoModal()==IDOK) 
      {FilePathName=dlg.GetPathName(); 
 
	if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... 
 
	if(f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件 
{	 
 
		int nSize = f.GetLength();          //先得到jpg文件长度 
		BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存 
	  
		if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请一块内存) 
		{   // +---------------------------------------------- 
						BYTE *pBuf = pBuffer;     ///下面这一大段是把pBuffer里的jpg数据放到库中 
						VARIANT			varBLOB; 
						SAFEARRAY		*psa; 
					    SAFEARRAYBOUND	rgsabound[1]; 
				 
						m_pRecordset->AddNew();   
							 
						if(pBuf) 
							{     
							rgsabound[0].lLbound = 0; 
							rgsabound[0].cElements = nSize; 
							psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
							for (long i = 0; i < (long)nSize; i++) 
							SafeArrayPutElement (psa, &i, pBuf++); 
							varBLOB.vt = VT_ARRAY | VT_UI1; 
							varBLOB.parray = psa; 
							m_pRecordset->GetFields()->GetItem("j")->AppendChunk(varBLOB); 
							} 
						m_pRecordset->Update(); 
						 
			// +---------------------------------------------- 
 
 
			(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 
			delete [] pBuffer;     //删掉堆上申请的那一块内存 
			pBuf=0;                //以防二次乱用 
		} 
		f.Close(); 
	 
	 
 
} 
  
		CClientDC dc(this);         
		m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels 
		m_Pic.Show(&dc, CRect(200,0,200+m_Pic.m_Width,m_Pic.m_Height) );//显示出来看看 
		 
		 
	  }	 
	   
	 
	 
} 
 
void COneDlg::OnButton2()   //----------------从库中提取并显示------------ 
{ 
	try 
	{ 
		long nSize = m_pRecordset->GetFields()->GetItem("j")->ActualSize; 
		if(nSize > 0) 
		{ 
			_variant_t	varBLOB; 
			varBLOB = m_pRecordset->GetFields()->GetItem("j")->GetChunk(nSize); 
			if(varBLOB.vt == (VT_ARRAY | VT_UI1)) 
			{ 
				if(BYTE *pBuffer = new BYTE [nSize+1])		///重新申请必要的存储空间 
				{	 
					char *pBuf = NULL; 
					SafeArrayAccessData(varBLOB.parray,(void **)&pBuf); 
					memcpy(pBuffer,pBuf,nSize);				///复制数据到缓冲区m_pBMPBuffer 
					SafeArrayUnaccessData (varBLOB.parray); 
				//	int nSize = lDataSize; 
					(m_Pic.LoadPictureData(pBuffer, nSize)); 
 
					 delete [] pBuffer; 
					 pBuf=0; 
					  
					  
					 ///生成BITMAP对象 
					 CClientDC dc(this); 
					 m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels 
					 m_Pic.Show(&dc, CRect(0,50,m_Pic.m_Width,50+m_Pic.m_Height) ); 
				} 
			} 
		} 
	} 
	catch (_com_error e) { 
	} 
		 
} 
 
 
 
 
void COneDlg::OnButton3()  
{ 
	try{ 
		m_pRecordset->MovePrevious();	//选上一个jpg 
		OnButton2(); 
	} 
	catch (_com_error e) { 
	} 
} 
 
 
 
void COneDlg::OnButton4()  
{ 
	try{ 
		m_pRecordset->MoveNext();       //选下一个jpg 
		OnButton2(); 
	} 
	catch (_com_error e) { 
	}	 
}