Text on Images in asp.net
Following code generates image with the desired text.
<%@ OutputCache NoStore="true" Duration="1" VaryByParam="Text;Xcord;YCord"%>
<%@ Page Language="C#" trace="false" Explicit="true" aspcompat="true" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace=" System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
<%@ Page Language="C#" trace="false" Explicit="true" aspcompat="true" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace=" System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
//Get the Image on which you want to update the text
Bitmap bitmap = new Bitmap(Server.MapPath("car.jpg"));
MemoryStream memStream = new MemoryStream();
// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
int width = bitmap.Width ;
int height = bitmap.Height;
Bitmap bitmap = new Bitmap(Server.MapPath("car.jpg"));
MemoryStream memStream = new MemoryStream();
// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
int width = bitmap.Width ;
int height = bitmap.Height;
//Pass Font Family using QueryString
string familyName = Request.Params["FontFamily"];
string familyName = Request.Params["FontFamily"];
//Pass Desired Text using QueryString
string text = Request.Params["Text"];
// get a rectangle on the Image
int X_cord = int.Parse(Request.Params["Xcord"]);
int Y_cord = int.Parse(Request.Params["Ycord"]);
Rectangle rect = new Rectangle(X_cord, Y_cord, 1000, 500);
// Initialize the font for the text to b added.
Font font;
float fsize = float.Parse(Request.Params["FontSize"]);
font = new Font(familyName, fsize, FontStyle.Regular);
// Initialize the Font Format
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a location where you want to add text
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format);
// Draw the desired Text
Color fc = Color.FromName(Request.Params["FontColor"]);
HatchBrush hatchBrush = new HatchBrush( HatchStyle.LargeConfetti, fc, fc);
g.FillPath(hatchBrush, path);
Response.Clear();
Response.ContentType="image/jpeg";
string text = Request.Params["Text"];
// get a rectangle on the Image
int X_cord = int.Parse(Request.Params["Xcord"]);
int Y_cord = int.Parse(Request.Params["Ycord"]);
Rectangle rect = new Rectangle(X_cord, Y_cord, 1000, 500);
// Initialize the font for the text to b added.
Font font;
float fsize = float.Parse(Request.Params["FontSize"]);
font = new Font(familyName, fsize, FontStyle.Regular);
// Initialize the Font Format
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a location where you want to add text
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format);
// Draw the desired Text
Color fc = Color.FromName(Request.Params["FontColor"]);
HatchBrush hatchBrush = new HatchBrush( HatchStyle.LargeConfetti, fc, fc);
g.FillPath(hatchBrush, path);
Response.Clear();
Response.ContentType="image/jpeg";
//OutPut the image
memStream.WriteTo(Response.OutputStream);
// Clean Memory
memStream.WriteTo(Response.OutputStream);
// Clean Memory
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
bitmap.Dispose();
}
</script>
hatchBrush.Dispose();
g.Dispose();
bitmap.Dispose();
}
</script>
Comments