In this article you will learn how to create a default profile image dynamically from first and last name in C#.
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.
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:
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.
Now write the following code that will select one random color from our color list.
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.
Read more articles on C#:
LINQ Quick Reference with C#