public ActionResult Index(int dimension, string text) { text = text ?? HttpContext.Request.UserHostAddress; var ic = new IdenticonGenerator(); ic.DefaultBrushGenerator = new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(text)); var bitmap = ic.Create(text, new Size(dimension, dimension)); var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); return File(ms.ToArray(), "image/png"); }
public static IHtmlString GenerateIdenticon(this HtmlHelper html, string value, int dimension, bool useStaticBrush = false) { var i = new IdenticonGenerator() .WithBlockGenerators(IdenticonGenerator.ExtendedBlockGeneratorsConfig) .WithBrushGenerator(useStaticBrush ? (IBrushGenerator)new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(value)) : new RandomColorBrushGenerator() ) .WithSize(dimension, dimension); using (var bitmap = i.Create(value)) using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); var img = new TagBuilder("img"); img.Attributes.Add("width", bitmap.Width.ToString()); img.Attributes.Add("height", bitmap.Height.ToString()); img.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(stream.ToArray()))); return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing)); } }
public void IdenticonGenerator_Throws_OnInvalidWidthAfterRounding() { var i = new IdenticonGenerator(); i.Create("test", new Size(10, 100), Color.Red, new Size(12, 2)); }
public void IdenticonGenerator_HasDefaultsForByteArray() { var i = new IdenticonGenerator(); i.Create(new byte[0]); }
public void IdenticonGenerator_Throws_OnInvalidHeight() { var i = new IdenticonGenerator(); i.Create("test", new Size(10, 0)); }
public void IdenticonGenerator_Throws_OnInvalidWidth() { var i = new IdenticonGenerator(); i.Create("test", new Size(0, 10)); }
public void IdenticonGenerator_Throws_OnInvalidBlocksHorizontal() { var i = new IdenticonGenerator(); i.DefaultBlocks = new Size(0, 10); i.Create("test"); }
public void IdenticonGenerator_Throws_OnInvalidBlocksVertical() { var i = new IdenticonGenerator(); i.DefaultBlocks = new Size(10, 0); i.Create("test"); }
public void IdenticonGenerator_HasDefaultsForIPAddress() { var i = new IdenticonGenerator(); i.Create(IPAddress.Loopback); }
public void IdenticonGenerator_Throws_OnInvalidAlgorithm() { var i = new IdenticonGenerator("XXX"); i.Create("test"); }
public void IdenticonGenerator_HasDefaults() { var i = new IdenticonGenerator(); i.Create("test"); }
public void IdenticonGenerator_Throws_OnNoBlockGenerators() { var i = new IdenticonGenerator(); i.DefaultBlockGenerators = new IBlockGenerator[0]; i.Create("test"); }
public void IdenticonGenerator_DoesNotThrowOnNullString() { var i = new IdenticonGenerator(); string s = null; i.Create(s); }
public void IdenticonGenerator_Throws_OnNullIPAddress() { var i = new IdenticonGenerator(); IPAddress a = null; i.Create(a); }
public void IdenticonGenerator_Throws_OnNullEncoding() { var i = new IdenticonGenerator(); i.DefaultEncoding = null; i.Create("test"); }
public void IdenticonGenerator_Throws_OnNullBlockGenerator() { var i = new IdenticonGenerator(); i.DefaultBlockGenerators = IdenticonGenerator.DefaultBlockGeneratorsConfig.Concat(new IBlockGenerator[] { null }).ToArray(); i.Create("test"); }
public void IdenticonGenerator_Throws_OnNoSymmetricBlockGeneratorsForUnevenHorizontalBlocks() { var i = new IdenticonGenerator().WithBlocks(5,5); //Get all NON-symmetric blockgens i.DefaultBlockGenerators = IdenticonGenerator.ExtendedBlockGeneratorsConfig.Where(bg => !bg.IsSymmetric).ToArray(); i.Create("test"); }