Rank: Advanced Member
Groups: Member
Joined: 3/9/2008(UTC) Posts: 554
Was thanked: 1 time(s) in 1 post(s)
|
I am using Vector Objects in my project, and I have saved the design using standard serialization mechanism. But after I updated Aurigma.GraphicsMill.WinControls.VectorObjects.dll, I could not deserialized the artwork that I did before.Standard methods Serialize and Deserialize are intended to be used with Undo/Redo operations. Because of binary format used for serialization is highly dependent on the build version, as the API may change from version to version, and properties may be added or removed. So if you need to save VObjects for a long time period I propose to use alternative method. I recommend to use XML format allowing to save nessesary information on VObject instances. Let us consider ImageVObject as an example. Code:
Public Sub ImageVObjectSerialize(ByVal imageVObj As _
Aurigma.GraphicsMill.WinControls.ImageVObject, _
ByVal filename As String)
'Get values of _name, _image, and _scaleToActualSize
'members using Reflection
Dim imageInfo As System.Reflection.FieldInfo = _
imageVObj.GetType().GetField("_image", _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.DeclaredOnly Or _
System.Reflection.BindingFlags.Instance)
Dim image As Aurigma.GraphicsMill.Bitmap = _
CType(imageInfo.GetValue(imageVObj), _
Aurigma.GraphicsMill.Bitmap)
Dim scaleInfo As System.Reflection.FieldInfo = _
imageVObj.GetType().GetField("_scaleToActualSize", _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.DeclaredOnly Or _
System.Reflection.BindingFlags.Instance)
Dim scaleToActualSize As Boolean = CType(_
scaleInfo.GetValue(imageVObj), Boolean)
Dim imageVObjName As String = imageVObj.Name
'ImageVObject serialization
bitmap.Save(filename & ".tif")
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.OmitXmlDeclaration = False
settings.Encoding = System.Text.Encoding.UTF8
Dim interimStream As New System.IO.FileStream(filename,_
FileMode.Create)
Try
Dim writer As XmlWriter = XmlWriter.Create(interimStream, _
settings)
writer.WriteStartElement("objects")
writer.WriteStartElement("ImageVObject")
writer.WriteElementString("image", filename & ".tif")
writer.WriteElementString("name", imageVObjName)
writer.WriteElementString("scaleToActualSize", _
scaleToActualSize.ToString)
writer.WriteStartElement("matrix")
writer.WriteElementString("a11", _
imageVObj.Transform.Elements(0).ToString())
writer.WriteElementString("a12", _
imageVObj.Transform.Elements(1).ToString())
writer.WriteElementString("a21", _
imageVObj.Transform.Elements(2).ToString())
writer.WriteElementString("a22", _
imageVObj.Transform.Elements(3).ToString())
writer.WriteElementString("dx", _
imageVObj.Transform.Elements(4).ToString())
writer.WriteElementString("dy", _
imageVObj.Transform.Elements(5).ToString())
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
Catch
interimStream.Close()
Throw
End Try
interimStream.Close()
End Sub
Public Function ImageVObjectDeserialize(ByVal filename As String) As _
Aurigma.GraphicsMill.WinControls.ImageVObject
Dim bitmapFileName, imageName, scaleString As String
Dim matrixElements(6) As Single
'Deserialize of ImageVObject
If (filename = Nothing) Then
Throw New System.ArgumentNullException("filename")
End If
Dim reader As XmlReader = XmlReader.Create(filename)
reader.ReadStartElement("objects")
reader.Read()
If Not reader.IsStartElement() Or _
reader.Name <> "ImageVObject" Then
Throw New ApplicationException("Wrong XML file format")
Else
reader.Read()
End If
While (True)
reader.Read()
Select Case (reader.Name)
Case "image"
bitmapFileName = reader.ReadElementString()
Case "name"
imageName = reader.ReadElementString()
Case "scaleToActualSize"
scaleString = reader.ReadElementString()
Case "matrix"
reader.Read()
matrixElements = ReadMatrix(reader)
Case Else
Exit While
End Select
End While
'Create new ImageVObject using data from .xml
Dim bmp As New Aurigma.GraphicsMill.Bitmap(bitmapFileName)
Dim imageVObject As New _
Aurigma.GraphicsMill.WinControls.ImageVObject( _
bmp, Boolean.Parse(scaleString), 0, 0)
imageVObject.Name = imageName
imageVObject.Transform = New System.Drawing.Drawing2D.Matrix( _
matrixElements(0), _
matrixElements(1), _
matrixElements(2), _
matrixElements(3), _
matrixElements(4), _
matrixElements(5))
Return imageVObject
End Function
Private Function ReadMatrix(ByVal reader As XmlReader)
Dim result(5) As Single
While (True)
reader.Read()
Select Case (reader.Name)
Case "a11"
result(0) = Single.Parse( _
reader.ReadElementString())
Case "a12"
result(1) = Single.Parse( _
reader.ReadElementString())
Case "a21"
result(2) = Single.Parse( _
reader.ReadElementString())
Case "a22"
result(3) = Single.Parse( _
reader.ReadElementString())
Case "dx"
result(4) = Single.Parse( _
reader.ReadElementString())
Case "dy"
result(5) = Single.Parse( _
reader.ReadElementString())
Case Else
Exit While
End Select
End While
Return result
End Function
Edited by moderator Monday, May 28, 2012 8:27:40 PM(UTC)
| Reason: Not specified |