using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; // dont forget to add this namespace! ** IMPORTANT ** public partial class DevCentre_AddTxtToImg_GenerateImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // get the picture filename and text to display from the query string string filename = Request.QueryString["Filename"]; string text = Request.QueryString["Text"]; // place the picture and our logo into seperate in-memory bitmaps System.Drawing.Image picture = System.Drawing.Image.FromFile(Server.MapPath("~/DevCentre/AddTxtImgToImg/" + filename)); System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("~/GlobalImages/webicon.ico")); // get the rectangle within which our picture and logo sit GraphicsUnit srcUnit = 0; // specifies the units of measure used by the RectangleF struct RectangleF pictureRect = picture.GetBounds(ref srcUnit); RectangleF logoRect = logo.GetBounds(ref srcUnit); // create a GDI+ graphics context for the image we intend to manipulate (in this case, "picture") Graphics g = Graphics.FromImage(picture); // now we can add the text & logo to our picture. The space set for the text will be the entire picture. // The location of the logo will be bottom right of the picture. Font font = new Font("Verdana", 12, FontStyle.Bold); g.DrawString(text, font, Brushes.White, pictureRect); g.DrawImage(logo, pictureRect.Width - logoRect.Width, pictureRect.Height - logoRect.Height); // render the image to the HTML output stream picture.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); // lastly dispose of the graphics context and images as they both tend to hold onto // unmanaged resources which may not be released immediately g.Dispose(); picture.Dispose(); logo.Dispose(); } }