【框架-MFC】CObject 继承对象的序列化存储与读取

来源:互联网 发布:mac散热器声音大怎么办 编辑:程序博客网 时间:2024/06/04 23:33

头文件

#pragma once#define IPL_DEPTH_24U   24#define IPL_DEPTH_20U   20class C24SCT: public CObject {public:C24SCT(void);~C24SCT(void);DECLARE_SERIAL(C24SCT)  private:CString strFilePath;  //保存文件的路径long  nSize;        //C24SCT结构大小    int  nChannels;    //图像通道数量    int  nDepth;        //图像位深    int  nWidth;        //图像宽度    int  nHeight;       //图像高度int  nVersion;//图像版本    char *pImageData; public://Attributeslong Size();int Width();int Height();int Depth();int Version();CString GetPath();void SetPath(CString filePath);public://Methodvoid Read();void Write();void New(int height ,int width); C24SCT& operator= (C24SCT & obj ) ;};
实现文件
#include "StdAfx.h"#include "C24SCT.h"IMPLEMENT_SERIAL(C24SCT, CObject, 1) C24SCT::C24SCT(void){nWidth =0 ;nHeight = 0;nDepth = 24;nVersion = 2016;pImageData = NULL;nChannels =1;}C24SCT::~C24SCT(void){if (pImageData!=NULL){//delete [] imageData;pImageData= NULL;}nWidth =0 ;nHeight = 0;}void C24SCT::Read(){CFile file;CFileException fe;//以读方式打开文件if(!file.Open(strFilePath,CFile::modeRead,&fe)){fe.ReportError();return;}//构建CArchive 对象CArchive ar(&file,CArchive::load);ar >>nWidth>>nHeight>>nDepth>>nVersion;CString str;str.Format(_T("width  %d  height  %d\n"),nWidth,nHeight);OutputDebugString(str);New(nHeight ,nWidth);for(DWORD i = 0; i < nWidth*nHeight*3; i++)ar>>(pImageData[i]);ar.Flush();//读完毕,关闭文件流ar.Close();file.Close();}void C24SCT::Write(){CFile file;CFileException fe;//以读方式打开文件if(!file.Open(strFilePath,CFile::modeWrite|CFile::modeCreate,&fe)){fe.ReportError();return;}//构建CArchive 对象CArchive ar(&file,CArchive::store);ar <<nWidth<<nHeight<<nDepth<<nVersion;CString str;str.Format(_T("width  %d  height  %d"),nWidth,nHeight);OutputDebugString(str);for(DWORD i = 0; i < nWidth*nHeight*3; i++)ar<<(pImageData[i]);ar.Flush();//写完毕,关闭文件流ar.Close();file.Close();}void C24SCT::New( int height ,int width ){if (pImageData!=NULL){delete []pImageData;pImageData=NULL;}nHeight = height;nWidth = width;CString str;str.Format(_T("width  %d  height  %d"),height,width);OutputDebugString(str);pImageData = new char[height*width*3];}long C24SCT::Size(){long nFilePathSize  =0 ;if (strFilePath.GetLength() == 0){nFilePathSize = sizeof(CString);}else{nFilePathSize = sizeof(TCHAR) * strFilePath.GetLength() ;}long pImageDataSize = 0 ;if(pImageData == NULL){pImageDataSize = sizeof(pImageData);}else{pImageDataSize = nWidth*nHeight*3*sizeof(char);}nSize = sizeof(int)*5 + sizeof(long)+pImageDataSize+nFilePathSize;return nSize;}int C24SCT::Width(){return nWidth;}int C24SCT::Height(){return nHeight;}int C24SCT::Depth(){return nDepth;}int C24SCT::Version(){return nVersion;}C24SCT& C24SCT::operator= (C24SCT &obj ) {SetPath(obj.GetPath());return *this;}CString C24SCT::GetPath(){return strFilePath;}void C24SCT::SetPath(CString filePath ){strFilePath = filePath;}

调用-写
C24SCT new24file;new24file.New(width,height);new24file.SetPath(L"C:\\tmp.tmp");new24file.Write();
调用-读

C24SCT new24file;new24file.SetPath(L"C:\\tmp.tmp");new24file.Read();




0 0
原创粉丝点击