In this article we will discuss to convert text to image using System.Drawing namespace. Using Graphics property we can convert text it in to bitmap image and save it the specified loction.
Step 1: Copy and paste the following code in the design page Default.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile=" Default.aspx.cs" Inherits="CS" ValidateRequest="false" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>convert text toimage </title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox runat="server" TextMode="MultiLine" Rows="5" Columns="6" Width="250px" ID="txtText"></asp:TextBox>
<asp:Button ID="btnConvert" runat="server" Text="Convert"
OnClick="btnConvert_Click" />
<asp:Image ID="imgText" runat="server" Visible="false" />
</form>
</body>
</html>
Step 2: Copy and paste the following code in the Default.aspx.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
public partial class Default : System.Web.UI.Page
{
protected void btnConvert_Click(object sender, EventArgs e)
{
string text = txtText.Text.Trim();
Bitmap bitmap = new Bitmap(1, 1);
Font font = new Font("Arial", 25, FontStyle.Regular,GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
bitmap =new Bitmap(bitmap, new Size(width, height));
graphics= Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "~/images/" + fileName;
imgText.Visible = true;
}
}
Output:
VIDEO TUTORIAL TEXT TO IMAGE ASP.NET MVC C#:
This video demonstrates how to convert text to image using asp.net c#.
Post your comments / questions
Recent Article
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
- Unicode error 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
- Could not find the 'angular-devkit/build-angular:dev-server' builder's node package | Angular Error
- Error: error:0308010C:digital envelope routines::unsupported
Related Article