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.
- private Image GenerateAvtarImage(String text, Font font, Color textColor, Color backColor, string filename)
- {
- //first, create a dummy bitmap just to get a graphics object
- Image img = new Bitmap(1, 1);
- Graphics drawing = Graphics.FromImage(img);
- //measure the string to see how big the image needs to be
- SizeF textSize = drawing.MeasureString(text, font);
- //free up the dummy image and old graphics object
- img.Dispose();
- drawing.Dispose();
- //create a new image of the right size
- img = new Bitmap(110, 110);
- drawing = Graphics.FromImage(img);
- //paint the background
- drawing.Clear(backColor);
- //create a brush for the text
- Brush textBrush = new SolidBrush(textColor);
- //drawing.DrawString(text, font, textBrush, 0, 0);
- 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();
- // Get the array of FontFamily objects.
- fontFamilies = installedFontCollection.Families;
- // The loop below creates a large string that is a comma-separated
- // list of all font family names.
- 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);
- }
- }
- 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#:

Tushar TyagiPosted Jun 11, 2018, 5:02 AM
Hy Kirtan, <telerik:RadListView ID="ListViewDirectory" DataKeyNames="EmpId" OnItemDataBound="ListViewDirectory_OnItemDataBound" OnNeedDataSource="ListViewDirectory_NeedDataSource" runat="server" RenderMode="Lightweight" BackColor="Red" Skin="Office2007"> <LayoutTemplate> <div class=" RadListView_Default"> <ul id="itemPlaceholder" runat="server"> <li></li> </ul> </div> </LayoutTemplate> <ItemTemplate> <div class="altItemFieldset"> <div class="productItem"> <br /> <asp:ImageButton ID="imgProfileBtn" runat="server" AlternateText='<%# Eval("EmpId") %>' Style="border-radius: 76px !important; width: 115px !important; height: 115px !important;" CommandName="Select" ImageUrl='<%# String.Format("../Theme/assets/customImages/noprofilepic.png", Eval("FullName").ToString()) %>'></asp:ImageButton> <p> <b style="color: red;"><%#Eval("FullName") %></b> </p> <p><%# Eval("JobTitle") %> (<%# Eval("DeptName") %>)</p> <p><i class="icon fa fa-envelope" aria-hidden="true" style="font-size: 20px;"></i> <%# Eval("Emailid") %></p> <p style="<%# Eval("Phone").NullToString()==""?"display:none;": "" %>"><i class="icon fa fa-phone-square" aria-hidden="true" style="font-size: 20px;"></i> <%# Eval("Phone") %></p> </div> </div> </ItemTemplate> this my code and how to show the profile image with first name and last name with the given value which is come from database "FullName"
Tushar TyagiPosted Jun 11, 2018, 5:01 AM
Hy kirtan ,
Hiren NayeePosted Apr 7, 2017, 9:46 AM
Nice very interesting post
Shubham KumarPosted Mar 3, 2016, 12:26 AM
Interesting
Raja TPosted Mar 2, 2016, 11:15 PM
Nice,Thanks for sharing..
Hemant SrivastavaPosted Mar 2, 2016, 2:25 PM
Interesting
Sibeesh VenuPosted Mar 2, 2016, 8:23 AM
Nice Share
Krishna Rajput SinghPosted Mar 2, 2016, 5:38 AM
http://www.c-sharpcorner.com/UploadFile/a5f59f/how-to-create-log-in-form-in-visual-studio-and-connection-wi/
Mohammed IbrahimPosted Mar 2, 2016, 2:54 AM
nice
Ammar ShaukatPosted Mar 1, 2016, 12:48 PM
Good
Ankur MistryPosted Mar 1, 2016, 11:10 AM
Nice share Kirtan Parmar
Vignesh ManiPosted Mar 1, 2016, 5:44 AM
Nice one