Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default user profile picture to an image of their initials

More and more I am seeing companies set a user's default profile picture as shown in the screenshot below which is from the Google homepage...

enter image description here

How have Google achieved this?

like image 886
ProSamHDx Avatar asked Oct 14 '15 08:10

ProSamHDx


People also ask

How do I change my profile picture back to initials?

In the top right hand corner of the Teams screen, there is a circle containing your initials (by default) or a photo. If you click on this, you will get your personal options (Settings).

Why does my profile picture in Teams show my initials?

When logging into your account on Teams, some of you may notice your initials in place of where your profile picture should be. If your profile picture shows your initial in Teams, then chances are that you are yet to upload a picture to be set as your profile photo on Microsoft Teams.

How do you put initials on Microsoft teams?

Click File > Options. In the Options dialog box, change your user name and initials in the Personalize your copy of Microsoft Office section.


1 Answers

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.

like image 107
Galaxy Starts Avatar answered Oct 18 '22 13:10

Galaxy Starts