Create a circle avatar image in .net

前端 未结 1 1306
北恋
北恋 2021-02-06 13:54

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?

相关标签:
1条回答
  • 2021-02-06 14:25

    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;
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题