如何有效地持久保存.Net字体对象?

时间:2022-03-23 19:42:27

Usecase: The user makes font customizations to an object on the design surface, that I need to load/save to my datastore. I.e. settings like Bold, Italics, Size, Font Name need to persisted.

用例:用户对设计图面上的对象进行字体自定义,我需要加载/保存到我的数据存储区。即Bold,Italics,Size,Font Name等设置需要保留。

Is there some easy (and reliable) mechanism to convert/read back from a string representation of the font object (in which case I would need just one attribute)? Or is multiple properties combined with custom logic the right option?

是否有一些简单(和可靠)的机制来转换/从字体对象的字符串表示回读(在这种情况下,我只需要一个属性)?或者是多个属性与自定义逻辑相结合的正确选项?

3 个解决方案

#1


10  

Use TypeConverter:

Font font = new Font("Arial", 12, GraphicsUnit.Pixel);

TypeConverter converter = TypeDescriptor.GetConverter(typeof (Font));

string fontStr = converter.ConvertToInvariantString(font);

Font font2 = (Font) converter.ConvertFromString(fontStr);

Console.WriteLine(font.Name == font2.Name); // prints True

If you want to use XML serialization you can create Font class wrapper which will store some subset of Font properties.

如果要使用XML序列化,可以创建Font类包装器,它将存储Font属性的一些子集。

Note(Gishu) - Never access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. Very important :)

注(Gishu) - 切勿直接访问型号转换器。而是使用TypeDescriptor访问适当的转换器。很重要 :)

#2


0  

In the project I'm working on, I went with the multiple properties.

在我正在进行的项目中,我选择了多个属性。

I save the font to a database table by breaking out its name, size, style and unit and then persist those values.

我通过打破它的名称,大小,样式和单位将字体保存到数据库表中,然后保留这些值。

Recreating the font on demand once these values are retrived is a snap.

恢复这些值后,按需重新创建字体非常容易。

#3


0  

What type of datastore do you need to persist this in? If it is just user settings that can be persisted in a file you could serialise the font object to a settings file in either binary or xml (if you want to be able to edit the config file directly). The serialisation namespaces (System.Xml.Serialization and System.Runtime.Serialization) provide all the tools to do this without writing custom code.

您需要什么类型的数据存储来保留它?如果只是用户设置可以保存在文件中,您可以将字体对象序列化为二进制或xml中的设置文件(如果您希望能够直接编辑配置文件)。序列化命名空间(System.Xml.Serialization和System.Runtime.Serialization)提供了所有工具,无需编写自定义代码。

MSDN Site on XML Serialisation: XML Serialization in the .Net Framework

关于XML序列化的MSDN站点:.Net Framework中的XML序列化

[EDIT]So aparrently the font object isn't serialisable. oops :( Sorry.

[编辑]所以很明显,字体对象不是可序列化的。哎呀,对不起。

#1


10  

Use TypeConverter:

Font font = new Font("Arial", 12, GraphicsUnit.Pixel);

TypeConverter converter = TypeDescriptor.GetConverter(typeof (Font));

string fontStr = converter.ConvertToInvariantString(font);

Font font2 = (Font) converter.ConvertFromString(fontStr);

Console.WriteLine(font.Name == font2.Name); // prints True

If you want to use XML serialization you can create Font class wrapper which will store some subset of Font properties.

如果要使用XML序列化,可以创建Font类包装器,它将存储Font属性的一些子集。

Note(Gishu) - Never access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. Very important :)

注(Gishu) - 切勿直接访问型号转换器。而是使用TypeDescriptor访问适当的转换器。很重要 :)

#2


0  

In the project I'm working on, I went with the multiple properties.

在我正在进行的项目中,我选择了多个属性。

I save the font to a database table by breaking out its name, size, style and unit and then persist those values.

我通过打破它的名称,大小,样式和单位将字体保存到数据库表中,然后保留这些值。

Recreating the font on demand once these values are retrived is a snap.

恢复这些值后,按需重新创建字体非常容易。

#3


0  

What type of datastore do you need to persist this in? If it is just user settings that can be persisted in a file you could serialise the font object to a settings file in either binary or xml (if you want to be able to edit the config file directly). The serialisation namespaces (System.Xml.Serialization and System.Runtime.Serialization) provide all the tools to do this without writing custom code.

您需要什么类型的数据存储来保留它?如果只是用户设置可以保存在文件中,您可以将字体对象序列化为二进制或xml中的设置文件(如果您希望能够直接编辑配置文件)。序列化命名空间(System.Xml.Serialization和System.Runtime.Serialization)提供了所有工具,无需编写自定义代码。

MSDN Site on XML Serialisation: XML Serialization in the .Net Framework

关于XML序列化的MSDN站点:.Net Framework中的XML序列化

[EDIT]So aparrently the font object isn't serialisable. oops :( Sorry.

[编辑]所以很明显,字体对象不是可序列化的。哎呀,对不起。