Today I learn a new thing, how to generate and image form given text or how to convert text in to image? Dotnet framework provides System.Drawing and System.Drawing.Graphics class which helps us to generate image from text or convert text into image. Below is the code,
1: private Bitmap CreateBitmapImage(string sImageText)
2: {
3: Bitmap objBmpImage = new Bitmap(1, 1);
4:
5: int intWidth = 0;
6: int intHeight = 0;
7:
8: // Create the Font object for the image text drawing.
9: Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
10:
11: // Create a graphics object to measure the text's width and height.
12: Graphics objGraphics = Graphics.FromImage(objBmpImage);
13:
14: // This is where the bitmap size is determined.
15: intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
16: intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
17:
18: // Create the bmpImage again with the correct size for the text and font.
19: objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
20:
21: // Add the colors to the new bitmap.
22: objGraphics = Graphics.FromImage(objBmpImage);
23:
24: // Set Background color
25: objGraphics.Clear(Color.White);
26: objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
27: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
28: objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
29: objGraphics.Flush();
30:
31: return (objBmpImage);
32: }
Fig (1) – generate image from text or convert text into image
Happy Programming!!!
Hi Can you tell me how to use about in a website…I measn I wanna put a form where if text is entered it gets converted to image??
TIA
pankajt
hey chirag,
how are you putting source code in WordPress. I tried putting
but this way code looks very clumsy.
What tag have you used for this post.
MP
http://grewal.wordpress.com
Grewal,
I am using Windos live writer and plugins for writting the blog. This plug in writes the code in this format.
Nice article chiragrdarji – well written and works perfectly for me. Thanks.
Hi,
I’m a beginner in C#.
I would like to know how to call this function.
I need to create an image from a text and your function looks like what i need but i dont know how to use it.
Can someone helps me plz ?
Thx !
MemoryStream memStream = new MemoryStream();
objBmpImage.Save(memStream, ImageFormat.Jpeg);
memStream.WriteTo(Response.OutputStream);
Nice article
Another good one! Thanks a lot.
Thanks This is more use full to me.
//i used it this way.
Image imgThumb = new Bitmap(176, 134, PixelFormat.Format24bppRgb);
MemoryStream ms = new MemoryStream();
System.Drawing.Bitmap b = new System.Drawing.Bitmap(imgThumb);
Graphics objGraphics = Graphics.FromImage(b);
Font objFont = new Font(“Arial”, 40, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
objGraphics = Graphics.FromImage(b);
// Set Background color
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.DrawString(“Insert In Image”, objFont, new SolidBrush(Color.FromArgb(000, 122, 102)), 0, 0);
b.Save(ms, ImageFormat.Jpeg);
byte[] be = ms.ToArray();
[…] C# code shown below borrows from Tech Treasure for image output code but is otherwise adapted from the algorithm text linked above. Though […]
it was very good !
thank you
Can you show me how to generate an image from a fonted text string in C++ or C?
You wouldn’t believe how long I was finding the ability to do this without paying for something. I figured we should be able to do it.
THANKS
.Hellow Sir,Can you simply tell how to retrive a image to imagemap in asp.ne using c sharp?
Hello
I am a beginner in C#
I have to convert a text into image/Graph as in..
if I write
“NodeA NodeB 100”
then i shuld get an output in the form of a
circle(NodeA)——>circle(NodeB)..
Hope you get it..i want to create a graph from data and display that graph on screen(window).plz help how should i do it????
How do you put line breaks if your text is very long and the width of the image becomes too long which adds horizontal scroll bars on your page?
I did this:
if (intWidth > 750)
intWidth = 750;
But now I need to figure out how much the height should be.
HI,
Anybody found a solution for webbapps?
thanks!
Hi,
That was really helpful 🙂
I think on .net 3 you need to use System.Drawing.Drawing2D to use SmoothingMode.
I want to make a correction. Please include System.Drawing and System.Drawing.Imaging as a namespace. I don’t find System.Drawing.Graphics in my available assemblies.
Regards
You will need these:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
Thank you , Chiragrdarji!
Your post is very helpful and worked fine!
any body help me about project, please.
I am working on a Programme (Stenography)
Extraction Text from Image —>
My Task is : a hidden message sent through a picture.
Muhammad Amir (Karachi, Pakistan)
E-mail : amir_cs01191@yahoo.com
Please send me full documention about this or full coding into a .zip or .rar (Compression)
Thank You.
I am waiting your reply.
Hello friends
Your code convert a text into bitmap image.But i want to use a web control image in my website.So plz tell me how i can convert a bitmap image into web control image.
Best Regard
ravi Kumar
Hi guys;
I am an amateur in c#.
Please provide me help to capture text from an image and convert this to text file.
hey Guys
Do you have any software where i can convert data of image (.tiff format) to notepad(.text format)?
is there any way you can guide me?
looking forward to your reply??
plz reply on arpit_00016@yahoo.co.in
thanx
Arpit
any one tell me how its work?i want that image will create on button click.plz reply….
please help me
i want to convert 500 word from text file to image by using bitmap and save the result in d drive but icant do it please see my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Imagetext = “”;
StreamReader reader = new StreamReader(“d://New Text Document.txt”);
for (int i = 0; i <= 500; i++)
{
Imagetext = reader.ReadLine();
Program x = new Program();
Bitmap receiver= x.creatbitmapimage(Imagetext);
Bitmap bmp1 = new Bitmap(receiver, new Size(20, 40));
// bmp1.Save("d:pro", System.Drawing.Imaging.ImageFormat.Gif);
bmp1.Save("d:pro");
}
}
public Bitmap creatbitmapimage(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);
/* StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
objBmpImage.Save("New Text Document.txt", System.Drawing.Imaging.ImageFormat.Bmp);*/
return (objBmpImage);
}
}
}
i want to convert bit map file or image into text….pls can anybody help?
i want to convert bit map file or image into text but that image file Text are into a Vivaldi font ….pls can anybody help?
Thanks for code 🙂
Your site is pretty cool to me and your subject matter is very relevant. I was browsing around and came across something you might find interesting. I was guilty of 3 of them with my sites. “99% of site managers are guilty of these 5 mistakes”. http://bit.ly/uw1s4S You will be suprised how simple they are to fix.
hello sir,,,,,
how to convert text from image in c#
i need codings for convert text from image using c#
hello sir
v need some websites c# codings
i wants to display the special character in below fashion for c#.net code only. so plz help me if u can………….
1)
“* * * * * *
* * * * * *
* * * * * *
* * * * * *”
2)
” *
* *
* * *
* * * *
“
Very helpful
Thank You very much
Thanks
Thanks for the code snippet, this was exactly what I was looking for.
–RS
Hi Chirag,
I would like to if you help me.
I want to convert JPG file to Text file using C#.
Please send me these solution at shashankkulshresthas@yahoo.co.in
Hello Guys
I need to put 2 Gridviews in an image , Would you be so kind to come up with the code
“Generate Image from text using C# OR Convert Text in to Image using
C# | Tech Treasure” ended up being a remarkable blog post, can’t help but wait to read through
alot more of your blogs. Time to squander some time on-line haha.
Regards ,Ronald
Hi sir chirag!
Can you help to my research which I use the C# programming language
I need to convert the captured image to editable text will save into word processing like MS word which is usually use by the users.
hi, can any body tell me which algorithm i should use in my OCR application??? for converting image into text??