Advertisement
andruhovski

Placeholder

Jan 29th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. public class PlaceHolderController : Controller
  2.     {
  3.         private const string ContentType = "image/svg+xml; charset=utf-8";
  4.  
  5.         // GET: PlaceHolder
  6.         public ActionResult Index(string widthStr, string heightStr, string fontColorStr, string bkColorStr)
  7.         {
  8.             var dataDir = Server.MapPath("/Content");
  9.             var document = new SVGDocument(dataDir + "\\template.svg");
  10.  
  11.             document.RootElement.SetAttribute("width", widthStr);
  12.             document.RootElement.SetAttribute("height", heightStr);
  13.             document.RootElement.SetAttribute("viewBox", $"0 0 {widthStr} {heightStr}");
  14.  
  15.             var rect = document.GetElementById("rect1") as SVGRectElement;
  16.             if (rect == null) return Content(document.RootElement.OuterHTML, ContentType);
  17.             rect.SetAttribute("width", widthStr);
  18.             rect.SetAttribute("height", heightStr);
  19.             rect.Style.SetProperty("fill", "#" + bkColorStr, string.Empty);
  20.  
  21.             var text = document.GetElementById("text1") as SVGTextElement;
  22.             if (text == null) return Content(document.RootElement.OuterHTML, ContentType);
  23.             var width = uint.Parse(widthStr);
  24.             var height = uint.Parse(heightStr);
  25.             text.SetAttribute("x", (width / 2).ToString());
  26.             text.SetAttribute("y", (height / 2).ToString());
  27.             text.TextContent = $"{widthStr}x{heightStr}";            
  28.             text.Style.SetProperty("font-family", "Arial", "");
  29.  
  30.             var fontSize = Math.Round(Math.Max(12, Math.Min(.75 * Math.Min(width, height), 0.75 * Math.Max(width, height) / 12)));
  31.             text.Style.SetProperty("font-size", $"{fontSize}px", "");
  32.             text.Style.SetProperty("fill", "#" + fontColorStr, string.Empty);
  33.  
  34.             return Content(document.RootElement.OuterHTML, ContentType);
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement