We are used to displaing one default image when a user signs up on a website, and until the user changes their profile picture we display that default profile picture, I am sure you have noticed this on many websites.
Today we will learn in this article how to generate an image dynamically by taking the user’s first and last names.
I have created this demo in MVC application, you can use this on any C# platform, even on the Windows form application, as we are using pure C# to generate image from text.
Whena user signs up on your platform you can simply call this method and that will return the saved path of the image, and you can save that image path on your database along with that user.
- private Image GenerateAvtarImage(String text, Font font, Color textColor, Color backColor, string filename)
- {
-
- Image img = new Bitmap(1, 1);
- Graphics drawing = Graphics.FromImage(img);
-
-
- SizeF textSize = drawing.MeasureString(text, font);
-
-
- img.Dispose();
- drawing.Dispose();
-
-
- img = new Bitmap(110, 110);
-
- drawing = Graphics.FromImage(img);
-
-
- drawing.Clear(backColor);
-
-
- Brush textBrush = new SolidBrush(textColor);
-
-
- drawing.DrawString(text, font, textBrush, new Rectangle(-2, 20, 200, 110));
-
- drawing.Save();
-
- textBrush.Dispose();
- drawing.Dispose();
-
- img.Save(Server.MapPath("~/Images/" + filename + ".gif"));
-
- return img;
-
- }
This method will generate a 110 x 100 size gif format image and store that image into the “images” folder; you can change size and format as per your requirement. For generatin an image you can pass these values in the method:
- public void CreateProfilePicture()
- {
- Font font = new Font(FontFamily.GenericSerif, 45, FontStyle.Bold);
- Color fontcolor = ColorTranslator.FromHtml("#FFF");
- Color bgcolor = ColorTranslator.FromHtml("#83B869");
- GenerateAvtarImage("KS", font, fontcolor, bgcolor, "test");
- }
It will generate the following image,
You can notice here I have set a fixed color for font and background o the image, you can pass here any colors like, ColorTranslator.FromHtml allows us to pass color hex value.
You can use any font style best suited to your website design, I have set here “GenericSerif” font family, let’s check how many other font families are available to set,
- public void testAllFonts()
- {
- string familyName;
- FontFamily[] fontFamilies;
- Font font = null;
-
- InstalledFontCollection installedFontCollection = new InstalledFontCollection();
-
-
- fontFamilies = installedFontCollection.Families;
-
-
-
- int count = fontFamilies.Length;
- for (int j = 0; j < count; ++j)
- {
- familyName = fontFamilies[j].Name;
-
- if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold)) {
- font = new Font(familyName, 45, FontStyle.Bold);
- }
- else if(fontFamilies[j].IsStyleAvailable(FontStyle.Regular)){
- font = new Font(familyName, 45, FontStyle.Regular);
- }
- else if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout)) {
- font = new Font(familyName, 45, FontStyle.Strikeout);
- }
-
- Color fontcolor = ColorTranslator.FromHtml("#FFF");
- Color bgcolor = ColorTranslator.FromHtml("#83B869");
- GenerateAvtarImage("KP", font, fontcolor, bgcolor, familyName + j);
- }
- }
I have created a collection of some of the best colors, now we will set one random color to each image.
- public List<string> ColorsCode()
- {
- List<string> list = new List<string>();
- list.Add("#EEAD0E");
- list.Add("#8bbf61");
-
- list.Add("#DC143C");
- list.Add("#CD6889");
- list.Add("#8B8386");
- list.Add("#800080");
- list.Add("#9932CC");
- list.Add("#009ACD");
- list.Add("#00CED1");
- list.Add("#03A89E");
-
- list.Add("#00C78C");
- list.Add("#00CD66");
- list.Add("#66CD00");
- list.Add("#EEB422");
- list.Add("#FF8C00");
- list.Add("#EE4000");
-
- list.Add("#388E8E");
- list.Add("#8E8E38");
- list.Add("#7171C6");
-
- return list;
- }
Now write the following code that will select one random color from our color list.
- Color bgcolor = ColorTranslator.FromHtml(ColorsCode().OrderBy(a => Guid.NewGuid()).FirstOrDefault());
If you are using a different font family you need to set font size according to that font; here I have set a 45 font size for the “GenericSerif” font family , and it also depends on what size of image you are generating.
Finally, if you implement this in your website it will look like the image below,
Read more articles on C#: