런타임에 즉시 텍스트에서 이미지를 생성하는 방법
누구나 입력 텍스트에서 이미지를 생성하는 방법을 안내 할 수 있습니다. 이미지에 확장자가있을 수 있습니다.
좋습니다. 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 메서드를 호출하면됩니다.
고마워요 Kazar. 사용 후 이미지 / 그래픽 개체의 처분에 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;
}
이 답변 에서 언급 한이 방법 을 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
편집 : 오타 수정.
use imagemagick for rendering text on images (on the server)
Since you are in C# you could also use the .Net classes for bitmap and font manipulation directly (with the classes like: System.Drawing.Bitmap
and System.Drawing.Graphics
)
F#
version:
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
Here is Panayiotis' version of Kazar's answer with optional parameters and documentation, suitable for adding to a library class.
/// <summary>
/// Creates an image containing the given text.
/// NOTE: the image should be disposed after use.
/// </summary>
/// <param name="text">Text to draw</param>
/// <param name="fontOptional">Font to use, defaults to Control.DefaultFont</param>
/// <param name="textColorOptional">Text color, defaults to Black</param>
/// <param name="backColorOptional">Background color, defaults to white</param>
/// <param name="minSizeOptional">Minimum image size, defaults the size required to display the text</param>
/// <returns>The image containing the text, which should be disposed after use</returns>
public static Image DrawText(string text, Font fontOptional=null, Color? textColorOptional=null, Color? backColorOptional=null, Size? minSizeOptional=null)
{
Font font = Control.DefaultFont;
if (fontOptional != null)
font = fontOptional;
Color textColor = Color.Black;
if (textColorOptional != null)
textColor = (Color)textColorOptional;
Color backColor = Color.White;
if (backColorOptional != null)
backColor = (Color)backColorOptional;
Size minSize = Size.Empty;
if (minSizeOptional != null)
minSize = (Size)minSizeOptional;
//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(text, 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(text, font, textBrush, 0, 0);
drawing.Save();
}
}
return retImg;
}
참고URL : https://stackoverflow.com/questions/2070365/how-to-generate-an-image-from-text-on-fly-at-runtime
'Nice programing' 카테고리의 다른 글
WPF의 텍스트 블록에 스트로크 적용 (0) | 2020.12.06 |
---|---|
Git에서 두 개의 개별 분기에있는 파일을 비교할 수 없음 (0) | 2020.12.06 |
비공개 github 저장소에서 가져올 gem을 어떻게 지정할 수 있나요? (0) | 2020.12.06 |
목록의 새 전체 복사본 (복제본)을 만드는 방법 (0) | 2020.12.06 |
TypeError : $ (…) .DataTable은 함수가 아닙니다. (0) | 2020.12.06 |