Font serialization in vb.net
- by jovany
Hello all,
as the title says , I need to serialize my font.
I have tried the following approach unfortunately to no avail.
This is what I have and what happens;
I have a drawing application and certain variables and properties need to be serialized.
(So , Xml.Serialization has been used.)
Now this has already been done in a huge portion and I've created some other attributes which needed to be serialized and it works.
There is one base class and classes such as drawablestar, drawableeclipse ,etc. all inherit from this class. As does my drawabletextboxclass.
The base class is Serializable as can be seen in the sample below.
It looks like this...
Imports System.Xml.Serialization
<Serializable()> _
Public MustInherit Class Drawable
' Drawing characteristics.
'Font characteristics
<XmlIgnore()> Public FontFamily As String
<XmlIgnore()> Public FontSize As Integer
<XmlIgnore()> Public FontType As Integer
<XmlIgnore()> Public ForeColor As Color
<XmlIgnore()> Public FillColor As Color
<XmlAttributeAttribute()> Public LineWidth As Integer = 0
<XmlAttributeAttribute()> Public X1 As Integer
<XmlAttributeAttribute()> Public Y1 As Integer
<XmlAttributeAttribute()> Public X2 As Integer
<XmlAttributeAttribute()> Public Y2 As Integer
' attributes for size textbox
<XmlAttributeAttribute()> Public widthLabel As Integer
<XmlAttributeAttribute()> Public heightLabel As Integer
'<XmlTextAttribute()> Public FontFamily As String
'<XmlAttributeAttribute()> Public FontSize As Integer
'this should actually not be here..
<XmlAttributeAttribute()> Public s_InsertLabel As String
' Indicates whether we should draw as selected.
<XmlIgnore()> Public IsSelected As Boolean = False
' Constructors.
Public Sub New()
ForeColor = Color.Black
FillColor = Color.White
'FontFamily = "Impact"
'FontSize = 12
End Sub
Friend WriteOnly Property _Label() As String
Set(ByVal Value As String)
s_InsertLabel = Value
End Set
End Property
Public Sub New(ByVal fore_color As Color, ByVal fill_color As Color, Optional ByVal line_width As Integer = 0)
LineWidth = line_width
ForeColor = fore_color
FillColor = fill_color
' FontFamily = Font_Family
' FontSize = Font_Size
End Sub
' Property procedures to serialize and
' deserialize ForeColor and FillColor.
<XmlAttributeAttribute("ForeColor")> _
Public Property ForeColorArgb() As Integer
Get
Return ForeColor.ToArgb()
End Get
Set(ByVal Value As Integer)
ForeColor = Color.FromArgb(Value)
End Set
End Property
<XmlAttributeAttribute("BackColor")> _
Public Property FillColorArgb() As Integer
Get
Return FillColor.ToArgb()
End Get
Set(ByVal Value As Integer)
FillColor = Color.FromArgb(Value)
End Set
End Property
'Property procedures to serialize and
'deserialize Font
<XmlAttributeAttribute("InsertLabel")> _
Public Property InsertLabel_() As String
Get
Return s_InsertLabel
End Get
Set(ByVal value As String)
s_InsertLabel = value
End Set
End Property
<XmlAttributeAttribute("FontSize")> _
Public Property FontSizeGet() As Integer
Get
Return FontSize
End Get
Set(ByVal value As Integer)
FontSize = value
End Set
End Property
<XmlAttributeAttribute("FontFamily")> _
Public Property FontFamilyGet() As String
Get
Return FontFamily
End Get
Set(ByVal value As String)
FontFamily = value
End Set
End Property
<XmlAttributeAttribute("FontType")> _
Public Property FontType_() As Integer
Get
Return FontType
End Get
Set(ByVal value As Integer)
FontType = value
End Set
End Property
#Region "Methods to override"
Public MustOverride Sub Draw(ByVal gr As Graphics)
' Return the object's bounding rectangle.
Public MustOverride Function GetBounds() As Rectangle
...... ........
.....
End Class
[/code]
My textbox class which looks like this , is the one that needs to save it's font.
Imports System.Math
Imports System.Xml.Serialization
Imports System.Windows.Forms
<Serializable()> _
Public Class DrawableTextBox
Inherits Drawable
Private i_StringLength As Integer
Private i_StringWidth As Integer
Private drawFont As Font = New Font(FontFamily, 12, FontStyle.Regular)
Private brsTextColor As Brush = Brushes.Black
Private s_insertLabelTextbox As String = "label"
' Constructors.
Public Sub New()
End Sub
Public Sub New(ByVal objCanvas As PictureBox, ByVal fore_color As Color, ByVal fill_color As Color, Optional ByVal line_width As Integer = 0, Optional ByVal new_x1 As Integer = 0, Optional ByVal new_y1 As Integer = 0, Optional ByVal new_x2 As Integer = 1, Optional ByVal new_y2 As Integer = 1)
MyBase.New(fore_color, fill_color, line_width)
Dim objGraphics As Graphics = objCanvas.CreateGraphics()
X1 = new_x1
Y1 = new_y1
'Only rectangles ,circles and stars can resize for now b_Movement
b_Movement = True
Dim frm As New frmTextbox
frm.MyFont = drawFont
frm.ShowDialog()
If frm.DialogResult = DialogResult.OK Then
FontFamily = frm.MyFont.FontFamily.Name
FontSize = frm.MyFont.Size
FontType = frm.MyFont.Style
'drawFont = frm.MyFont
drawFont = New Font(FontFamily, FontSize)
drawFont = FontAttributes()
brsTextColor = New SolidBrush(frm.txtLabel.ForeColor)
s_InsertLabel = frm.txtLabel.Text
i_StringLength = s_InsertLabel.Length
'gefixtf
Dim objSizeF As SizeF = objGraphics.MeasureString(s_InsertLabel, drawFont, New PointF(X2 - X1, Y2 - Y1), New StringFormat(StringFormatFlags.NoClip))
Dim objPoint As Point = objCanvas.PointToClient(New Point(X1 + objSizeF.Width, Y1 + objSizeF.Height))
widthLabel = objSizeF.Width
heightLabel = objSizeF.Height
X2 = X1 + widthLabel
Y2 = Y1 + heightLabel
Else
Throw New ApplicationException()
End If
End Sub
' Draw the object on this Graphics surface.
Public Overrides Sub Draw(ByVal gr As System.Drawing.Graphics)
' Make a Rectangle representing this rectangle.
Dim rectString As Rectangle
rectString = New Rectangle(X1, Y1, widthLabel, heightLabel)
rectString = GetBounds()
' See if we're selected.
If IsSelected Then
gr.DrawString(s_InsertLabel, drawFont, brsTextColor, X1, Y1)
'gr.DrawRectangle(Pens.Black, rect) ' Pens.Transparent
gr.DrawRectangle(Pens.Black, rectString)
' Draw grab handles.
DrawGrabHandle(gr, X1, Y1)
DrawGrabHandle(gr, X1, Y2)
DrawGrabHandle(gr, X2, Y2)
DrawGrabHandle(gr, X2, Y1)
Else
gr.DrawString(s_InsertLabel, drawFont, brsTextColor, X1, Y1)
'gr.DrawRectangle(Pens.Black, rect) ' Pens.Transparent
gr.DrawRectangle(Pens.Black, rectString)
End If
End Sub
'get fontattributes
Public Function FontAttributes() As Font
Return New Font(FontFamily, 12, FontStyle.Regular)
End Function
' Return the object's bounding rectangle.
Public Overrides Function GetBounds() As System.Drawing.Rectangle
Return New Rectangle( _
Min(X1, X1), _
Min(Y1, Y1), _
Abs(widthLabel), _
Abs(heightLabel))
End Function
' Return True if this point is on the object.
Public Overrides Function IsAt(ByVal x As Integer, ByVal y As Integer) As Boolean
Return (x >= Min(X1, X2)) AndAlso _
(x <= Max(X1, X2)) AndAlso _
(y >= Min(Y1, Y2)) AndAlso _
(y <= Max(Y1, Y2))
End Function
' Move the second point.
Public Overrides Sub NewPoint(ByVal x As Integer, ByVal y As Integer)
X2 = x
Y2 = y
End Sub
' Return True if the object is empty (e.g. a zero-length line).
Public Overrides Function IsEmpty() As Boolean
Return (X1 = X2) AndAlso (Y1 = Y2)
End Function
End Class
The coordinates ( X1 ,X2,Y1, Y2 ) are needed to draw a circle , rectangle etc. ( in the other classes ).This all works.
If I load my saved file it shows me the correct location and correct size of drawn objects. If I open my xml file I can see all values are correctly saved ( including my FontFamily ).
Also the color which can be adjusted is saved and then properly displayed when I load a previously saved drawing.
Of course because the coordinates work, if I insert a textField ,the location where it is being displayed is correct.
However here comes the problem , my fontSize and fontfamily don't work.
As you can see I created them in the base class, However this does not
work. Is my approach completely off? What can I do ?
Before saving
img14.imageshack.us/i/beforeos.jpg/
After loading the Font jumps back to Sans serif and size 12.
I could really use some help here..
Edit: I've been using the sample from this website
http://www.vb-helper.com/howto_net_drawing_framework.html