2010-01-15 4 views
47

누구나 입력 텍스트에서 이미지를 생성하는 방법을 안내 할 수 있습니다. 이미지의 확장자가 중요하지 않을 수 있습니다. (서버) 이미지에 텍스트를 렌더링런타임시 텍스트에서 이미지를 생성하는 방법

+0

화면 캡처와 같은 이미지를 의미합니까? 틀림없이 * 일부 형식/확장은 다른 것보다 낫습니다. – pavium

+0

어떤 종류의 텍스트 입력을 의미합니까? –

+0

아니요 화면 캡처가 아니며 입력란에 C# – Ravia

답변

123

좋아, 당신은 C#에서 이미지에 문자열을 그리려는 가정, 여기 System.Drawing 네임 스페이스를 사용할 필요가 예정 :

private Image DrawText(String text, Font font, Color textColor, Color backColor) 
{ 
    //first, create a dummy bitmap just to get a graphics object 
    Image img = new Bitmap(1, 1); 
    Graphics drawing = Graphics.FromImage(img); 

    //measure the string to see how big the image needs to be 
    SizeF textSize = drawing.MeasureString(text, font); 

    //free up the dummy image and old graphics object 
    img.Dispose(); 
    drawing.Dispose(); 

    //create a new image of the right size 
    img = new Bitmap((int) textSize.Width, (int)textSize.Height); 

    drawing = Graphics.FromImage(img); 

    //paint the background 
    drawing.Clear(backColor); 

    //create a brush for the text 
    Brush textBrush = new SolidBrush(textColor); 

    drawing.DrawString(text, font, textBrush, 0, 0); 

    drawing.Save(); 

    textBrush.Dispose(); 
    drawing.Dispose(); 

    return img; 

} 

이 코드는 먼저 문자열을 측정 할 것이다하는 그런 다음 올바른 크기의 이미지를 만듭니다.

이 함수의 반환 값을 저장하려면 반환 된 이미지의 Save 메서드를 호출하기 만하면됩니다.

+6

을 사용하고 있습니다. "Image img = new Bitmap (0, 0);"줄이 작동하지 않습니다. 0 크기 이미지를 만들 수 없습니다. 그것을 "새로운 비트 맵 (1, 1)"로 변경하면됩니다. – neminem

+3

'drawing.DrawString (text, font, textBrush, 0, 0);'앞에'drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;'를 추가하면 더 부드러운 앤티 앨리어싱 된 텍스트를 얻을 수 있습니다 . – LoneBunny

3

사용 imagemagick 당신이 C#으로하기 때문에

당신은 또한 (같은 클래스와 : System.Drawing.BitmapSystem.Drawing.Graphics) 직접 비트 맵 폰트 조작을위한 닷넷 클래스를 사용할 수

3

방금이 answer에서 언급 한이 메서드를 VB.NET 메서드로 변환했습니다. 아마도 이것은 누군가를 돕는 것일 수 있습니다.

Public Function DrawText(ByVal text As String, ByRef font As Font, ByRef textColor As Color, ByRef backColor As Color) As Image 
    ' first, create a dummy bitmap just to get a graphics object 
    Dim img As Image = New Bitmap(1, 1) 
    Dim drawing As Graphics = Graphics.FromImage(img) 

    ' measure the string to see how big the image needs to be 
    Dim textSize As SizeF = drawing.MeasureString(Text, Font) 

    ' free up the dummy image and old graphics object 
    img.Dispose() 
    drawing.Dispose() 

    ' create a new image of the right size 
    img = New Bitmap(CType(textSize.Width, Integer), CType(textSize.Height, Integer)) 

    drawing = Graphics.FromImage(img) 

    ' paint the background 
    drawing.Clear(BackColor) 

    ' create a brush for the text 
    Dim textBrush As Brush = New SolidBrush(textColor) 

    drawing.DrawString(text, font, textBrush, 0, 0) 

    drawing.Save() 

    textBrush.Dispose() 
    drawing.Dispose() 

    Return img 

End Function 

편집 : 고정 오타.

+0

감사합니다 프레디, 당신은 저에게 많은 에너지를 저장했습니다. – BedfordNYGuy

1

F# 버전 :


open System.Drawing 

let drawText text font textColor backColor = 
    let size = 
     use dummyImg = new Bitmap(1, 1) 
     use drawing = Graphics.FromImage(dummyImg) 
     drawing.MeasureString(text, font) 
    let img = new Bitmap((int size.Width), (int size.Height)) 
    use drawing = Graphics.FromImage(img) 
    use textBrush = new SolidBrush(textColor) 
    do 
     drawing.Clear(backColor) 
     drawing.DrawString(text, font, textBrush, PointF()) 
     drawing.Save() |> ignore 
    img 
3

감사 카 자르. 사용 후 이미지/그래픽 개체를 처분하고 US 크기의 매개 변수를 도입하기 위해 USING을 사용하는 이전 답변을 약간 향상 시켰습니다.

private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) { 
     return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty); 
    } 
    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) { 
     //first, create a dummy bitmap just to get a graphics object 
     SizeF textSize; 
     using (Image img = new Bitmap(1, 1)) { 
      using (Graphics drawing = Graphics.FromImage(img)) { 
       //measure the string to see how big the image needs to be 
       textSize = drawing.MeasureString(currencyCode, font); 
       if (!minSize.IsEmpty) { 
        textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width; 
        textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height; 
       } 
      } 
     } 

     //create a new image of the right size 
     Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height); 
     using (var drawing = Graphics.FromImage(retImg)) { 
      //paint the background 
      drawing.Clear(backColor); 

      //create a brush for the text 
      using (Brush textBrush = new SolidBrush(textColor)) { 
       drawing.DrawString(currencyCode, font, textBrush, 0, 0); 
       drawing.Save(); 
      } 
     } 
     return retImg; 
    } 
관련 문제