C# 将字体转换为字符串并再次返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:26:03  来源:igfitidea点击:

convert font to string and back again

c#winformsstringfontscolors

提问by jay_t55

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

贝尔

采纳答案by Hans Passant

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

使用 System.Drawing.FontConverter 类可以轻松地在字体和字符串之间来回切换。例如:

        var cvt = new FontConverter();
        string s = cvt.ConvertToString(this.Font);
        Font f = cvt.ConvertFromString(s) as Font;

回答by effkay

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 );

回答by Oded

You can serializethe font class to a file.

您可以将字体类序列化为文件。

See this MSDN articlefor details of how to do so.

有关如何执行此操作的详细信息,请参阅此 MSDN 文章

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;
}

回答by Gerrie Schenck

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>);

回答by Codesleuth

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.

否则序列化是要走的路。