代码之家  ›  专栏  ›  技术社区  ›  Ravi Vanapalli

如何在运行时从动态文本生成图像

  •  54
  • Ravi Vanapalli  · 技术社区  · 15 年前

    任何人都能指导如何从输入文本生成图像吗?图像可能有任何扩展并不重要。

    5 回复  |  直到 7 年前
        1
  •  141
  •   Alistair Evans    14 年前

    好的,假设您想在C中的图像上绘制一个字符串,您需要使用系统。在这里绘制名称空间:

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

    此代码将首先测量字符串,然后创建正确大小的图像。

    如果要保存此函数的返回,只需调用返回图像的保存方法。

        2
  •  6
  •   Panayiotis Panayiotou    9 年前

    谢谢Kazar。对之前的答案进行了细微改进,用于处理使用后的图像/图形对象,并引入了最小尺寸参数。

        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;
        }
    
        3
  •  3
  •   Community Mohan Dere    8 年前

    我刚刚翻译了这里面提到的方法 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
    

    编辑 固定排字。

        4
  •  3
  •   Amirhossein Mehrvarzi GGregson    7 年前

    使用 imagemagick 用于在图像上呈现文本(在服务器上)

    由于您在C中,您还可以直接使用.NET类进行位图和字体操作(使用如下类: System.Drawing.Bitmap System.Drawing.Graphics )

        5
  •  1
  •   Dzmitry Lahoda Adam    9 年前

    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