Wednesday, January 15, 2014

Make Your Own Gravatar using C# in ASP.NET

Now a days its a style to use Gravatar (Globally Recognized Avatar) as default profile pictures in various websites. You can assign this gravatar in ASP.NET using AJAX Toolkit but if you are looking for the gravatar using your name or something other text then the following example is the best one for you.

Here I will show you how to convert a text into an image using C# in ASP.NET. For this we need to take class Graphics and to generate the bitmap we need class Bitmap.

So lets start.

First take a text box to take input the text from user and add a button to convert it into image
Text : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Convert & Download"  onclick="Button1_Click" />.

Add namespaces

using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;


Now on the Button1_Click

protected void Button1_Click(object sender, EventArgs e)
        {
            string t = TextBox1.Text;
            Bitmap bitmap = new Bitmap(1, 1);
            Font font = new Font("Algerian", 150, FontStyle.Regular, GraphicsUnit.Pixel);
            Graphics graphics = Graphics.FromImage(bitmap);
            int width = (int)graphics.MeasureString(t, font).Width;
            int height = (int)graphics.MeasureString(t, font).Height;
            bitmap = new Bitmap(bitmap, new Size(width, height));
            graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            graphics.DrawString(t, font, new SolidBrush(Color.FromArgb(1, 0, 0)), 0, 0);
            graphics.Flush();
            graphics.Dispose();
            string fileName = TextBox1.Text + ".jpg";
            bitmap.Save(Server.MapPath("~/image/") + fileName, ImageFormat.Jpeg);
        }  

Now the image is saving into the "~/image/{text-name}.jpeg".

To create a bitmap :
Bitmap bitmap = new Bitmap(1, 1);  

To set front :
Font font = new Font("Algerian", 150, FontStyle.Regular, GraphicsUnit.Pixel);

arguments passing are 
  • Font family name
  • Font size
  • Font style(Regular,Bold,Italics,Strike out,Underline)
  • Graphics unit(Display,Document,Inch,Pixel,Point,World) 
To set color :
graphics.DrawString(t, font, new SolidBrush(Color.FromArgb(1, 0, 0)), 0, 0);

To upload the image :
bitmap.Save(Server.MapPath("~/image/") + fileName, ImageFormat.Jpeg);

Saving the image under image folder with the  name of  filename with the extension of Jpeg

There are another way to make a gravatar wit the help of FillRectangle() method.

private Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
        {
            Bitmap bmp = new Bitmap(width, Height);
            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                Font font = new Font(fontname, fontsize);
                graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
                graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
                graphics.Flush();
                font.Dispose();
                graphics.Dispose();
            }
            return bmp;
        }


It will return a bmp file, catch that bmp file and using  bitmap.Save() method save the gravatar.



Output :



With this Url of the image you can do whatever you want to do.

Enjoy the code. :)

Download the full code here.

1 comment:

  1. This blog is really helpful regarding all educational knowledge I earned. It covered a great area of subject which can assist a lot of needy people. Everything mentioned here is clear and very useful. Computer Graphics Programs

    ReplyDelete

Popular Posts

Pageviews