将字体转换为字符串,然后再转换回来

发布于 2024-08-20 05:24:29 字数 157 浏览 2 评论 0原文

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

谢谢你,

贝尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愁以何悠 2024-08-27 05:24:29

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

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

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;
ゞ花落谁相伴 2024-08-27 05:24:29

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

有关如何操作的详细信息,请参阅这篇 MSDN 文章这样做。

序列化:

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

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;
}
我是有多爱你 2024-08-27 05:24:29

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

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

否则序列化就是要走的路。

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.

明天过后 2024-08-27 05:24:29

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

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

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 );
笑脸一如从前 2024-08-27 05:24:29

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

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

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>);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文