andruhovski icon

Placeholder

andruhovski | PRO | 01/29/18 08:52:57 PM UTC | 0 ⭐ | 1012 👁️ | Never ⏰ | []
C# |

1.85 KB

|

None

|

0 👍

/

0 👎

public class PlaceHolderController : Controller
    {
        private const string ContentType = "image/svg+xml; charset=utf-8";
 
        // GET: PlaceHolder
        public ActionResult Index(string widthStr, string heightStr, string fontColorStr, string bkColorStr)
        {
            var dataDir = Server.MapPath("/Content");
            var document = new SVGDocument(dataDir + "\\template.svg");
 
            document.RootElement.SetAttribute("width", widthStr);
            document.RootElement.SetAttribute("height", heightStr);
            document.RootElement.SetAttribute("viewBox", $"0 0 {widthStr} {heightStr}");
 
            var rect = document.GetElementById("rect1") as SVGRectElement;
            if (rect == null) return Content(document.RootElement.OuterHTML, ContentType);
            rect.SetAttribute("width", widthStr);
            rect.SetAttribute("height", heightStr);
            rect.Style.SetProperty("fill", "#" + bkColorStr, string.Empty);
 
            var text = document.GetElementById("text1") as SVGTextElement;
            if (text == null) return Content(document.RootElement.OuterHTML, ContentType);
            var width = uint.Parse(widthStr);
            var height = uint.Parse(heightStr);
            text.SetAttribute("x", (width / 2).ToString());
            text.SetAttribute("y", (height / 2).ToString());
            text.TextContent = $"{widthStr}x{heightStr}";            
            text.Style.SetProperty("font-family", "Arial", "");
 
            var fontSize = Math.Round(Math.Max(12, Math.Min(.75 * Math.Min(width, height), 0.75 * Math.Max(width, height) / 12)));
            text.Style.SetProperty("font-size", $"{fontSize}px", "");
            text.Style.SetProperty("fill", "#" + fontColorStr, string.Empty);
 
            return Content(document.RootElement.OuterHTML, ContentType);
        }
    }

Comments