---
title: "Convert text to image"  
description: "Convert text to image"  
author: "Anonymous User"  
published: 2014-12-11  
updated: 2014-12-11  
canonical: https://www.mindstick.com/forum/12781/convert-text-to-image  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Convert text to image

I got a [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) of [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) into [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) using C#. The code is given below. [Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) my quetion is that this [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) return a [bitmap](https://www.mindstick.com/forum/1924/find-image-format-using-bitmap-object-in-c-sharp) image. How to show it in my [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) 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);}  
```

## Replies

### Reply by Barbara Jones

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);

### Reply by Tom Cruser

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);    }
```


---

Original Source: https://www.mindstick.com/forum/12781/convert-text-to-image

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
