Convert text to image

Total Post:47

Points:331
C# 
 1336  View(s)
Ratings:
Rate this:

I got a code of convert text into image using C#. The code is given below. Now my quetion is that this function return a bitmap image. How to show it in my asp.net page. I want show the image which is returned by this function. 

private Bitmap CreateBitmapImage(string sImageText)
{
    Bitmap objBmpImage = new Bitmap(1, 1);
 
    int intWidth = 0;
    int intHeight = 0;
 
    // Create the Font object for the image text drawing.
    Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
 
    // Create a graphics object to measure the text's width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);
 
    // This is where the bitmap size is determined.
    intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
 
    // Create the bmpImage again with the correct size for the text and font.
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight)); 
    // Add the colors to the new bitmap.
    objGraphics = Graphics.FromImage(objBmpImage); 
    // Set Background color
    objGraphics.Clear(Color.White);
    objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
    objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
    objGraphics.Flush();
    return (objBmpImage);
}  

  1. Post:28

    Points:196
    Re: Convert text to image

    reate httphandler (like image.ashx) and use something like this code: 

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/png";
            var image = CreateBitmapImage("Hello world");
            var ms = new MemoryStream();
            image.Save(ms,ImageFormat.Png);
     
            context.Response.BinaryWrite(ms.ToArray());
        }

    And where you need to have your image add the link < img src="image.ashx" alt="image" /> 

    Updated [based on comments]: 

    http-handler: 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/png";
     
            var text =context.Request.Params["text"];
            if (text ==null) text = string.Empty;
            var image =CreateBitmapImage(text); 
            image.Save(context.Response.OutputStream, ImageFormat.Png);
        }

    page layout: 

        <asp:TextBox runat="server" ID="MyTextBox"></asp:TextBox>
        <asp:Button runat="server" OnClick="RenderImageButtonClicked" Text="Change text"/>
        <asp:Image runat="server" ID="MyTextImage" />

    page event: 

        protected void RenderImageButtonClicked(object sender, EventArgs e)    {
            MyTextImage.ImageUrl = "CreateImageText.ashx?text=" + HttpContext.Current.Server.UrlEncode(MyTextBox.Text);
        }

      Modified On Apr-07-2018 04:56:41 AM
  1. Post:13

    Points:91
    Re: Convert text to image

    there is an example of this with the following function 

    public Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
        {
            Bitmap bmp = new Bitmap(width, Height);
            using (Graphics graphics = Graphics.FromImage(bmp))
            { 
                Font font = new Font(fontname, fontsize);
                graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
                graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
                graphics.Flush();
                font.Dispose();
                graphics.Dispose(); 
     
            }
            return bmp;
        }

    and to use this function: 

    ConvertTextToImage(txtvalue.Text, "Bookman Old Style", 10, Color.Yellow, Color.Red, txtvalue.Width, txtvalue.Height);

      Modified On Apr-07-2018 04:54:20 AM

Answer