【问题标题】:convert font to string and back again将字体转换为字符串并再次返回
【发布时间】:2011-01-13 13:02:04
【问题描述】:

我有一个应用程序,我的用户在其中更改不同标签等的字体和字体颜色,并将其保存到文件中,但我需要能够将指定标签的字体转换为要写入文件的字符串,并且然后当他们打开该文件时,我的程序会将该字符串转换回字体对象。如何才能做到这一点?我还没有找到任何地方显示它是如何完成的。

谢谢

贝尔

【问题讨论】:

  • 如您想将 .ttf 文件存储在您的文件中?
  • ttf?我的意思是我只需要将字体信息(例如:Arial、12.0、Regular、Green)转换为文件。然后当我将该文件加载到字符串中时,只需将其转换为字体对象

标签: c# winforms string fonts colors


【解决方案1】:

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

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

【讨论】:

  • 根据MSDN,它说通过调用GetConverter方法通过TypeDescriptor类访问FontConverter类。这有什么区别吗?
  • 不是我能想到的,读取 Font 类的 [TypeConverter] 属性并获得完全相同的转换器只是很长的路要走。该属性永远不会改变,永远不会 +/- 1%
  • 好答案。但是最好使用ConvertToInvariantStringConvertFromInvariantString。否则,字符串格式在不同国家/地区看起来会有所不同,即。 Tahoma, 10.5pt 在美国与 Tahoma; 10,5pt 在大多数欧洲国家/地区。
【解决方案2】:

你可以serialize把字体类放到一个文件里。

请参阅this MSDN article 了解如何操作的详细信息。

序列化:

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

反序列化:

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

【讨论】:

  • +1 这可能是序列化整个对象最优雅的解决方案。
  • 请注意,您不必使用 BinaryFormatter 并直接序列化为文件,您可以使用任何 IFormatter 和任何 Stream。
【解决方案3】:

如果你想让它在文件中可读,真的很简单:

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

否则序列化是要走的路。

【讨论】:

  • 我不会费心滚动我自己的序列化程序,因为 Font 类已经 [serializable]。
【解决方案4】:

首先,您可以使用以下文章来枚举系统字体。

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

创建字体:

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

用它来设置字体样式等......

Label label = new Label();
. . .
label.Font = new Font( label.Font, FontStyle.Bold );

【讨论】:

    【解决方案5】:

    使用此代码根据名称和颜色信息创建字体:

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

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2013-02-18
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2012-11-01
      • 2011-11-15
      • 1970-01-01
      • 2017-11-19
      相关资源
      最近更新 更多