Contact Form

Name

Email *

Message *

Dynamically image resize Or thumb and unique name upload in asp,net




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="fileupload.aspx.cs" Inherits="LuqmanSolutions.fileupload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">  
    <div>  
        <h2 style="color:Green">asp.net FileUpload example: File Rename</h2>  
        <asp:Label   
             ID="Label1"   
            
             runat="server"   
             Font-Size="Large"  
             ForeColor="OrangeRed"  
             >  
        </asp:Label>  
        <br /><br />  
        <asp:TextBox ID="txttitleone" runat="server"></asp:TextBox> <br /><br />  
        <asp:TextBox ID="txttitletwo" runat="server" 
           ></asp:TextBox>
       
        <asp:FileUpload   
             ID="FileUpload1"   
             runat="server"   
              Font-Size="Large" 
             ForeColor="OrangeRed" 
             />  
       
<br /><br />
             <asp:Button ID="btnSave" runat="server" Text="  Save " 
            onclick="btnSave_Click" style="height: 26px" />     
    </div>  
    </form>  




</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BE_Luqman;
using BL_Luqman;


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


        }
       
       

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BESlider be = new BESlider();
            be.Pictitleone = txttitleone.Text;
            be.Pictitletwo = txttitletwo.Text;

            if (FileUpload1.HasFile)
            {
                string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string fileExtension = GetExtension(filename);
                string guid = Guid.NewGuid().ToString();
                filename = guid + "." + fileExtension;
               FileUpload1.SaveAs(MapPath("~/images/F/" + filename));
                System.Drawing.Image img1 = System.Drawing.Image.FromFile(MapPath("~/images/F/") + filename);
                //System.Drawing.Image bmp1 = img1.GetThumbnailImage(1400, 500, null, IntPtr.Zero);
                //bmp1.Save(MapPath("~/images/S/") + filename);
                be.Picname=filename.ToString();
                System.Drawing.Image bmp2 = img1.GetThumbnailImage(150, 59, null, IntPtr.Zero);
                bmp2.Save(MapPath("~/images/L/") + filename);
                be.Picthumb = filename.ToString();
              
            }
            else
            {
                Label1.Text = "First select a file.";
            }

            new BLSlider().Get_Data_SliderInsert(be);
            txttitleone.Text="";
            txttitletwo.Text="";

        }

        private string GetExtension(string FileName)
        {
            string[] split = FileName.Split('.');
            string Extension = split[split.Length - 1];
            return Extension;
        }


    
    }
 }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BE_Luqman;

namespace DAL_Luqman
{
    public class DALSlider
    {
        private string connect = MyConnectionString.path;
        public List<BESlider> show()
        {
            List<BESlider> list = new List<BESlider>();
            SqlConnection con = new SqlConnection(connect);
            SqlCommand cmd = new SqlCommand(MyProc.Name.spshow.ToString(), con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        BESlider BESlider = new BESlider();
                        BESlider.Picid = Convert.ToInt32(sdr["Picid"]);
                        BESlider.Picname = Convert.ToString(sdr["Picname"]);
                        BESlider.Picthumb = Convert.ToString(sdr["Picthumb"]);
                        BESlider.Pictitleone = Convert.ToString(sdr["Pictitleone"]);
                        BESlider.Pictitletwo = Convert.ToString(sdr["Pictitletwo"]);
                        list.Add(BESlider);

                    }

                }



            }
            catch (Exception exe)
            {
                throw exe;
            }
            finally
            {

                con.Close();

            }

            return list;

        }
        public void Get_Data_SliderInsert(BESlider be)
        {
            SqlConnection con = new SqlConnection(connect);
            SqlCommand cmd = new SqlCommand(MyProc.Name.spsliderinsert.ToString(), con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Picname", be.Picname);
            cmd.Parameters.AddWithValue("@Picthumb", be.Picthumb);
            cmd.Parameters.AddWithValue("@Pictitleone", be.Pictitleone);
            cmd.Parameters.AddWithValue("@Pictitletwo", be.Pictitletwo);
            con.Open();
            cmd.ExecuteNonQuery();


        }
        public void Get_Data_SliderUpdate(BESlider be)
        {
            SqlConnection con = new SqlConnection(connect);
            SqlCommand cmd = new SqlCommand(MyProc.Name.spsliderupdate.ToString(), con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Picid", be.Picid);
            cmd.Parameters.AddWithValue("@Picname", be.Picname);
            cmd.Parameters.AddWithValue("@Picthumb", be.Picthumb);
            cmd.Parameters.AddWithValue("@Pictitleone", be.Pictitleone);
            cmd.Parameters.AddWithValue("@Pictitletwo", be.Pictitletwo);
            con.Open();
            cmd.ExecuteNonQuery();

        }


    }
}





Image upload Genrat thumb and resize in Asp.net




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="slider2.aspx.cs" Inherits="slider.slider2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="labelMessage" runat="server" Text="Browse Image:" ForeColor="navy" Width="220"></asp:Label>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Upload" runat="server" Text="Upload" OnClick="Upload_Click" />
    </div>

    <div>
        <asp:Label ID="label1" runat="server" Text="Normal Image (Full size): " ForeColor="navy" Width="220"></asp:Label>
        <asp:Image ID="NormalImage" runat="server" />
    </div>

    <div>
        <asp:Label ID="label2" runat="server" Text="Thumbnail Small Image (50x50)px: " ForeColor="navy" Width="220"></asp:Label>
        <asp:Image ID="ThumbnailImageS" runat="server" />
    </div>

    <div>
        <asp:Label ID="label3" runat="server" Text="Thumbnail Large Image (100x100)px: "
ForeColor="navy" Width="220"></asp:Label>
        <asp:Image ID="ThumbnailImageM" runat="server" />
    </div>
</form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

        }

        protected void Upload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(MapPath("~/Image/" + FileUpload1.FileName));
                System.Drawing.Image img1 = System.Drawing.Image.FromFile(MapPath("~/image/") + FileUpload1.FileName);

                System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero);
                bmp1.Save(MapPath("~/thumbnail/S/") + FileUpload1.FileName);
                System.Drawing.Image bmp2 = img1.GetThumbnailImage(100, 100, null, IntPtr.Zero);
                bmp2.Save(MapPath("~/thumbnail/L/") + FileUpload1.FileName);
                NormalImage.ImageUrl = "~/Image/" + FileUpload1.FileName;
                ThumbnailImageS.ImageUrl = "~/thumbnail/S/" + FileUpload1.FileName;
                ThumbnailImageM.ImageUrl = "~/thumbnail/L/" + FileUpload1.FileName;
            }
        }
    }
}


Upload & save generated thumbnail ASP.net




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="slider.aspx.cs" Inherits="slider.slider" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
     <form id="Form1"  runat="server">
        <h4>Upload & save generated thumbnail
        </h4>
        Select File To Upload to Server:
        <input id="MyFile" type="file" runat="server" />
        <br/>
        Desired Width of Thumbnail&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp:
        <asp:TextBox id="w" runat="server" name="w" Width="38px"></asp:TextBox>
        <br/>
Desired Height of Thumbnail&nbsp;&nbsp&nbsp;&nbsp&nbsp;:
        <asp:TextBox id="h" runat="server" name="h" Width="38px"></asp:TextBox>
        <br/>
         <asp:Button ID="Submit1" runat="server" Text="Upload!" onclick="Submit1_Click"  />
     
        <br/>
        <br/>
        <hr/>
        <div id="FileDetails" runat="server" visible="false">FileName: <span id="FileName" runat="server"></span><br/>
        ContentType: <span id="MyContentType" runat="server"></span><br/>
        ContentLength: <span id="ContentLength" runat="server">bytes
            </span><br/><br/>
        Original Image : <asp:Image id="Image1" runat="server"></asp:Image></br>
Resized  Image : <asp:Image id="Image2" runat="server"></asp:Image>
        </div>
    </form>
</body>
</html>


Upload & save generated thumbnail


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

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

        }

 

        //this function is reqd for thumbnail creation
        public bool ThumbnailCallback()
        {
            return false;
        }

        protected void Submit1_Click(object sender, EventArgs e)
        {

            String UploadedFile = MyFile.PostedFile.FileName;
            int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;

            //to retrieve only Filename from the complete path
            String UploadedFileName = UploadedFile.Substring(ExtractPos, UploadedFile.Length - ExtractPos);


            // Display information about posted file. Div is invisible by default
            FileName.InnerHtml = UploadedFileName;
            MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
            ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString();

            FileDetails.Visible = true; //div is made visible
            // Save uploaded file to server at the in the Pics folder
            MyFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "pics\\" + UploadedFileName);

            //thumbnail creation starts
            try
            {
                //Read in the image filename whose thumbnail has to be created
                String imageUrl = UploadedFileName;

                //Read in the width and height
                int imageHeight = Convert.ToInt32(h.Text);
                int imageWidth = Convert.ToInt32(w.Text);

                //You may even specify a standard thumbnail size
                //int imageWidth  = 70;
                //int imageHeight = 70;

                if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0)
                {
                    //We found a / or \
                    Response.End();
                }

                //the uploaded image will be stored in the Pics folder.
                //to get resize the image, the original image has to be accessed from the
                //Pics folder
                imageUrl = "pics/" + imageUrl;

                System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));

                System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

                System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);

                //We need to create a unique filename for each generated image
                DateTime MyDate = DateTime.Now;

                String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png";

                //Save the thumbnail in Png format. You may change it to a diff format with the ImageFormat property
                thumbNailImg.Save(Request.PhysicalApplicationPath + "pics\\" + MyString, ImageFormat.Png);

                thumbNailImg.Dispose();

                //Display the original & the newly generated thumbnail
                Image1.AlternateText = "Original image";
                Image1.ImageUrl = "pics\\" + UploadedFileName;

                Image2.AlternateText = "Thumbnail";
                Image2.ImageUrl = "pics\\" + MyString;
            }

            catch (Exception ex)
            {
                Response.Write("An error occurred - " + ex.ToString());
            }

        }

    }
}