When Windows developers need a unique value, they often utilize a Globally Unique Identifier (GUID). Microsoft uses the term GUID for a unique number that identifies an entity.

A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required. There's a very low probability that this type of identifier will be duplicated.

This is the format of a typical GUID:


Sample Program to Create a GUID in .NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            //It will always print Unique GUID using NewGuid() of GUID Class.
            Console.WriteLine("GUID : " + System.Guid.NewGuid());

 Now, we’re moving to some advance concept in using GUID.

Here is the sample of a program that will show use the use of Image GUID to validate image format.

While developing any Web Application there may be a requirement to Upload User’s Image to the server,  so we need to validate that image file before uploading to the server, because there may be a chance that user may upload malicious script.

For validating Image File, generally we check the extension of the uploaded file before uploading the file, if found appropriate extension of the file, we proceed it for uploading to the server. But this validation is not quite enough to prevent uploading any malicious script to the server, because user may easily change the extension of script file to upload it to the server.  Then?? How can we tackle this problem?

Using Image GUID, we can overcome to this problem. Following program will show you how to use Image GUID in ASP.NET.

1.      Create a new WebSite- File> New> Website. Select ASP.NET Web Site from the Templates. Choose Location to store the file and Give some name to it. In my case it is Default.aspx

2.      Now, Drag Drop a FileUpload, Label and a Button Control to the Web Page as following.


3.      Write following Codes on Click Event Handler of Upload Button.

protected void btnUpload_Click(object sender, EventArgs e)


        string ImageFormat=string.Empty;



            // Checking whether FileUpload has any File Selected or Not.

            if (FileUpload1.HasFile)


                //Retrieving the Content of File uploaded by FileUpload

                System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);


                // This will match the GUID of Uploaded File to GUID of Actual Image Format.

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)

                    ImageFormat = "bmp";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Gif.Guid)

                    ImageFormat = "gif";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)

                    ImageFormat = "icon";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)

                    ImageFormat = "jpeg";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Png.Guid)

                    ImageFormat = "png";



                    // It will raise an exception if Uploaded File Extension

                    //not matched with given Image Format Extensions GUID.

                    throw new System.Exception();                    


                lblError.Text = "File Uploaded Successfully.";



        catch (Exception ex)


            lblError.Text = "Attempt to Upload Invalid Image File.";




Just try to upload an Image file with file extension bmp, gif, icon, jpeg, png, you will be able to upload these Files. 

Now, change the extension of any doc,docx or any other file’s extension to bmp, gif, icon, jpeg, png and try upload it. 

You will get an Error that “Attempt to Upload Invalid Image File”, because here we have put the code to match the GUID of the Uploaded File to GUID of Actual file Format.








  Modified On Nov-30-2017 03:24:05 AM

Leave Comment