Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create thumbnail of image by just passing height, width, source, destination
Posted

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using System.IO;
using System.Drawing.Imaging;
namespace New_Practice.Admin
{
    public partial class AddContent : System.Web.UI.Page
    {
        public string strimg = "";
        ContentBl bl = new ContentBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    FillData();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                bl.InsertContent(txtTitle.Text, "", txtDescription.Text, chkIsActive.Checked);
                tblContent tbl = bl.SelectMaxId();
                Int32 maxid = tbl.id;
                string img = UploadImage(maxid);
                bl.UpdateContentImage(maxid, img);
                Response.Redirect("Content.aspx?flag=add");
            }
            else
            {
                if (fuImage.HasFile)
                {
                    string strImg = UploadImage(Convert.ToInt32(Request.QueryString["id"]));
                    bl.UpdateContent(Convert.ToInt32(Request.QueryString["id"]), txtTitle.Text, strImg, txtDescription.Text, chkIsActive.Checked);
                }
                else
                {
                    bl.UpdateContent(Convert.ToInt32(Request.QueryString["id"]), txtTitle.Text, hfImage.Value, txtDescription.Text, chkIsActive.Checked);
                }
            }
        }
        public string UploadImage(Int32 maxid)
        {
            string actualFolder = Server.MapPath("../Content/Full/");
            string thumbFolder = Server.MapPath("../Content/Thumb/");
            string smallFolder = Server.MapPath("../Content/Small/");
            DirectoryInfo actDir = new DirectoryInfo(actualFolder);
            DirectoryInfo thumbDir = new DirectoryInfo(thumbFolder);
            DirectoryInfo smallDir = new DirectoryInfo(smallFolder);
            if (!actDir.Exists)
            {
                Directory.CreateDirectory(actualFolder);
            }
            if (!thumbDir.Exists)
            {
                Directory.CreateDirectory(thumbFolder);
            }
            if (!smallDir.Exists)
            {
                Directory.CreateDirectory(smallFolder);
            }
            string imagename = maxid + Path.GetExtension(fuImage.PostedFile.FileName);
            string fullpath = actualFolder + imagename;
            string thumbpath = thumbFolder + imagename;
            string smallpath = smallFolder + imagename;
            if (File.Exists(fullpath))
            {
                File.Delete(fullpath);
            }
            fuImage.SaveAs(fullpath);
            ResizeImageFreeSize(fullpath, thumbpath, 320, 260, Path.GetExtension(fuImage.PostedFile.FileName));
            ResizeImageFreeSize(fullpath, smallpath, 120, 80, Path.GetExtension(fuImage.PostedFile.FileName));
            return imagename;
        }

        public static void ResizeImageFreeSize(string OriginalFile, string NewFile, int MinWidth, int MinHeight, string FileExtension)
        {
            var NewHeight = MinHeight;
            var NewWidth = MinWidth;
            var OriginalImage = System.Drawing.Image.FromFile(OriginalFile);

            if (OriginalImage.Width < MinWidth || OriginalImage.Height < MinHeight)
                throw new Exception(String.Format("Invalid Image Dimensions, please upload an image with minmum dimensions of {0}x{1}px", MinWidth.ToString(), MinHeight.ToString()));

            // If the image dimensions are the same then make the new dimensions the largest of the two mins.
            if (OriginalImage.Height == OriginalImage.Width)
                NewWidth = NewHeight = (MinWidth > MinHeight) ? MinWidth : MinHeight;
            else
            {
                if (MinWidth > MinHeight)
                    NewHeight = (int)(OriginalImage.Height * ((float)MinWidth / (float)OriginalImage.Width));
                else
                    NewWidth = (int)(OriginalImage.Width * ((float)MinHeight / (float)OriginalImage.Height));
            }

            // Just resample the Original Image into a new Bitmap
            var ResizedBitmap = new System.Drawing.Bitmap(OriginalImage, NewWidth, NewHeight);

            // Saves the new bitmap in the same format as it's source image
            FileExtension = FileExtension.ToLower().Replace(".", "");

            ImageFormat Format = null;
            switch (FileExtension)
            {
                case "jpg":
                    Format = ImageFormat.Jpeg;
                    break;
                case "gif":
                    Format = ImageFormat.Gif;
                    break;
                case "png":
                    Format = ImageFormat.Png;
                    break;
                default:
                    Format = ImageFormat.Png;
                    break;
            }

            ResizedBitmap.Save(NewFile, Format);


            // Clear handle to original file so that we can overwrite it if necessary
            OriginalImage.Dispose();
            ResizedBitmap.Dispose();
        }


        public void FillData()
        {

            tblContent tbl = new tblContent();
            tbl = bl.SelectSingle(Convert.ToInt32(Request.QueryString["id"]));
            if (tbl != null)
            {
                txtTitle.Text = tbl.title;
                hfImage.Value = tbl.img;
                txtDescription.Text = tbl.desc;
                strimg = "<a href="../Content/Thumb/" + hfImage.Value + ""  runat="server" rel="facebox">View</a>";
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900