This
article will show you how to convert text into image in c# In windows
application. In this I have controlled textbox value into image.
Some of my
previous articles are as follows: Detecting
Ctrl+ V Key Pressed Or Stroke Using C# & VB.Net In Windows Application, Group
CheckBox In C# WinForm, Read
Outlook Email Subject Using c#, Read
Outlook Inbox Mail And Email Count Of Inbox Using C#.Net, How
To Read XML File In DataSet And Display in DataGridview Using C#.Net, WebBrowser
Control in C#, Windows Application, Change
Control Font Size At Runtime / Dynamically Using C#.Net and VB.Net In Windows
Application, Export
GridView Data To Excel Sheet Using C#.Net In Windows Application, Windows
Application In C#.Net (My first video article on youtube.com), How
to Create a Code 128 Barcode Label Using windows Font in C# ,Windows
Application.
So for this application first we will create a new windows application and add textbox, button and an image control in the form.
So for this application first we will create a new windows application and add textbox, button and an image control in the form.
Now we
generate the button click event, and add the below code as shown below.
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//how to convert text into image in c# In Windows
Application
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please enter some value.");
}
else
{
pictureBox1.Image = DrawText(textBox1.Text);
}
}
public Image
ConvertImageToText(string textValue)
{
Image img = new Bitmap(200, 30,
System.Drawing.Imaging.PixelFormat.Format64bppArgb);
Graphics drawing = Graphics.FromImage(img);
SizeF textSize =
drawing.MeasureString(textValue, new Font("Arial", 20, FontStyle.Bold));
img.Dispose();
drawing.Dispose();
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
drawing.Clear(Color.Black);
Brush textBrush = new SolidBrush(Color.White);
drawing.DrawString(text, new Font("Arial", 20, FontStyle.Bold), textBrush, new PointF(0.4F, 2.4F));
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
}
}
|
In above
code I have created a function whose return type is image and then bind the
function to the picture box control by passing the textbox value as a method
parameter. Now we have done run the application to check the output.
0 comments:
Please let me know your view