views:

255

answers:

5

Hello,

i have an application where my user changes font and font color for different labels etc and they save it to a file but i need to be able to convert the font of the specified label to a string to be written to file, and then when they open that file my program will convert that string back into a font object. How can this be done? I haven't found anywhere that shows how it can be done.

thank you

bael

A: 

First, you can use following article to enumerate system fonts.

public void FillFontComboBox(ComboBox comboBoxFonts)
{
    // Enumerate the current set of system fonts,
    // and fill the combo box with the names of the fonts.
    foreach (FontFamily fontFamily in Fonts.SystemFontFamilies)
    {
        // FontFamily.Source contains the font family name.
        comboBoxFonts.Items.Add(fontFamily.Source);
    }

    comboBoxFonts.SelectedIndex = 0;
}

To create a font:

Font font = new Font( STRING, 6F, FontStyle.Bold );

Use it to setup font style etc....

Label label = new Label();
. . .
label.Font = new Font( label.Font, FontStyle.Bold );
effkay
+4  A: 

You can serialize the font class to a file.

See this MSDN article for details of how to do so.

To serialize:

private void SerializeFont(Font fn, string FileName)
{
  using(Stream TestFileStream = File.Create(FileName))
  {
    BinaryFormatter serializer = new BinaryFormatter();
    serializer.Serialize(TestFileStream, fn);
    TestFileStream.Close();
  }
}

And to deserialize:

private Font DeSerializeFont(string FileName)
{
    if (File.Exists(FileName))
    {
        using(Stream TestFileStream = File.OpenRead(FileName))
        {
            BinaryFormatter deserializer = new BinaryFormatter();
            Font fn = (Font)deserializer.Deserialize(TestFileStream);
            TestFileStream.Close();
        }
        return fn;
    }
    return null;
}
Oded
+1 this is probably the most elegant solution to serialize the entire object.
Gerrie Schenck
Note that you don't have to use the BinaryFormatter and serialize straight to a file, you can use any IFormatter and any Stream.
Iain Galloway
A: 

Use this code to create a font based on the name and color information:

Font myFont = new System.Drawing.Font(<yourfontname>, 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Color myColor = System.Drawing.Color.FromArgb(<yourcolor>);
Gerrie Schenck
A: 

Quite simple really if you want to make it readable in the file:

class Program
{
    static void Main(string[] args)
    {
        Label someLabel = new Label();
        someLabel.Font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Strikeout | FontStyle.Italic);

        var fontString = FontToString(someLabel.Font);
        Console.WriteLine(fontString);
        File.WriteAllText(@"D:\test.txt", fontString);

        var loadedFontString = File.ReadAllText(@"D:\test.txt");

        var font = StringToFont(loadedFontString);
        Console.WriteLine(font.ToString());

        Console.ReadKey();
    }

    public static string FontToString(Font font)
    {
        return font.FontFamily.Name + ":" + font.Size + ":" + (int)font.Style;
    }

    public static Font StringToFont(string font)
    {
        string[] parts = font.Split(':');
        if (parts.Length != 3)
            throw new ArgumentException("Not a valid font string", "font");

        Font loadedFont = new Font(parts[0], float.Parse(parts[1]), (FontStyle)int.Parse(parts[2]));
        return loadedFont;
    }
}

Otherwise serialization is the way to go.

Codesleuth
I wouldn't bother rolling my own serializer since the Font class is [serializable] already.
Iain Galloway
+3  A: 

It is easy to go back and forth from a font to a string and back with the System.Drawing.FontConverter class. For example:

        var cvt = new FontConverter();
        string s = cvt.ConvertToString(this.Font);
        Font f = cvt.ConvertFromString(s) as Font;
Hans Passant
thank you nobugz :)
baeltazor