Code: Programmatically add text to an image
The System.Drawing namespace offers a great deal of functionality that could be achieved only with paid third party tools in ASP earlier.
Try the following code snippet. It assumes that you have the image and text with you already...
<%@ Page language="C#" ContentType="image/jpeg"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object o, EventArgs e)
{
String FileName = "image1.jpg";
String Message = "Season's Greetings";
Color FillColor = Color.FromArgb(127,255,255,255);
SolidBrush FillBrush = new SolidBrush(FillColor);
Rectangle FillRectangle = new Rectangle(5,5,310,50);
Font TextFont = new Font("Comic Sans MS",20);
SolidBrush TextBrush = new SolidBrush(Color.Navy);
StringFormat TextFormat = new StringFormat();
TextFormat.Alignment = StringAlignment.Center;
TextFormat.LineAlignment = StringAlignment.Center;
System.Drawing.Image GreetingImage = System.Drawing.Image.FromFile(Server.MapPath("images") + "\\" + FileName);
Graphics DrawingSurface = Graphics.FromImage(GreetingImage);
DrawingSurface.FillRectangle(FillBrush, FillRectangle);
DrawingSurface.DrawString(Message, TextFont, TextBrush, FillRectangle, TextFormat);
Stream Output = Response.OutputStream;
GreetingImage.Save(Output,ImageFormat.Jpeg);
}
</script>
The above interesting code snippet is from a posting on the Hyderabad Dotnet User Group (which is a part of MUGH). The UG is a INETA member and currently has 2700 members! And I'm one among them.
Try the following code snippet. It assumes that you have the image and text with you already...
<%@ Page language="C#" ContentType="image/jpeg"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object o, EventArgs e)
{
String FileName = "image1.jpg";
String Message = "Season's Greetings";
Color FillColor = Color.FromArgb(127,255,255,255);
SolidBrush FillBrush = new SolidBrush(FillColor);
Rectangle FillRectangle = new Rectangle(5,5,310,50);
Font TextFont = new Font("Comic Sans MS",20);
SolidBrush TextBrush = new SolidBrush(Color.Navy);
StringFormat TextFormat = new StringFormat();
TextFormat.Alignment = StringAlignment.Center;
TextFormat.LineAlignment = StringAlignment.Center;
System.Drawing.Image GreetingImage = System.Drawing.Image.FromFile(Server.MapPath("images") + "\\" + FileName);
Graphics DrawingSurface = Graphics.FromImage(GreetingImage);
DrawingSurface.FillRectangle(FillBrush, FillRectangle);
DrawingSurface.DrawString(Message, TextFont, TextBrush, FillRectangle, TextFormat);
Stream Output = Response.OutputStream;
GreetingImage.Save(Output,ImageFormat.Jpeg);
}
</script>
The above interesting code snippet is from a posting on the Hyderabad Dotnet User Group (which is a part of MUGH). The UG is a INETA member and currently has 2700 members! And I'm one among them.
Hai,
ReplyDeleteThe above code is working nice..but in my application i need to save the output in desktop.please let me know how to save the text with image in my system.
Thanking You.
Excellent!
ReplyDeleteI used this code to allow Electricity consumers to view their actual bill online by writing data from a database on top of the scanned jpeg image of a bill.