using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class Resize_Image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public Size OriginalImageSize { get; set; }        //Store original image size.
    public Size NewImageSize { get; set; }

    public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight, int Maxwidth)
    {
        var forheight = (double)maxHeight / image.Height;
        var forwidth = (double)Maxwidth / image.Width;

        var newWidth = (int)(image.Width * forwidth);
        var newHeight = (int)(image.Height * forheight);
        var newImage = new Bitmap(newWidth, newHeight);
        using (var g = Graphics.FromImage(newImage))
        {
            g.DrawImage(image, 0, 0, newWidth, newHeight);
            g.Dispose();
        }
        
        return newImage;

    }




    protected void Button1_Click(object sender, EventArgs e)
    {
        lblmsg.Text = "";


        if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
        {
            Guid uid = Guid.NewGuid();

            string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string SaveLargeimage = Server.MapPath("Largeimage") + "\\"  + fn;
            string SaveMediumimages = Server.MapPath("MediumImages") + "\\" + "mediumimg_" + uid + fn;
            string Savesmallimages = Server.MapPath("Smallimages") + "\\" + "smallimg_" + uid + fn;
            string Savethumbnailimages = Server.MapPath("Thumbnailimages") + "\\" + "thumbnailimg_" + uid + fn;
            try
            {
                string fileExtention = File1.PostedFile.ContentType;
                int fileLenght = File1.PostedFile.ContentLength;
                if (fileExtention == "image/gif" || fileExtention == "image/jpeg" || fileExtention == "image/jpg")
                {
                    //if (fileLenght <= 1048576)
                    if (fileLenght <= 204800)
                    {
                        System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
                        System.Drawing.Image Mediumimage = ScaleImage(bmpPostedImage, 200, 205);
                        System.Drawing.Image objsmallimage = ScaleImage(bmpPostedImage, 194, 166);
                        System.Drawing.Image objthumbnailimage = ScaleImage(bmpPostedImage, 80, 80);
                        bmpPostedImage.Save(SaveLargeimage, ImageFormat.Png);
                        Mediumimage.Save(SaveMediumimages, ImageFormat.Png);
                        objsmallimage.Save(Savesmallimages, ImageFormat.Png);
                        objthumbnailimage.Save(Savethumbnailimages, ImageFormat.Png);
                        objsmallimage.Dispose();
                        Mediumimage.Dispose();
                        objthumbnailimage.Dispose();
                        bmpPostedImage.Dispose();
                       
                        lblmsg.Text = "The file has been uploaded.";
                        lblmsg.Style.Add("Color", "Green");
                    }
                    else
                    {
                        lblmsg.Text = "Image size cannot be more then 200 KB.";
                        lblmsg.Style.Add("Color", "Red");
                    }
                }
                else
                {
                    lblmsg.Text = "Invaild Format!";
                    lblmsg.Style.Add("Color", "Red");
                }
            }
            catch (Exception ex)
            {
                lblmsg.Text = "Error: " + ex.Message;
                lblmsg.Style.Add("Color", "Red");
            }
        }
    }
}