Nikey646

C# Identicons

May 11th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9.     public sealed class Identicon : Control
  10.     {
  11.         public enum HashAlgorithms
  12.         {
  13.             MD5,
  14.             SHA1,
  15.             SHA256,
  16.             SHA384,
  17.             SHA512,
  18.             RIPEMD160
  19.         }
  20.  
  21.         private string Identity = string.Empty;
  22.         private HashAlgorithms HashType = HashAlgorithms.SHA256;
  23.         private Double Margin = .08;
  24.  
  25.         public Identicon()
  26.         {
  27.             if (string.IsNullOrEmpty(this.Identity))
  28.             {
  29.                 this.Identity = Guid.NewGuid().ToString();
  30.             }
  31.         }
  32.  
  33.                 protected override void OnPaint(PaintEventArgs e)
  34.         {
  35.             var hash = this.HashString(this.Identity, this.HashType);
  36.             var size = (this.Size.Width < this.Size.Height) ? this.Size.Width : this.Size.Height;
  37.             var margin = Math.Floor(size*this.Margin);
  38.             var cell = Math.Floor((size - (margin * 2)) / 5);
  39.  
  40.             var b = new Bitmap(this.Size.Width, this.Size.Height);
  41.             var g = Graphics.FromImage(b);
  42.  
  43.             var hsl = new HSLColor(
  44.                 (Convert.ToInt32(hash.Substring(hash.Length - 7), 16) / 0xfffffff),
  45.                 .5,
  46.                 .7);
  47.  
  48.             var rgb = (Color)hsl;
  49.  
  50.             var bg = this.BackColor;
  51.             var fg = rgb;
  52.  
  53.             for (var i = 0; i < 15; i++)
  54.             {
  55.                 var color = (Convert.ToInt32(hash[i].ToString(), 16) % 2) == 1 ? bg : fg;
  56.  
  57.                 if (i < 5)
  58.                 {
  59.                     Rectangle(2 * cell + margin, i * cell + margin, cell, cell, new SolidBrush(color), g);
  60.                 }
  61.                 if (i < 10)
  62.                 {
  63.                     this.Rectangle(1 * cell + margin, (i - 5) * cell + margin, cell, cell, new SolidBrush(color), g);
  64.                     this.Rectangle(3 * cell + margin, (i - 5) * cell + margin, cell, cell, new SolidBrush(color), g);
  65.                 }
  66.                 if (i < 15)
  67.                 {
  68.                     this.Rectangle(0 * cell + margin, (i - 10) * cell + margin, cell, cell, new SolidBrush(color), g);
  69.                     this.Rectangle(4 * cell + margin, (i - 10) * cell + margin, cell, cell, new SolidBrush(color), g); 
  70.                 }
  71.             }
  72.  
  73.             e.Graphics.DrawImage(b, new Point(0, 0));
  74.  
  75.             base.OnPaint(e);
  76.         }
  77.  
  78.         private void Rectangle(double x, double y, double w, double h, SolidBrush color, Graphics g)
  79.         {
  80.             g.FillRectangle(color, new RectangleF((float)x, (float)y + ((this.Size.Width < this.Size.Height) ? this.Size.Width : this.Size.Height * 9 / 100), (float)w, (float)h));
  81.         }
  82.  
  83.         private string HashString(string str, HashAlgorithms Algorithm)
  84.         {
  85.             var hasher = HashAlgorithm.Create(Algorithm.ToString());
  86.             var strBytes = Encoding.UTF8.GetBytes(str);
  87.  
  88.             var hashed = hasher.ComputeHash(strBytes);
  89.             var sb = new StringBuilder();
  90.  
  91.             for (var i = 0; i < hashed.Length; i++)
  92.             {
  93.                 sb.Append(hashed[i].ToString("x2"));
  94.             }
  95.  
  96.             return sb.ToString();
  97.         }
  98.  
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment