将AE对象序列化为二进制文件

将AE对象序列化为二进制文件

当我们编写AE程序时,通常会遇到需要存储某个AE对象的情况,

比如Layer,Element,Map,Legend,NorthArrow等等这些

举个例子说明一下:在我们编辑Featurelayer时,我们可以容易的将Feature存储在Featureclass中,

同样,如果我们向Graphicscontainer中添加了Element,我们也希望可以容易的存储Element

这样每次加载时可以将Element顺利显示出来,但是不巧的是,AE中并没有提供存储Element的方法

这个时候,我们就需要将Graphicscontainer序列化成文件,这样就可以达到存储的目的了

要知道,Featureclass存储成shapefile也好,Geodatabase也罢,都是一种文件的组织形式,也都是一种特殊意义上的序列化。

好,下面,介绍序列化的方法:

首先可以序列化的对象必须实现了IPersistStream接口,

其中IPersistStream接口是Windos中的接口,派生自 IPersist,并增加了4个函数,从流(IStream)中读写组件属性信息。

下面是各个函数的意义:

AE中用下面两个函数即可实现对象的序列化,说明已经写的很清楚了,一目了然啊。

这个是我做的序列化Layer的例子,大家可以下载使用(VB.Net)/Files/wall/TestESRIStream.rar

''' <summary>
''' 将AE中实现了IPersistStream接口的对象序列化为二进制文件
''' </summary>
''' <param name="pObject">对象</param>
''' <param name="pFilePath">文件名全路径(形如:“C:/file.blb”)</param>
''' <remarks></remarks>

Sub WriteObject(ByVal pFilePath As String, ByVal pObject As Object)
If Not TypeOf pObject Is IPersistStream Then
MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream
Dim pObjectStream As IObjectStream = New ObjectStream
pObjectStream.Stream = pMemoryBlobStream
Dim pPersistStream As IPersistStream = pObject
pPersistStream.Save(pObjectStream, True)
Try
pMemoryBlobStream.SaveToFile(pFilePath)
Catch ex As Exception
MessageBox.Show("序列化文件路径不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
''' <summary>
''' 从序列化文件中读取对象(反序列化)
''' </summary>
''' <param name="pObject">对象</param>
''' <param name="pFilePath">文件名全路径(形如:“C:/file.blb”)</param>
''' <remarks></remarks>
Sub ReadObject(ByVal pFilePath As String, ByRef pObject As Object)
If Not TypeOf pObject Is IPersistStream Then
MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If Not System.IO.File.Exists(pFilePath) Then
MessageBox.Show("序列化文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream
pMemoryBlobStream.LoadFromFile(pFilePath)
Dim pObjectStream As IObjectStream = New ObjectStream
pObjectStream.Stream = pMemoryBlobStream
Dim pPersistStream As IPersistStream = pObject
pPersistStream.Load(pObjectStream)
End Sub

来自:http://www.cnblogs.com/wall/archive/2009/02/26/1398447.html

IsDirty()

组件内部属性是否发生了变化。为调用者是否需要保存信息提供依据

Load()

IStream 中读入信息,初始化组件属性

Save()

把属性信息保存到 IStream

GetSizeMax()

返回信息尺寸,以便调用者事先开辟空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值