- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何使用我当前的 windows.forms 应用程序中的自定义 .tff 字体文件?我读了一些将其用作嵌入式资源的地方,但如何将其设置为 System.Drawing.Font 类型?
最佳答案
本文:How to embed a true type font展示了如何在 .NET 中执行您所要求的操作。
如何嵌入 True Type 字体
Some applications, for reasons of esthetics or a required visualstyle, will embed certain uncommon fonts so that they are always therewhen needed regardless of whether the font is actually installed onthe destination system.
The secret to this is twofold. First the font needs to be placed inthe resources by adding it to the solution and marking it as anembedded resource. Secondly, at runtime, the font is loaded via astream and stored in a PrivateFontCollection object for later use.
This example uses a font which is unlikely to be installed on yoursystem. Alpha Dance is a free True Type font that is available fromthe Free Fonts Collection. This font was embedded into the applicationby adding it to the solution and selecting the "embedded resource"build action in the properties.
Once the file has been successfully included in the resources you needto provide a PrivateFontCollection object in which to store it and amethod by which it's loaded into the collection. The best place to dothis is probably the form load override or event handler. Thefollowing listing shows the process. Note how the AddMemoryFont methodis used. It requires a pointer to the memory in which the font issaved as an array of bytes. In C# we can use the unsafe keyword forconvienience but VB must use the capabilities of the Marshal classesunmanaged memory handling. The latter option is of course open to C#programmers who just don't like the unsafe keyword.PrivateFontCollection pfc = new PrivateFontCollection();
private void Form1_Load(object sender, System.EventArgs e)
{
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("embedfont.Alphd___.ttf");
byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata,0,(int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed(byte * pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length);
}
}
}
Fonts may have only certain styles which are available andunfortunately, selecting a font style that doesn't exist will throw anexception. To overcome this the font can be interrogated to see whichstyles are available and only those provided by the font can be used.The following listing demonstrates how the Alpha Dance font is used bychecking the available font styles and showing all those that exist.Note that the underline and strikethrough styles are pseudo stylesconstructed by the font rendering engine and are not actually providedin glyph form.
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
bool bold=false;
bool regular=false;
bool italic=false;
e.Graphics.PageUnit=GraphicsUnit.Point;
SolidBrush b = new SolidBrush(Color.Black);
float y=5;
System.Drawing.Font fn;
foreach(FontFamily ff in pfc.Families)
{
if(ff.IsStyleAvailable(FontStyle.Regular))
{
regular=true;
fn=new Font(ff,18,FontStyle.Regular);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Bold))
{
bold=true;
fn=new Font(ff,18,FontStyle.Bold);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Italic))
{
italic=true;
fn=new Font(ff,18,FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(bold && italic)
{
fn=new Font(ff,18,FontStyle.Bold | FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
fn=new Font(ff,18,FontStyle.Underline);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
fn=new Font(ff,18,FontStyle.Strikeout);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
}
b.Dispose();
}
Figure 2 shows the application in action.
参见Form1_Paint 事件处理程序,它具体说明了如何设置System.Drawing.Font 类型。它基于使用 System.Drawing.Text.PrivateFontCollection 类。
希望这对您有所帮助。
关于c# - 在 C# windows.Form 中使用的自定义 .ttf 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/544590/
我在我的 Twitter Bootstrap 驱动网站上使用 CC-BY FontAwesome 字体作为图标。现在我想在图像编辑器中使用它来制作另一个网站的原型(prototype)。但它不起作用。
我在下面使用了这段代码,它提出了这个堆栈跟踪: java.io.FileNotFoundException: font.ttf (No such file or directory) at j
我在我的 android Assets 文件夹中包含了一个 ttf 文件。 我正在使用 day poster 作为 ttf。 但问题是某些手机不支持此自定义字体,即:它在某些手机上看起来不同。 beb
我正在制作一个网站。在 http://snpskollam.cf并且在 http://chemio.cf 上也有制作另一个的经验我面临一个问题,IE。我的服务器中有一个文本文件 (Montserrat
我是这样使用@font-face的: @font-face { font-family: DINPro; font-weight: bold; src: url(../res/
我想使用谷歌字体中没有的“Antonio 字体”。我收到了一个 .ttf 文件并按照下面的说明使用了它,它是否足够或者我是否需要额外的文件(woff、woff2 等)? @font-face {
我需要能够在我的网站上使用特定的泰卢固语字体。我有 ttf 文件并生成了所需的 .eot、.woff、.woff2 和 .svg 文件。我按照下面网站上的说明创建了如下所示的样式表 @font-fac
我的应用程序中需要 OpenSans,因此我导入了整个 OpenSans 系列,其中包括粗体、超粗体、斜体、常规、浅色等。我将它们添加到我的 Mac 上的 Fontbook(以检查我在使用它们时需要使
设想 我有一组名为 32.png,..,126.png 的图像与文件名中数字的 ASCII 可打印字符有关的手写字母,我打算将它们转换为字体文件,如 .ttf这样我就可以用它输入(基本) latex
这是一个定义了 few terms for the horizontal glyph metrics for fonts 的图表. 假设我有一句话, Foo bar baz. 如何获取单词“Foo”和
我正在尝试为我的系统设置字体并在我的应用程序中使用它它适用于 android 但不适用于 IOS- 首先我在IOS项目的资源中添加了.ttf文件 然后将此字体添加到 Entitlements.plis
在 OS X 上,您可以安装 .ttf双击字体文件。这在处理多个文件时很麻烦。是否有使用终端应用程序安装字体文件的命令? 最佳答案 您可以使用复制字体 cp myfont.ttf /Library/F
我正在尝试在我的应用程序中嵌入自定义字体。 我已将字体文件包含在我的项目中 我将 key 添加到.plist 我将字体添加到目标 结果: NSLog(@"Fonts: %@", [UIFont fam
我将字体文件 (.ttf) 添加到我的 Xcode 项目的资源中。另外,我将其添加到了 info.plist 中的 UIAppFonts 中。 当我想使用这种字体时,我什至不认为它是 IB 中的一个选
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
目前,我想添加到我的 React 项目中的 3 种字体:a、a light、bold。 我的文件结构: /src ├── /fonts/ │ ├── /A.ttf │ ├── /A-light
我正在尝试使用从网络下载的 .ttf 文件来使用“anagram.ttf”作为我的字体系列。 @font-face {font-family: 'Anagram'; src: u
我正在尝试研究创建我自己的自定义字体。我读了一些书,但由于我对字体一无所知,所以我有一个问题无法找到答案。 你能定义一种字体颜色是静态的吗?例如,我想创建一种字体,其中每个 A 都有蓝色背景,B 有黄
如果我有一个 .NET Font 对象,有没有办法以编程方式确定 C:\Windows\Fonts 目录中的哪个 ttf 文件是用它创建的? 最佳答案 This helpful article建议通过
我正在尝试使用从网络下载的 .ttf 文件来使用“anagram.ttf”作为我的字体系列。 @font-face {font-family: 'Anagram'; src: u
我是一名优秀的程序员,十分优秀!