2010-02-05 90 views
16

我有一個應用程序,其中我的用戶更改不同標籤等的字體和字體顏色,並將其保存到文件中,但我需要能夠將指定標籤的字體轉換爲字符串寫入文件,然後當他們打開該文件時,我的程序會將該字符串轉換回字體對象。如何才能做到這一點?我還沒有找到任何表明它如何完成的地方。將字體轉換爲字符串並返回

謝謝

巴爾

+0

如你想在你的文件中存儲.ttf文件? – 2010-02-05 14:11:47

+0

ttf?我的意思是我只需要將字體信息(例如:Arial,12.0,Regular,Green)轉換爲文件。然後當我加載該文件到一個字符串只是將其轉換爲字體對象 – 2010-02-05 14:14:05

回答

28

使用System.Drawing.FontConverter類可以很容易地從一個字體到一個字符串並返回。例如:

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

謝謝你nobugz :) – 2010-02-05 23:23:32

+0

根據[MSDN](http://msdn.microsoft.com/en-us/library/system.drawing.fontconverter.aspx)它說_訪問FontConverter類通過TypeDescriptor類通過調用GetConverter method_。這有什麼區別嗎? – Ben 2012-04-17 15:27:14

+1

不是我能想到的,讀取Font類中的[TypeConverter]屬性並得到完全相同的轉換器只是很長的路要走。該屬性永遠不會改變,從不+/- 1% – 2012-04-17 15:57:19

0

首先,你可以用下面的文章來枚舉系統字體。

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

可以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

+1這可能是序列化整個對象最優雅的解決方案。 – 2010-02-05 14:20:42

+0

請注意,您不必使用BinaryFormatter並直接序列化到文件,您可以使用任何IFormatter和任何Stream。 – 2010-02-05 14:33:54

0

使用此代碼基礎上的名稱和顏色信息來創建一個字體:

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

真的很簡單如果你想讓它在文件中可讀:

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

否則序列化是要走的路。

+0

因爲Font類已經是[serializable]了,所以我不會打擾我自己的序列化程序。 – 2010-02-05 14:45:14

相關問題