누구나 입력 텍스트에서 이미지를 생성하는 방법을 안내 할 수 있습니다. 이미지의 확장자가 중요하지 않을 수 있습니다. (서버) 이미지에 텍스트를 렌더링런타임시 텍스트에서 이미지를 생성하는 방법
답변
좋아, 당신은 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 메서드를 호출하기 만하면됩니다.
을 사용하고 있습니다. "Image img = new Bitmap (0, 0);"줄이 작동하지 않습니다. 0 크기 이미지를 만들 수 없습니다. 그것을 "새로운 비트 맵 (1, 1)"로 변경하면됩니다. – neminem
'drawing.DrawString (text, font, textBrush, 0, 0);'앞에'drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;'를 추가하면 더 부드러운 앤티 앨리어싱 된 텍스트를 얻을 수 있습니다 . – LoneBunny
사용 imagemagick 당신이 C#으로하기 때문에
당신은 또한 (같은 클래스와 : System.Drawing.Bitmap
및 System.Drawing.Graphics
) 직접 비트 맵 폰트 조작을위한 닷넷 클래스를 사용할 수
방금이 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
편집 : 고정 오타.
감사합니다 프레디, 당신은 저에게 많은 에너지를 저장했습니다. – BedfordNYGuy
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
감사 카 자르. 사용 후 이미지/그래픽 개체를 처분하고 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;
}
- 1. 런타임시 텍스트에서 LINQ 쿼리를 작성하는 방법?
- 2. 아이폰의 텍스트에서 PDF를 생성하는 코드
- 3. VB6.0에서 런타임시 SQL 스크립트를 생성하는 방법
- 4. ASP.NET에서 즉시 이미지를 생성하는 방법
- 5. 런타임시 gridview 셀의 이미지를 변경하는 방법?
- 6. 컴팩트 프레임 워크에서 큰 이미지를 생성하는 방법
- 7. 런타임시 동적 메소드를 생성하는 자바 코드
- 8. URL을 텍스트에서 제거하는 방법
- 9. 타원형 브러시 이미지를 생성하는 알고리즘?
- 10. 런타임시 텍스트를 표시하는 방법 LightBox
- 11. 텍스트에서 명사와 동사를 추출하는 방법
- 12. 텍스트에서 객체 태그를 추출하는 방법
- 13. 선택한 텍스트에서 배경색을 변경하는 방법
- 14. 런타임시 호출 방법
- 15. 런타임시 레이블을 늘리는 방법
- 16. 런타임시 모노를 확인하는 방법
- 17. 런타임시 sql_mode를 변경하는 방법
- 18. 자연 텍스트에서 예쁘고 유효한 URL을 생성하는 PHP 함수가 있습니까?
- 19. 하나의 이미지를 생성하는 데 약간의 결과가 있습니다.
- 20. TYPE_BYTE_GRAY를 사용하여 AWT를 사용하여 효율적으로 회색조 버퍼 이미지를 생성하는 방법
- 21. iPhone/iPad 앱의 시작 이미지를 동적으로 생성하는 좋은 방법
- 22. 테이블을 동적으로 생성 (런타임시)
- 23. ListView를 런타임시 ItemControl을 VirtualizingStackPanel로 변경하는 방법
- 24. Nutch에서 이미지를 크롤링하는 방법?
- 25. 런타임시 현재 패키지를 런타임시 문자열로 가져 오는 방법
- 26. WPF : 런타임시 CurrentUICulture를 변경하는 방법
- 27. 런타임시 JasperReports의 열을 숨기는 방법?
- 28. 런타임시 after_save 콜백을 만드는 방법
- 29. 런타임시 AMF 응답 수정 - 방법?
- 30. 이미지를 생성하는 함수를 사용하는 방법은 무엇입니까?
화면 캡처와 같은 이미지를 의미합니까? 틀림없이 * 일부 형식/확장은 다른 것보다 낫습니다. – pavium
어떤 종류의 텍스트 입력을 의미합니까? –
아니요 화면 캡처가 아니며 입력란에 C# – Ravia