using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Web.Util; using System.Net.Mail; /// /// Web Service class. Provides functions related to adding text to an image /// [WebService(Namespace = "http://harbormist.com/", Description = "A web service which superimposes some text " + "onto an image. [Programmers: Eric Chudow and Lee Gumnic.]")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class DynamicImageGenerator : System.Web.Services.WebService { public DynamicImageGenerator() { } /// /// Create an image with the specified text added to it. /// /// The string message to add to the picture /// A byte array representing a jpeg file [WebMethod(Description = "Add to a jpeg image and return as a byte array")] public byte[] CreateImage(string message) { MemoryStream memStream = new MemoryStream(); using (Bitmap bmp = new Bitmap(Image.FromFile(Server.MapPath("image.jpg")))) { using (Graphics g = Graphics.FromImage(bmp)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawString(message, new Font(FontFamily.GenericSansSerif, 12), Brushes.White, new Rectangle(5, 5, 315, 165)); } bmp.Save(memStream, ImageFormat.Jpeg); } memStream.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[memStream.Length]; for (long i = 0; i < memStream.Length; i++) bytes[i] = (byte)memStream.ReadByte(); return bytes; } /// /// Send an email with an image and message to a seas user. /// /// The email address to be sent from /// /// The username to send the email to. /// (seas email address with no suffix...ie. gumnic, not gumnic@seas.upenn.edu) /// /// A text message to appear wit the picture /// Subject of the email /// text to add to the image /// string response [WebMethod(Description = "Send an email with added to a jpeg. " + " is a seas username to send the email to(@seas.upenn.edu)")] public string sendmail(string from, string to, string message, string subject, string caption) { try { message = Server.HtmlEncode(message); to = to.Replace("@seas.upenn.edu", ""); to += "@seas.upenn.edu"; byte[] imgbytes = CreateImage(caption); MailMessage emailMessage = new MailMessage(); emailMessage.From = new MailAddress(from); emailMessage.To.Add(to); emailMessage.Subject = subject; string filename = WriteToTempFile(imgbytes); Attachment imageAttach = new Attachment(filename); imageAttach.ContentId = "image"; imageAttach.Name = "image.jpg"; imageAttach.ContentType = new System.Net.Mime.ContentType("image/jpeg"); imageAttach.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; emailMessage.Attachments.Add(imageAttach); emailMessage.IsBodyHtml = true; emailMessage.Body = message.Replace("\n", "
") + "
"; GlobalInfo globals = GlobalInfo.GetInstance(); SmtpClient cli = new SmtpClient(globals.SmtpServer); cli.DeliveryMethod = SmtpDeliveryMethod.Network; cli.Send(emailMessage); } catch (Exception ex) { return "An Error Occurred: " + ex.Message + "
"; } return "success
"; } /// /// write data in bytes array to a temp file /// /// the data to write /// The temp filename private string WriteToTempFile(byte[] data) { string filename = System.IO.Path.GetTempFileName() + ".jpg"; FileStream fs = new FileStream(filename, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); for (long i = 0; i < data.Length; i++) fs.WriteByte(data[i]); fs.Flush(); fs.Close(); return filename; } }