ASP.Net で指定されたフォントを使用してビットマップをプログラムで作成しようとしています。アイデアは、テキスト、フォント名、サイズの色などが変数から渡され、フォントなどを使用したテキストのビットマップが返されるというものです。ただし、特定のフォントで次のコードを使用することしかできないことがわかりました。
<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>
コメントで示された領域のフォント名を Arial または Verdana に変更すると、画像とラベルの両方が正しいフォントで表示されます。ただし、両方の場所のフォント名を「Segoe Script」に変更すると、ラベルは Segoe Script で表示されますが、画像は Arial のように見えます。
アップデート:
この質問hereに基づいて、 PrivateFontCollection() を使用してフォントファイルをロードすることで機能させることができました。
<div>
<%
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>