新的System.Drawing.Font代码在后面 [英] new System.Drawing.Font Code Behind
问题描述
我正尝试在c#中使用名为Black Rose的字体创建图像。图像被创建,但不是我想要的字体。这是我用来创建图像的代码:
pre $ protected $ page_Load(object sender,EventArgs e)
{
string MoneyImg =1000;
位图位图=新的位图(MoneyImg.Length * 40,150);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
Font oFont = new System.Drawing.Font(BLACKR,20);
PointF point = new PointF(2f,2f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush white = new SolidBrush(Color.White);
graphics.FillRectangle(white,0,0,bitmap.Width,bitmap.Height);
graphics.DrawString($+ MoneyImg,oFont,黑色,点);
$ / code $ / pre
我试着改变这一行
Font oFont = new System.Drawing.Font(BLACKR,20);
到
Font oFont = new System.Drawing.Font(BLACKR.TTF,20);
但是没有区别。我知道字体文件是在正确的位置,因为我做了CSS样式的测试,并显示正常。这里是CSS代码和截图。
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>< / title>
< style>
@ font-face
{
font-family:Black Rose; src:url('BLACKR.TTF');
}
h3 {
font-family:'黑玫瑰'
}
< / style>
< / head>
< body>
< form id =form1runat =server>
< div>
< asp:Image ID =Image1runat =server/>
< br />
< h3>这就是图像字体的样子< / h3>
< / div>
< / form>
< / body>
< / html>
主机系统可能不知道字体。该文件可能位于Web应用程序文件层次结构中的某个位置,但这对底层系统没有什么影响。 Windows不会仅仅因为该字体文件存在于硬盘驱动器上的某处就知道字体。
您可以将字体添加到一个 PrivateFontCollection
代码并从中加载。像这样:
var myFonts = new System.Drawing.Text.PrivateFontCollection();
myFonts.AddFontFile(@C:\path\to\BLACKR.TTF);
var oFont = new System.Drawing.Font(myFonts.Families [0],20);
I am trying to create an image in c# using a font called Black Rose. The image gets created but not in the font that I want. Here is my code that I used to create the image:
protected void Page_Load(object sender, EventArgs e)
{
string MoneyImg = "1000";
Bitmap bitmap = new Bitmap(MoneyImg.Length * 40, 150);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font oFont = new System.Drawing.Font("BLACKR", 20);
PointF point = new PointF(2f, 2f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush white = new SolidBrush(Color.White);
graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
graphics.DrawString("$" + MoneyImg , oFont, black, point);
}
}
I tried changing this line
Font oFont = new System.Drawing.Font("BLACKR", 20);
to
Font oFont = new System.Drawing.Font("BLACKR.TTF", 20);
but is does not make a difference. I know font file is in the correct location because I did a test with CSS Style and it shows up fine. Here is the CSS Code and a screenshot.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
@font-face
{
font-family: Black Rose; src: url('BLACKR.TTF');
}
h3 {
font-family: 'Black Rose'
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<br />
<h3>This is what the Image Font Looks Like</h3>
</div>
</form>
</body>
</html>
It's likely that the font simply isn't known to the host system. The file may be somewhere in the web application file hierarchy, but that doesn't make a difference to the underlying system. Windows isn't going to know about a font simply because that font file exists somewhere on the hard drive.
You can add the font to a PrivateFontCollection
in code and load it from that. Something like this:
var myFonts = new System.Drawing.Text.PrivateFontCollection();
myFonts.AddFontFile(@"C:\path\to\BLACKR.TTF");
var oFont = new System.Drawing.Font(myFonts.Families[0], 20);
这篇关于新的System.Drawing.Font代码在后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!