1 using System; 2 using System.Web; 3 using System.Web.Services; 4 using System.Web.Services.Protocols; 5 using System.IO; 6 using System.Drawing; 7 using System.Drawing.Drawing2D; 8 using System.Drawing.Imaging; 9 using System.Web.Util; 10 using System.Net.Mail; 11 12 13 /// 14 /// Web Service class. Provides functions related to adding text to an image 15 /// 16 [WebService(Namespace = "http://harbormist.com/", 17 Description = "A web service which superimposes some text " + 18 "onto an image. [Programmers: Eric Chudow and Lee Gumnic.]")] 19 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 20 public class DynamicImageGenerator : System.Web.Services.WebService 21 { 22 public DynamicImageGenerator() 23 { 24 25 } 26 27 /// 28 /// Create an image with the specified text added to it. 29 /// 30 /// The string message to add to the picture 31 /// A byte array representing a jpeg file 32 [WebMethod(Description = "Add to a jpeg image and return as a byte array")] 33 public byte[] CreateImage(string message) 34 { 35 MemoryStream memStream = new MemoryStream(); 36 using (Bitmap bmp = new Bitmap(Image.FromFile(Server.MapPath("image.jpg")))) 37 { 38 using (Graphics g = Graphics.FromImage(bmp)) 39 { 40 g.SmoothingMode = SmoothingMode.AntiAlias; 41 g.DrawString(message, new Font(FontFamily.GenericSansSerif, 12), Brushes.White, new Rectangle(5, 5, 315, 165)); 42 43 } 44 bmp.Save(memStream, ImageFormat.Jpeg); 45 } 46 memStream.Seek(0, SeekOrigin.Begin); 47 byte[] bytes = new byte[memStream.Length]; 48 49 for (long i = 0; i < memStream.Length; i++) bytes[i] = (byte)memStream.ReadByte(); 50 51 return bytes; 52 } 53 54 /// 55 /// Send an email with an image and message to a seas user. 56 /// 57 /// The email address to be sent from 58 /// 59 /// The username to send the email to. 60 /// (seas email address with no suffix...ie. gumnic, not gumnic@seas.upenn.edu) 61 /// 62 /// A text message to appear wit the picture 63 /// Subject of the email 64 /// text to add to the image 65 /// string response 66 [WebMethod(Description = "Send an email with added to a jpeg. " + 67 " is a seas username to send the email to(@seas.upenn.edu)")] 68 public string sendmail(string from, string to, string message, string subject, string caption) 69 { 70 try 71 { 72 message = Server.HtmlEncode(message); 73 74 to = to.Replace("@seas.upenn.edu", ""); 75 to += "@seas.upenn.edu"; 76 77 byte[] imgbytes = CreateImage(caption); 78 79 MailMessage emailMessage = new MailMessage(); 80 emailMessage.From = new MailAddress(from); 81 emailMessage.To.Add(to); 82 emailMessage.Subject = subject; 83 84 string filename = WriteToTempFile(imgbytes); 85 86 Attachment imageAttach = new Attachment(filename); 87 imageAttach.ContentId = "image"; 88 imageAttach.Name = "image.jpg"; 89 imageAttach.ContentType = new System.Net.Mime.ContentType("image/jpeg"); 90 imageAttach.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 91 92 emailMessage.Attachments.Add(imageAttach); 93 emailMessage.IsBodyHtml = true; 94 emailMessage.Body = message.Replace("\n", "
") + "
"; 95 96 GlobalInfo globals = GlobalInfo.GetInstance(); 97 98 SmtpClient cli = new SmtpClient(globals.SmtpServer); 99 cli.DeliveryMethod = SmtpDeliveryMethod.Network; 100 cli.Send(emailMessage); 101 } 102 catch (Exception ex) 103 { 104 return "An Error Occurred: " + ex.Message + "
"; 105 } 106 return "success
"; 107 } 108 109 /// 110 /// write data in bytes array to a temp file 111 /// 112 /// the data to write 113 /// The temp filename 114 private string WriteToTempFile(byte[] data) 115 { 116 string filename = System.IO.Path.GetTempFileName() + ".jpg"; 117 FileStream fs = new FileStream(filename, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); 118 119 for (long i = 0; i < data.Length; i++) fs.WriteByte(data[i]); 120 121 fs.Flush(); 122 fs.Close(); 123 124 return filename; 125 } 126 127 } 128 129