CDlgOracleImageRWDlg.cpp
资源名称:08.zip [点击查看]
上传用户:ynjin1970
上传日期:2014-10-13
资源大小:6438k
文件大小:16k
源码类别:
中间件编程
开发平台:
Visual C++
- // CDlgOracleImageRWDlg.cpp : implementation file
- #include "stdafx.h"
- #include "CDlgOracleImageRW.h"
- #include "CDlgOracleImageRWDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // 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()
- /////////////////////////////////////////////////////////////////////////////
- // CCDlgOracleImageRWDlg dialog
- CCDlgOracleImageRWDlg::CCDlgOracleImageRWDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CCDlgOracleImageRWDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCDlgOracleImageRWDlg)
- m_ID = _T("");
- m_Name = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- //获得主框架指针
- pMainWnd=AfxGetMainWnd();
- //给变量赋初值
- m_bModify = FALSE;
- m_hPhotoBitmap = NULL;
- m_pBMPBuffer = NULL;
- }
- void CCDlgOracleImageRWDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCDlgOracleImageRWDlg)
- DDX_Text(pDX, IDC_ID, m_ID);
- DDX_Text(pDX, IDC_Name, m_Name);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCDlgOracleImageRWDlg, CDialog)
- //{{AFX_MSG_MAP(CCDlgOracleImageRWDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_PREVIOUSUSER, OnPrevioususer)
- ON_BN_CLICKED(IDC_NEXTUSER, OnNextuser)
- ON_BN_CLICKED(IDC_SAVEINFO, OnSave)
- ON_BN_CLICKED(IDC_ADDUSER, OnAddPhoto)
- ON_BN_CLICKED(IDC_DELETEUSER, OnDeletePhoto)
- ON_BN_CLICKED(IDC_SELECTPHOTO, OnSelectphoto)
- ON_WM_PAINT()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCDlgOracleImageRWDlg message handlers
- BOOL CCDlgOracleImageRWDlg::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
- //连接数据库
- odb.Open("blobdb", "scott", "tiger");
- //打开记录集
- odyn.Open(odb, "SELECT * FROM IMAGE ");
- //检查记录集是否打开
- if (!odyn.IsOpen())
- {
- CString err;
- CString err1;
- err.Format("%s", (char*)(odb.GetErrorText()) );
- err1.Format("%s", (char*)(odb.GetErrorText()) );
- AfxMessageBox(err);
- AfxMessageBox(err1);
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CCDlgOracleImageRWDlg::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 CCDlgOracleImageRWDlg::OnPaint()
- {
- CPaintDC dc(this);
- 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
- {
- //将图像显示在对话框控件中
- DrawUserPhoto(50,30,&dc);
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CCDlgOracleImageRWDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CCDlgOracleImageRWDlg::OnPrevioususer()
- {
- // TODO: Add your control notification handler code here
- // 检查记录集中是否有记录
- if(odyn.GetRecordCount()==0)
- {
- AfxMessageBox("记录集中没有图片!请输入");
- return;
- }
- //移到下一个记录
- odyn.MovePrev();
- //是否到首记录标志处
- if(odyn.IsBOF())
- odyn.MoveFirst();//移到第一个记录
- //从Oracle数据库中读出像片数据
- ReadData();
- Invalidate();
- }
- void CCDlgOracleImageRWDlg::OnNextuser()
- {
- // TODO: Add your control notification handler code here
- // 检查记录集中是否有记录
- if(odyn.GetRecordCount()==0)
- {
- AfxMessageBox("记录集中没有图片!请输入");
- return;
- }
- //移到下一个记录
- odyn.MoveNext();
- //是否到末记录标志处
- if(odyn.IsEOF())
- odyn.MoveLast();
- ReadData();
- //从Oracle数据库中读出像片数据
- Invalidate();
- }
- void CCDlgOracleImageRWDlg::OnSave()
- {
- // TODO: Add your control notification handler code here
- m_bNewImage = true;
- if(!m_bModify) return;
- UpdateData();
- if( m_Name == "")
- {
- AfxMessageBox("您没有提供完整的用户信息。rn这些信息包括:用户名、年龄、照片");
- return;
- }
- //指向缓存的指针
- char *pBuf = m_pBMPBuffer;
- //定义一个结果对象,返回值0为失败,1为成功
- oresult ores;
- //移动到上一个记录
- ores = odyn.MoveLast();
- //定义一个OValue值对象
- OValue val;
- //对象置空值
- val.SetEmpty();
- //准备添加一个记录
- odyn.AddNewRecord();
- //把ID号值放到第一个字段
- odyn.SetFieldValue("ID", m_ID);
- //把名字放到第二个字段
- ores=odyn.SetFieldValue("NAME", m_Name);
- //把空值放到DATA字段
- ores = odyn.SetFieldValue((const char *)"DATA", val);
- //更新记录集
- ores = odyn.Update();
- //移动到上一个字段,此值非常重要,否者会造成属性值和BLOB图像数据的错位
- ////////////////////////////////////////////////////////////////////
- odyn.MoveLast();
- ////////////////////////////////////////////////////////////////////
- //定义一个OBlob对象
- OBlob oblob;
- //开始编辑记录集
- odyn.StartEdit();
- //把oblob对象定位到OBLOB字段上
- odyn.GetFieldValue("DATA", &oblob);
- //定义一个缓冲指针
- unsigned char *buffer = 0;
- try
- {
- // 计算一个合适的缓冲值大小
- unsigned long optchunk = oblob.GetOptimumChunkSize();
- //在分配中开辟缓冲区;用分块循环的方法可以减小开辟大块内存
- unsigned int bufsize = ((int)(32768/optchunk)) *optchunk;
- //用分块循环的方法可以减小开辟大块内存
- buffer = (unsigned char *)malloc(bufsize);
- ////定义一个fstream 对象
- fstream fs;
- //打开一个图像文件
- fs.open(pathname, ios::in);
- //设置模式
- fs.setmode(filebuf::binary);
- //移动指针到文件末尾
- fs.seekg(0, ios::end);
- //计算文件长度
- unsigned long filesize = fs.tellg();
- //回到文件指针头
- fs.seekg(0, ios::beg);
- //定义一个变量,计算一共书写的字节数
- unsigned long totalwritten = 0;
- //定义一个变量,计算每次读的字节数
- unsigned long amtread = 0;
- //第一块读取的OBLOB字段标记
- int piecetype = OLOB_FIRST_PIECE;
- //分块读入的方法可以充分利用数据流的优势,不用开很大的内存
- if (filesize <= bufsize)
- //如果文件数据字节数小于一块内存,只需一次读入
- piecetype = OLOB_ONE_PIECE;
- else
- //如果文件数据字节数大于一块内存,设置文件数据流读入方式
- oblob.EnableStreaming(filesize);
- while(totalwritten != filesize)
- {
- //从文件数据中读入一块缓冲块大小的内存
- fs.read(buffer, bufsize);
- //计算读入的数据字节数
- amtread = fs.gcount();
- //一次写一块缓冲块大小的数据到OBLOB对象中
- oblob.Write(buffer, amtread, piecetype);
- //计算总共书写的数据字节数
- totalwritten = totalwritten + amtread;
- //从文件数据中读入一块缓冲块大小的内存
- if ((filesize - totalwritten)<=bufsize)
- piecetype = OLOB_LAST_PIECE;
- else
- piecetype = OLOB_NEXT_PIECE;
- }
- //结束流数据的输入
- oblob.DisableStreaming();
- //更新记录集
- ores = odyn.Update();
- //关闭流文件
- fs.close();
- }
- //异常处理
- catch(OException E)
- {
- CString err;
- CString err1;
- err.Format("%s", (char*)(E.GetFailedMethodName()) );
- err1.Format("%s", (char*)(E.GetErrorText()) );
- AfxMessageBox(err1);
- }
- //消除buffer指针
- if (buffer)
- free(buffer);
- }
- void CCDlgOracleImageRWDlg::DestroyPhoto()
- {
- //图像句柄是否为空
- if(m_hPhotoBitmap)
- {
- //如果非空删除
- DeleteObject(m_hPhotoBitmap);
- //图像句柄置空
- m_hPhotoBitmap = NULL;
- }
- //指向图像内存指针是否为空
- if(m_pBMPBuffer)
- {
- //如果非空,删除
- delete m_pBMPBuffer;
- //指向图像内存指针置空
- m_pBMPBuffer = NULL;
- }
- }
- void CCDlgOracleImageRWDlg::OnAddPhoto()
- {
- // TODO: Add your control notification handler code here
- //装载选择图像
- LoadBMP();
- }
- void CCDlgOracleImageRWDlg::OnDeletePhoto()
- {
- // TODO: Add your control notification handler code here
- UpdateData(false);
- //关闭打开的记录集
- odyn.Close();
- //准备SQL语句
- CString strQuery;
- strQuery.Format("Select * From IMAGE Where ID = '%s'", m_ID);
- //打开满足条件的记录集
- odyn.Open(odb, strQuery,ODYNASET_DEFAULT);
- //删除满足条件的记录
- odyn.DeleteRecord();
- //关闭打开的记录集
- odyn.Close();
- //重新打开记录集
- odyn.Open(odb, "SELECT * FROM IMAGE ");
- }
- void CCDlgOracleImageRWDlg::OnSelectphoto()
- {
- // TODO: Add your control notification handler code here
- //装载选择图像
- LoadBMP();
- }
- BOOL CCDlgOracleImageRWDlg::LoadBMPFile(const char* pBMPPathname)
- {
- CFile file;
- //打开系统文件上的图像
- if( !file.Open( pBMPPathname, CFile::modeRead) )
- return FALSE;
- //取得文件长度
- m_nFileLen = file.GetLength();
- //开辟图像文件系统缓冲内存
- m_pBMPBuffer = new char[m_nFileLen + 1];
- if(!m_pBMPBuffer)
- return FALSE;
- //读图像文件
- if(file.ReadHuge(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
- return FALSE;
- return TRUE;
- }
- HBITMAP CCDlgOracleImageRWDlg::BufferToHBITMAP()
- {
- //BITMAP图像相关定义
- HBITMAP hBmp;
- LPSTR hDIB,lpBuffer = m_pBMPBuffer;
- LPVOID lpDIBBits;
- BITMAPFILEHEADER bmfHeader;
- DWORD bmfHeaderLen;
- //BITMAP图像文件头长度
- bmfHeaderLen = sizeof(bmfHeader);
- //把内存中数据拷贝到图像数据所指的记录象素数据的指针
- strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen);
- //判断是否是一幅BMP图像
- if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;
- hDIB = lpBuffer + bmfHeaderLen;
- BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- //指针移位至图像数据所指的记录象素数据的位置
- lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
- //从头文件中获取信息
- width= bmInfo.bmiHeader.biWidth;
- height =bmInfo.bmiHeader.biHeight;
- //获取当前客户区指针
- CClientDC dc(this);
- //生成BITMAP文件
- hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
- return hBmp;
- }
- void CCDlgOracleImageRWDlg::DrawUserPhoto(int x, int y, CDC *pDC)
- {
- if(!m_hPhotoBitmap) return;
- HBITMAP OldBitmap;
- CDC MemDC;
- MemDC.CreateCompatibleDC(pDC);
- OldBitmap=(HBITMAP)MemDC.SelectObject(m_hPhotoBitmap);
- //获得ID号为IDC_DRAWBOX的控件的CWnd指针
- //并使用它获得相应的设备环境
- CWnd *pWnd = GetDlgItem(IDC_Picture);
- CDC *pDC1 = pWnd->GetDC();
- CRect conRect;
- //获得该控件的矩形区域
- ::GetClientRect(pWnd->m_hWnd, conRect);
- //将索引值为NULL_BRUSH库存对象选进设备环境
- pDC1->SelectStockObject(NULL_BRUSH);
- //设置视口原点
- pDC1->SetViewportOrg(conRect.left, conRect.top);
- // CBitmap *pbitmap=new CBitmap;///////////// no use
- // BITMAP *m_bitmap=new BITMAP;/////////// no use
- // pbitmap->FromHandle(OldBitmap)->GetBitmap(m_bitmap);/////// no use
- int conW=conRect.right-conRect.left;
- int conH=conRect.bottom-conRect.top;
- // int w=m_bitmap->bmWidth;
- // int h=m_bitmap->bmHeight;
- pDC1->StretchBlt(0,0,conW,conH,&MemDC,0,0,width,height,SRCCOPY);
- MemDC.SelectObject(OldBitmap);
- pWnd->ReleaseDC(pDC1);
- // delete m_bitmap;///////////// no use
- // delete pbitmap;///////////// no use
- /////////////////////////////////////////////////////////////////////////////////
- }
- void CCDlgOracleImageRWDlg::ResetControls()
- {
- //是否是记录集的第一个记录
- BOOL IsFirstRecord =odyn.IsFirst();
- //是否是记录集的第二个记录
- BOOL IsLastRecord = odyn.IsLast();
- }
- void CCDlgOracleImageRWDlg::ReadData()
- {
- //消除图像指针,确保缓存为空
- DestroyPhoto();
- if (!odyn.IsOpen())
- {
- CString err;
- CString err1;
- err.Format("%s", (char*)(odb.GetErrorText()) );
- err1.Format("%s", (char*)(odb.GetErrorText()) );
- AfxMessageBox(err);
- AfxMessageBox(err1);
- }
- ////定义两个变量,保存从Oracle数据库中读出的和两个表字段(ID和NAME)相匹配的字段数据
- char cval1[30];
- char cval2[30];
- //定义一个OBlob 对象
- OBlob oblob;
- //得到ID字段值
- odyn.GetFieldValue("ID", cval1,30);
- //得到NAME字段值
- odyn.GetFieldValue("NAME", cval2,30);
- //把字段值赋给对话框中相应变量
- m_ID=cval1;m_Name=cval2;
- //取得DATA字段定位指针
- odyn.GetFieldValue("DATA", &oblob);
- //定义缓冲区指针
- unsigned char *buffer = 0;
- try
- {
- //定义数据流对象
- fstream fs;
- //在文件系统上设定一个文件
- fs.open("D:\imageou.bmp", ios::out);
- //设置模式
- fs.setmode(filebuf::binary);
- //取得数据流对象数据合适缓冲值大小
- unsigned long size = oblob.GetSize();
- // 计算合适的缓冲值大小
- unsigned long optchunk = oblob.GetOptimumChunkSize();
- unsigned int bufsize = ((int)(32768/optchunk)) *optchunk;
- //比较两个缓冲值,取较小的一个
- if (bufsize > size)
- bufsize = size;
- //开辟缓冲区;用分块循环的方法可以减小开辟大块内存
- buffer = (unsigned char *)malloc(bufsize);
- //设置文件数据流读入方式
- oblob.EnableStreaming(size);
- //设置文件数据流读入状态
- short status= OLOB_NEED_DATA;
- //定义一个变量,计算每次读的字节数
- unsigned long amtread=0;
- while(status == OLOB_NEED_DATA)
- {
- //从Oracle数据库中读入一块缓冲块大小的数据
- amtread = oblob.Read(&status, buffer, bufsize);
- //向文件系统写入一块缓冲块大小的数据
- fs.write(buffer, amtread);
- }
- //结束流数据的输入
- oblob.DisableStreaming();
- //关闭流文件
- fs.close();
- CString pathname1 ="D:\imageou.bmp";
- //消除图像指针,确保缓存为空
- DestroyPhoto();
- //读入数据
- LoadBMPFile(pathname1);
- //生成BMP图像
- m_hPhotoBitmap = BufferToHBITMAP();
- Invalidate();
- }
- //异常处理
- catch(OException E)
- {
- CString err;
- CString err1;
- err.Format("%s", (char*)(E.GetFailedMethodName()) );
- err1.Format("%s", (char*)(E.GetErrorText()) );
- AfxMessageBox(err);
- AfxMessageBox(err1);
- }
- //消除buffer指针
- if (buffer)
- free(buffer);
- //更新控件变量的值
- UpdateData(false);
- }
- void CCDlgOracleImageRWDlg::OnDestroy()
- {
- //关闭数据库
- odb.Close();
- //关闭连接
- OShutdown();
- CDialog::OnDestroy();
- }
- void CCDlgOracleImageRWDlg::LoadBMP()
- {
- static char BASED_CODE szFilter[] = "BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*||";
- CFileDialog dlg(TRUE,"BMP",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
- if(dlg.DoModal() == IDOK)
- {
- //取得系统图像文件路径名
- pathname = dlg.GetPathName();
- DestroyPhoto();
- //读入图像
- LoadBMPFile(pathname);
- //生成BMP格式的图像
- m_hPhotoBitmap = BufferToHBITMAP();
- m_bModify = TRUE;
- Invalidate();
- }
- }