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

头文件

#pragma once

#define IPL_DEPTH_24U   24
#define IPL_DEPTH_20U   20

class 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://Attributes
	long Size();
	int Width();
	int Height();
	int Depth();
	int Version();
	CString GetPath();
	void SetPath(CString filePath);
public://Method
	void 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值