Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Tamila  
#1 Posted : Monday, July 14, 2008 4:20:10 PM(UTC)
Tamila

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

Aurigma Support Team

UserPostedImage Follow Aurigma on Twitter!
HabaHaba  
#2 Posted : Saturday, September 13, 2008 6:56:25 AM(UTC)
HabaHaba

Rank: Member

Groups: Member
Joined: 9/7/2008(UTC)
Posts: 19

What about classes derived from PathVObject? For ellipse or rectangle I can call PathVObject.Path.GetBounds() and save in xml. But for line this information is not enough :( What do you can suggest in this case?

Tamila  
#3 Posted : Wednesday, September 17, 2008 8:18:14 PM(UTC)
Tamila

Rank: Advanced Member

Groups: Member
Joined: 3/9/2008(UTC)
Posts: 554

Was thanked: 1 time(s) in 1 post(s)
Hi,

For LineVObject and other VObjects you can use the same method using Reflection for getting nessesary information for serialize.

This code sample allows to get information about VObjects:
Code:

'===========For Line=========================
Dim obj As New Aurigma.GraphicsMill.WinControls.LineVObject(10, 10, _
20, 20)

Dim pathInfo As System.Reflection.PropertyInfo
Dim linePath As System.Drawing.Drawing2D.GraphicsPath

pathInfo = obj.GetType().GetProperty("Path", _
Reflection.BindingFlags.NonPublic _
Or Reflection.BindingFlags.DeclaredOnly Or Reflection.BindingFlags.Instance)
           
linePath = CType(pathInfo.GetValue(obj, Nothing), _
System.Drawing.Drawing2D.GraphicsPath)

Dim pnt0 As PointF = linePath.PathPoints(0)
Dim pnt1 As PointF = linePath.PathPoints(1)

'===========For Rectangle=====================
Dim rectObj As New Aurigma.GraphicsMill.WinControls.RectangleVObject(10,_
10, 80, 40)

Dim rectPathInfo As System.Reflection.PropertyInfo
Dim rectPath As System.Drawing.Drawing2D.GraphicsPath

rectPathInfo = rectObj.GetType().GetProperty("Path", _
Reflection.BindingFlags.NonPublic _
Or Reflection.BindingFlags.DeclaredOnly Or Reflection.BindingFlags.Instance)
rectPath = CType(rectPathInfo.GetValue(rectObj, Nothing), _
System.Drawing.Drawing2D.GraphicsPath)

Dim pnt00 As PointF = rectPath.PathPoints(0)
Dim pnt01 As PointF = rectPath.PathPoints(1)
Dim pnt02 As PointF = rectPath.PathPoints(2)
Dim pnt03 As PointF = rectPath.PathPoints(3)

'===========For Freehand======================
Dim fHandObj As New Aurigma.GraphicsMill.WinControls.FreehandVObject( _
this, False, Drawing2D.FillMode.Winding)

Dim fHandPathInfo As System.Reflection.PropertyInfo
Dim retrievedPoints As System.Drawing.PointF()

fHandPathInfo = fHandObj.GetType().GetProperty("Points", _
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.DeclaredOnly _
Or Reflection.BindingFlags.Instance)
retrievedPoints = CType(fHandPathInfo.GetValue(fHandObj, Nothing), _
System.Drawing.PointF()) 'This array stores all freehand points inside.
        
'===========For Polyline======================
Dim polylineObj As New Aurigma.GraphicsMill.WinControls.PolylineVObject(_
this, False, Drawing2D.FillMode.Winding)
'polylineObj.Points - this property contains all nodes of the polyline

Now you can use the same method for serialize like for ImageVObject.
Aurigma Support Team

UserPostedImage Follow Aurigma on Twitter!
cpav  
#4 Posted : Saturday, September 27, 2008 8:12:30 AM(UTC)
cpav

Rank: Advanced Member

Groups: Member
Joined: 12/17/2007(UTC)
Posts: 49

I just want to add that even if you use your own Vobject, like a TextVobject with background color for example, you just need to store and and read in .xml that extra thing, for example the color you use. I have implemented my self(with the usufull help of support also), 3 custom Vobjects based on default GM Vobjects and i just want to mention it works perfect, no matter what version of GM or VS you have.
ChingYen  
#5 Posted : Wednesday, December 2, 2009 3:12:15 PM(UTC)
ChingYen

Rank: Advanced Member

Groups: Member
Joined: 3/3/2008(UTC)
Posts: 185

Thanks: 8 times
Hi,

Thanks for the great example. Can we have the sample for TextObject? I manage to solve most of the issue... but, stuck @ the place that if the user "drag and drop" to resize the text, then basically how can get those "enlarged" text?

Please advice.
Tamila  
#6 Posted : Thursday, December 3, 2009 8:37:03 PM(UTC)
Tamila

Rank: Advanced Member

Groups: Member
Joined: 3/9/2008(UTC)
Posts: 554

Was thanked: 1 time(s) in 1 post(s)
Hi,

This is the code sample for TextVObject:
Code:
'===========For textVObject======================
Dim textObj As New Aurigma.GraphicsMill.WinControls.TextVObject("test", _
"Arial", 12, New System.Drawing.RectangleF(0, 0, 30, 10))

Dim textString As System.Reflection.FieldInfo = textObj.GetType().GetField("_text", _
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.DeclaredOnly Or _
Reflection.BindingFlags.Instance)
Dim text As String = CType(textString.GetValue(textObj), String)

Dim textFont As System.Reflection.FieldInfo = textObj.GetType().GetField("_font", _
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.DeclaredOnly Or _
Reflection.BindingFlags.Instance)
Dim font As System.Drawing.Font = CType(textFont.GetValue(textObj), System.Drawing.Font)

Dim textFormat As System.Reflection.FieldInfo = textObj.GetType().GetField("_format", _
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.DeclaredOnly Or _
Reflection.BindingFlags.Instance)
Dim format As System.Drawing.StringFormat = CType(textFormat.GetValue(textObj), _
System.Drawing.StringFormat)

Dim textArea As System.Reflection.FieldInfo = textObj.GetType().GetField("_textArea", _
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.DeclaredOnly Or _
Reflection.BindingFlags.Instance)
Dim area As System.Drawing.RectangleF = CType(textArea.GetValue(textObj), _
System.Drawing.RectangleF) 


Aurigma Support Team

UserPostedImage Follow Aurigma on Twitter!
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.