Set default user profile picture to an image of their initials

不想你离开。 提交于 2019-11-29 00:42:44

Simple use .Net basic libraries. You can change in it as per your requirement. basic purpose is for creating user Profile Avatar image if user not use specific image for profile default image will be use. Two common types of images we need be made Rectangle & Circle.

For Circle Image

public MemoryStream GenerateCircle(string firstName, string lastName)
    {
        var avatarString = string.Format("{0}{1}", firstName[0], lastName[0]).ToUpper();

        var randomIndex = new Random().Next(0, _BackgroundColours.Count - 1);
        var bgColour = _BackgroundColours[randomIndex];

        var bmp = new Bitmap(192, 192);
        var sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;
        sf.LineAlignment = StringAlignment.Center;

        var font = new Font("Arial", 72, FontStyle.Bold, GraphicsUnit.Pixel);
        var graphics = Graphics.FromImage(bmp);

        graphics.Clear(Color.Transparent);
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        using (Brush b = new SolidBrush(ColorTranslator.FromHtml("#" + bgColour)))
        {
            graphics.FillEllipse(b, new Rectangle(0, 0, 192, 192));
        }
        graphics.DrawString(avatarString, font, new SolidBrush(Color.WhiteSmoke), 95, 100, sf);
        graphics.Flush();

        var ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Png);
        return ms;
    }

For Rectangle image :

public MemoryStream GenerateRactangle(string firstName, string lastName)
    {
        var avatarString = string.Format("{0}{1}", firstName[0], lastName[0]).ToUpper();

        var randomIndex = new Random().Next(0, _BackgroundColours.Count - 1);
        var bgColour = _BackgroundColours[randomIndex];

        var bmp = new Bitmap(192, 192);
        var sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;
        sf.LineAlignment = StringAlignment.Center;

        var font = new Font("Arial", 72, FontStyle.Bold, GraphicsUnit.Pixel);
        var graphics = Graphics.FromImage(bmp);

        graphics.Clear((Color)new ColorConverter().ConvertFromString("#" + bgColour));
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        graphics.DrawString(avatarString, font, new SolidBrush(Color.WhiteSmoke), new RectangleF(0, 0, 192, 192), sf);

        graphics.Flush();

        var ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Png);

        return ms;
    }

For generating background random colors can be use :

private List<string> _BackgroundColours = new List<string> { "339966", "3366CC", "CC33FF", "FF5050" };

i hope it will help your,please pass your suggestions to improve it.

You could save one image for each letter as

/your_path/a.png
/your_path/b.bng, 
/your_path/c.png
...

When loading the user profile, if the user does not have a picture filename in the db, you load the filename of the image with his/her initial instead.

For instance...

SELECT name, address, 
COALESCE(picture, CONCAT("/your_path/", LEFT(name, 1),".png")) as picture
FROM users_table WHERE... ; 

This way, when a user have a picture, the picture filename is loaded, if not, the filename for the file with his initials is loaded (assuming you are using mysql to store your user's data)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!