2010-05-06 4 views
1

프로그래밍 방식으로 ASP.Net에서 지정된 글꼴로 비트 맵을 만들려고합니다. 아이디어는 텍스트, 글꼴 이름, 크기 색상 등이 변수에서 전달되고 글꼴 등을 사용하는 텍스트의 비트 맵이 반환된다는 것입니다. 그러나 특정 글꼴로 다음 코드를 사용하여 수행 할 수 있음을 발견했습니다. 나는 이미지와 표지 모두 굴림 또는 굴림에 주석으로 표시된 지역에서 글꼴 이름을 변경하는 경우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> 

올바른 글꼴로 나타납니다. 그러나 두 위치의 글꼴 이름을 "Segoe Script"로 변경하면 Segoe Script에 레이블이 표시되지만 이미지는 Arial과 비슷합니다.

업데이트 : 나는 PrivateFontCollection()를 사용하여 등과 같은 글꼴 파일을로드하여 작업 얻을 수있었습니다이 질문에 here을 바탕으로

.

<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> 

답변

1

글꼴이 서버에 설치되어 있는지 확인하십시오.

또한 두 사람이 동시에 페이지를 보는 경우 코드가 손상 될 수 있습니다.
쿼리 문자열에서 매개 변수를 가져 와서 이미지를 동적으로 제공하는 .ASHX 처리기를 만들어야합니다.

+0

글꼴이 웹 서버에 설치되어 있으며 paint.net을 사용하여 글꼴로 이미지를 수동으로 만들 수 있습니다. 내가 게시 한 코드는 그것이 수행 될 수 있는지를 확인하는 것입니다. 감사. – etoisarobot

0

코드로 인해 메모리 문제가 발생합니다. GDI + 개체를 모두 조심스럽게 릴리스해야합니다. 그렇지 않으면 관리되지 않는 메모리가 미사용 상태로 남아있어 응용 프로그램이 더 일찍 중단 될 수 있으므로 GC가 최종 처리기를 통해 결국 정리할지라도 너무 늦을 수 있습니다.

또한 "정적"파일을 만드는 대신 "동적 텍스트"에 대한 요청을 처리하는 특수한 IHttpHandler를 사용할 수 있습니다.

관련 문제