protected override void WriteFile(HttpResponseBase response) { response.Clear(); if(!string.IsNullOrEmpty(Etag)) { response.AppendHeader("ETag", Etag); } response.ContentType = "image/png"; var renderer = new IdenticonRenderer(); using(Bitmap b = renderer.Render(Code, Size)) { using(var stream = new MemoryStream()) { b.Save(stream, ImageFormat.Png); stream.WriteTo(response.OutputStream); } } }
private void GenerateCivIdenticons() { List<Entity> civs = Entities.Where(entity => entity.IsCiv).ToList(); List<string> races = Entities.Where(entity => entity.IsCiv).GroupBy(entity => entity.Race).Select(entity => entity.Key).OrderBy(entity => entity).ToList(); int identiconSeed = Events.Count; Random code = new Random(identiconSeed); //Calculates color //Creates a variety of colors //Races 1 to 6 get a medium color //Races 7 to 12 get a light color //Races 13 to 18 get a dark color //19+ reduced color variance int maxHue = 300; int colorVariance; if (races.Count <= 1) colorVariance = 0; else if (races.Count <= 6) colorVariance = Convert.ToInt32(Math.Floor(maxHue / Convert.ToDouble(races.Count - 1))); else if (races.Count > 18) colorVariance = Convert.ToInt32(Math.Floor(maxHue / (Math.Ceiling(races.Count / 3.0) - 1))); else colorVariance = 60; foreach (Entity civ in civs) { int colorIndex = races.IndexOf(civ.Race); Color raceColor; if (colorIndex * colorVariance < 360) raceColor = Formatting.HsvToRgb(colorIndex * colorVariance, 1, 1.0); else if (colorIndex * colorVariance < 720) raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 360, 0.4, 1); else if (colorIndex * colorVariance < 1080) raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 720, 1, 0.4); else raceColor = Color.Black; int identiconCode; IdenticonRenderer identiconRenderer = new IdenticonRenderer(); identiconCode = code.Next(); civ.IdenticonCode = identiconCode; int alpha; if (races.Count <= 12) alpha = 175; else alpha = 175; if (!MainRaces.ContainsKey(civ.Race)) { MainRaces.Add(civ.Race, raceColor); } civ.IdenticonColor = Color.FromArgb(alpha, raceColor); civ.LineColor = raceColor; using (MemoryStream identiconStream = new MemoryStream()) { using (Bitmap identicon = identiconRenderer.Render(identiconCode, 64, civ.IdenticonColor)) { identicon.Save(identiconStream, System.Drawing.Imaging.ImageFormat.Png); byte[] identiconBytes = identiconStream.GetBuffer(); civ.Identicon = new Bitmap(identicon); civ.IdenticonString = Convert.ToBase64String(identiconBytes); } } using (MemoryStream smallIdenticonStream = new MemoryStream()) { using (Bitmap smallIdenticon = identiconRenderer.Render(identiconCode, 24, civ.IdenticonColor)) { smallIdenticon.Save(smallIdenticonStream, System.Drawing.Imaging.ImageFormat.Png); byte[] smallIdenticonBytes = smallIdenticonStream.GetBuffer(); civ.SmallIdenticonString = Convert.ToBase64String(smallIdenticonBytes); } } foreach (Entity group in civ.Groups) { group.Identicon = civ.Identicon; group.SmallIdenticonString = civ.SmallIdenticonString; } } Bitmap nullIdenticon = new Bitmap(64, 64); using (Graphics nullGraphics = Graphics.FromImage(nullIdenticon)) { using (SolidBrush nullBrush = new SolidBrush(Color.FromArgb(150, Color.Red))) nullGraphics.FillRectangle(nullBrush, new Rectangle(0, 0, 64, 64)); Entities.Where(entity => entity.Identicon == null).ToList().ForEach(entity => entity.Identicon = nullIdenticon); } }
public static void SetIdenticonAsUserImage(User user) { var code = Guid.NewGuid().GetHashCode(); var profileImage = new IdenticonRenderer().Render(code, SIZE); user.ProfileImageBytes = ProfileImageHelper.ConvertForDb(profileImage); }
public Bitmap GetIdenticon(int size) { IdenticonRenderer identiconRenderer = new IdenticonRenderer(); return identiconRenderer.Render(IdenticonCode, size, IdenticonColor); }