Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a circle avatar image in .net

Tags:

c#

.net

graphics

I want to create a default avatar image which is a circle with initials in it. I want to do this on the server side as a png. Is this possible using the .net graphics library?

like image 812
Tija Avatar asked Feb 15 '15 15:02

Tija


1 Answers

I ended up doing this. Thanks for pointing me in the right direction TaW

public ActionResult Avatar()
        {
            using (var bitmap = new Bitmap(50, 50))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(Color.White);
                    using (Brush b = new SolidBrush(ColorTranslator.FromHtml("#eeeeee")))
                    {

                        g.FillEllipse(b, 0, 0, 49, 49);
                    }

                    float emSize = 12;
                    g.DrawString("AM", new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular),
                        new SolidBrush(Color.Black), 10, 15);
                }

                using (var memStream = new System.IO.MemoryStream())
                {
                    bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
                    var result = this.File(memStream.GetBuffer(), "image/png");
                    return result;
                }
            }
        }
like image 168
Tija Avatar answered Sep 20 '22 05:09

Tija