protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (Fupload_WatermarkImage.HasFile)
        {
            string FileType = Path.GetExtension(Fupload_WatermarkImage.PostedFile.FileName).ToLower().Trim();

            // Checking the format of the uploaded file.
            if (FileType != “.jpg” && FileType != “.png” && FileType != “.gif” && FileType != “.bmp”)
            {
                string alert = “alert(‘File Format Not Supported. Only .jpg, .png, .bmp and .gif file formats are allowed.’);”;
                ScriptManager.RegisterStartupScript(this, GetType(), “JScript”, alert, true);
            }
            else
            {
                FileStream stream = new FileStream(@”C:UsersmacPicturesKinectScreenshot-CoordinateMapping-04-31-47.png”, FileMode.Open);
                // Getting the stream of the selected image and assign it to the Image Object.
                System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(stream);
                stream.Close();

                // Checking if the Width or height is greater than 250px
                if (UploadedImage.PhysicalDimension.Width > 2500 || UploadedImage.PhysicalDimension.Height > 2500)
                {
                    string alert = “alert(‘Image size should be exactly 250*250 dimension. Your Current Image width is ” + UploadedImage.PhysicalDimension.Width + ” and height is ” + UploadedImage.PhysicalDimension.Height + “.’);”;
                    ScriptManager.RegisterStartupScript(this, GetType(), “JScript”, alert, true);
                }
                else
                {
                    string filename = System.IO.Path.GetFileName(“KinectScreenshot-CoordinateMapping-04-31-47.png”);

                    FileStream str = new FileStream(@”C:UsersmacPicturesKinectScreenshot-CoordinateMapping-04-31-47.png”, FileMode.Open);

                    System.Drawing.Image image = System.Drawing.Image.FromStream(str);
                   
                    // Get the height and width of an image.
                    int width = image.Width;
                    int height = image.Height;

                    // Creating the Bitmap Object and assigning the width and height of the image.
                    // It internally creates a graphical image with the same dimension.
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
                   
                    // Creating a Graphics Object from the Bitmap Object.
                    System.Drawing.Graphics graphics1 = System.Drawing.Graphics.FromImage((System.Drawing.Image)bmp);
                   
                    // Assigning few properties to the Graphics Object, to maintain the quality of the image.
                    graphics1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    graphics1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics1.Clear(System.Drawing.Color.Transparent);
                    graphics1.DrawImage(image, 0, 0, width, height);

                    // Create the Font and the Brush Object and assign appropriate parameters inorder to add watermark to the image.
                    System.Drawing.Font font = new System.Drawing.Font(“Arial”, 20);
                    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Aqua);

                    // Drawstring actually writes / watermarks the image with the specified content.
                    // Parameters contain :- Watermark Content, font style, brush used, x and y position for the string to be written
                    graphics1.DrawString(“”, font, brush, 25F, 115F);
                   
                    // Create the image object from the bitmap and then use it to save the watermarked image in the folder.
                    System.Drawing.Image newImage = (System.Drawing.Image)bmp;

                    // Getting the file name of the selected image
                    string FileName = Path.GetFileNameWithoutExtension(“KinectScreenshot-CoordinateMapping-04-31-47”);

                    // Getting the file extension of the selected image
                    string FileExtension = Path.GetExtension(@”C:UsersmacPicturesKinectScreenshot-CoordinateMapping-04-31-47.png”);

                    // Creating a complete relative path for storing the image. And also attaching the datetime stamp with the image name.
                    string path = “Watermarked Images/” + FileName + DateTime.Now.ToString(“yyyy-MM-dd HHmmtt”) + FileExtension;
                   //
               
                    // Assigning the uploaded image url to the Image control.
                    imgUploadedImage.ImageUrl = @”C:UsersmacPicturesKinectScreenshot-CoordinateMapping-04-31-47.png”;

                    // Disposing the Graphics Object and other resources from the memory.
                    graphics1.Dispose();

                    if (!String.IsNullOrEmpty(imgUploadedImage.ImageUrl))
                    {
                        // Showing a notification of success after uploading.
                        string alert = “alert(‘Image uploaded successfully’);”;
                        ScriptManager.RegisterStartupScript(this, GetType(), “JScript”, alert, true);
                    }
                    str.Close();
                }
            }
        }

        else
        {
            string alert = “alert(‘Please upload an image first.’);”;
            ScriptManager.RegisterStartupScript(this, GetType(), “JScript”, alert, true);
        }

Ramazan CESUR