开发者

Unable to use certain Fonts programatically in ASP.Net

I am trying to programatically create a bitmap with a specified font in ASP.Net. The idea is that the text, font name, size color etc. will be passed in from variables and a bitmap of the text using the font etc will be returned. However, I have been finding that I am only able to do so using the following code with certain fonts.

   <div>
    <%
     string fontName = "Segoe Script"; //Change Font here
     System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
     System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
     System.Drawing.Font fnt = new System.Drawing.Font(fontName, 20);
     System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
    graph.DrawString("Help", fnt, brush, new System.Drawing.Point(10, 10));

    bmp.Save(@"C:\Development\Path\image1.bmp");
    this.Image1.ImageUrl = "http://mysite/Images/image1.bmp";
    %>
 <asp:Label ID="Label1" runat="server" Text="Label" Font-Names="Segoe Script">  <%Response.Write("Help"); %></asp:Label> //Change font here
<asp:Image ID="Image1" runat="server" />
</div>

If I change the font name in the areas indicated by the comments to Arial or Verdana both the image and the label appear with the correct font. If however, I change the font name in both locations to something "Segoe Script" the Label will show up in Segoe Script but the image is in what looks like Arial.

Update:

Based on this question here I was able to get it working by using a PrivateFontCollection() and loading the font file like so.

   <div>
   &开发者_如何学Pythonlt;%
    string TypeFaceName = "Segoe Script";
    System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection();
    fnts.AddFontFile(@"C:\Development\Fonts\segoesc.ttf");
    System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(TypeFaceName);
    System.Drawing.Font fnt = new System.Drawing.Font(fntfam, 13);

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
    System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
    graph.DrawString("Help", fnt, brush, new System.Drawing.Point(10, 10));

    bmp.Save(@"C:\Development\Path\Images\image1.bmp");
    this.Image1.ImageUrl = "http://MySite/Images/image1.bmp";
    %>
    <asp:Label ID="Label1" runat="server" Text="Label" Font-Names="Segoe Script">     <%Response.Write("Help"); %></asp:Label>
    <asp:Image ID="Image1" runat="server" />
    </div>


Make sure that the font is installed on your server.

Also, your code will fail horribly if two people view the page at the same time.
You need to create a .ASHX handler that takes parameters in the query-string and dynamically serves the image.


You will run into memory troubles with your code. All GDI+ objects need to be carefully released or they show leaking behavior (even though the GC will eventually clean up via finalizers this can be too late, since the amount of unmanaged memory left sitting unused may cause the application to break earlier).

Also, you may want to use a special IHttpHandler which processed the requests for such "dynamic text" instead of creating "static" files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜