Friday 21 February 2014

Resize image and show




<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
        <asp:ImageButton ID="imgBtn" runat="server" ToolTip="To download right click 'Save Image As'" />
    </div>
</form>





using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Collections.Specialized;
using System.Drawing.Imaging;



public partial class ProgramFiles_OnlineRegistrationForm_ImageResizer : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          
            string FilePath = System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString();
            Session["PhotoGalleryPath"] = FilePath;
        }
    }
   
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string FileName1 = "";
        if (FileUpload1.HasFile == true)
        {
            FileName1 = FileUpload1.FileName.ToUpper(); ;
            string Extension = Path.GetExtension(FileName1);

            if (Extension == ".JPG" || Extension == ".JPEG" || Extension == ".PNG" || Extension == ".GIF" || Extension == ".BMP")
            {
                if (File.Exists(Session["PhotoGalleryPath"].ToString()))
                {
                    File.Delete(Session["PhotoGalleryPath"].ToString());
                }
                else
                {
                    FileUpload1.SaveAs(Server.MapPath((System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString())) + FileName1);


                    string path = Server.MapPath((System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString())) + FileName1;
                   
                    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                    using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 120, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            thumbnail.Save(memoryStream, ImageFormat.Png);
                            Byte[] bytes = new Byte[memoryStream.Length];
                            memoryStream.Position = 0;
                            memoryStream.Read(bytes, 0, (int)bytes.Length);
                            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                            imgBtn.ImageUrl = "data:image/png;base64," + base64String;
                            imgBtn.Visible = true;
                        }
                    }
                }
            }
        }
    }

    public bool ThumbnailCallback()
    {
        return false;
    }
}



Note :

    1. Add <add key="UpcomingEventPath" value="\DemoSite\UserSpace\UpcomingEvents\"/> in <appSettings> @ web.Congic
    2. Create two folder inside your project (DemoSite) USerSpace & UpcomingEvents.
    3. You can save this resized image by right click and save as on that thumbnal.



Cheers!!!

No comments:

Post a Comment