Dynamically Create a Text on Image

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Drawing2D;  
  5. using System.Drawing.Imaging;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.Web;  
  9. using System.Web.Mvc;  
  10. namespace WebTask.Controllers  
  11. {  
  12. public class BarcodeImageController : Controller  
  13. {  
  14. //  
  15. // GET: /BarcodeImage/  
  16. public void ImageText()  
  17. {  
  18. try  
  19. {  
  20. Bitmap bitMapImage = new System.Drawing.Bitmap("E:\\Ananth\\coupon_popup.png");  
  21. Graphics graphicImage = Graphics.FromImage(bitMapImage);  
  22. //Smooth graphics is nice.  
  23. graphicImage.SmoothingMode = SmoothingMode.AntiAlias;  
  24. //I am drawing a Rectangle around my text.  
  25. //graphicImage.DrawRectangle(new Pen(Color.Red, 3), 290, 915, 380, 50);  
  26. //Write your text.  
  27. graphicImage.DrawString("12F5B576B9E54B028B4D70DC7AD16376"new Font("Arial", 12, FontStyle.Bold), SystemBrushes.WindowText, new Point(310, 930));  
  28. //Set the content type  
  29. Response.ContentType = "image/jpeg";  
  30. bitMapImage.Save(@"E:\Ananth\BarcodeImage\test.jpeg", ImageFormat.Jpeg);  
  31. graphicImage.Dispose();  
  32. bitMapImage.Dispose();  
  33. }  
  34. catch (Exception ex)  
  35. {  
  36. string errormsg = ex.Message.ToString();  
  37. }  
  38. }  
  39. }  
  40. }