Wednesday, 27 August 2014

uploading and generating Thumbnails asp.net webforms

HTML


<form id="form1" runat="server">
    <h3>Upload image with Thumbnail</h3>
    <hr />
    <div>
        Select Image :
        <asp:FileUpload ID="FileUpload1" runat="server" />
        &nbsp;
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
    </div>
    <div>
        <br />
        <br />
        <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal" BackColor="White" BorderColor="#CC9966"
            BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <ItemStyle BackColor="White" ForeColor="#330099" />
            <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <ItemTemplate>
                <div style="text-align:center">
                    <div><a target="_blank" href='/Images/<%#Eval("FileName")%>'><img src="/Thumbnail/<%#Eval("FileName")%>" /></a></div>
                    <div ><%#Eval("FileSize")%> KB</div>
                    <div><a target="_blank" href='/Images/<%#Eval("FileName")%>'>View Full Size</a> </div>
                </div>
            </ItemTemplate>
        </asp:DataList>
    </div>

    </div>
    </form>

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

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string uploadedFileName = FileUpload1.PostedFile.FileName;
            Image img = Image.FromStream(FileUpload1.PostedFile.InputStream);
            string ext = Path.GetExtension(uploadedFileName);

            string uploadFilePath = DateTime.Now.ToString("ddMMyyyyhhmmsstt");
            string imageFile = uploadFilePath + ext;

            try
            {
                FileUpload1.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/Images"), imageFile));
                var ratio = (double)100 / img.Height;
                int imageHeight = (int)(img.Height * ratio);
                int imageWidth = (int)(img.Width * ratio);

                Image.GetThumbnailImageAbort dCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                Image thumbnailImg = img.GetThumbnailImage(imageWidth, imageHeight, dCallback, IntPtr.Zero);

                thumbnailImg.Save(Path.Combine(Server.MapPath("~/Thumbnail"), imageFile), ImageFormat.Jpeg);
                thumbnailImg.Dispose();

                //Here Code for Get Uploaded Images
                PopulateImage();
            }
            catch (Exception ex)
            {

                throw;
            }


        }
        public bool ThumbnailCallback()
        {
            return true;
        }
        private void PopulateImage()
        {
            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Images"));
            List<MyImageFiles> images = new List<MyImageFiles>();
            foreach (var i in dir.GetFiles())
            {
                images.Add(new MyImageFiles { FileName = i.Name, FileSize = (i.Length / 1024).ToString() });
            }
            DataList1.DataSource = images;
            DataList1.DataBind();
        }
    }
********************class****************
public class MyImageFiles
    {
        public string FileName { get; set; }
        public string FileSize { get; set; }

    }