System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(Fupload_WatermarkImage.PostedFile.InputStream);

                // 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
                {
                    // Create an Image Object and fill it with the stream of the selected image.
                    System.Drawing.Image image = System.Drawing.Image.FromStream(Fupload_WatermarkImage.PostedFile.InputStream);
                   
                    // 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(“C Sharp Corner”, 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(Fupload_WatermarkImage.PostedFile.FileName);

                    // Getting the file extension of the selected image
                    string FileExtension = Path.GetExtension(Fupload_WatermarkImage.PostedFile.FileName);

                    // 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;

                    newImage.Save(Server.MapPath(path));

                    // Assigning the uploaded image url to the Image control.
                    imgUploadedImage.ImageUrl = path;

                    // 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);
                    }
                }

Ramazan CESUR…