设置默认的用户配置文件图片,它们的缩写的图像 [英] Set default user profile picture to an image of their initials

查看:152
本文介绍了设置默认的用户配置文件图片,它们的缩写的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

越来越多,我看到很多公司设置用户的默认配置文件图片,如图下面的截图是从谷歌主页...

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...

如何谷歌已经做到了这一点?
什么PHP和C#的API可用于实现这一目标?

How have Google achieved this? What APIs for PHP and C# can be used to achieve this?

推荐答案

简单使用的.Net基础库。你可以在它按您的要求更改。
基本目的是创建用户档案头像图片,如果用户不使用特定的图像轮廓默认的图像将使用。
两种常见类型的图像,我们需要进行矩形和放大器;圈子。

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.

有关圆形图片

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;
    }

有关矩形图像:

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.

这篇关于设置默认的用户配置文件图片,它们的缩写的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆