Pages

Friday, 13 April 2012

How to image crop to large size to smaller size with out spoiling the image quality


write class file............
........................................

using System;
using System.Collections.Generic;

using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


/// <summary>
/// Summary description for PhotoUploader
/// </summary>
    public class PhotoUploader
    {
        FileUpload photo;
        string _imageName;
        string _path;
        string _outputMessage;
        int _newWidth;

        //GET AND SET IMAGE NAME
        public string ImageName
        {
            get { return _imageName; }
            set { _imageName = value; }
        }

        //GET AND SET IMAGE PATH
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        //GET AND SET NEW WIDTH
        public int NewWidth
        {
            get { return _newWidth; }
            set { _newWidth = value; }
        }

        //GET AND SET ERROR MESSAGE
        public string OutputMessage
        {
            get { return _outputMessage; }
            set { _outputMessage = value; }
        }

        public bool StartUploadProcess(FileUpload ProductPhoto, int LargestWidthPX, string MapPath)
        {
            photo = ProductPhoto;
            ImageName = photo.FileName.Replace(" ", "").Replace("%20", "");

            //--- make sure image is in acceptable type
            //if (ImageName.ToLower().EndsWith(".jpg") || ImageName.ToLower().EndsWith(".jpeg") || ImageName.ToLower().EndsWith(".png") || ImageName.ToLower().EndsWith(".gif"))
            //{
            //    if ((ImageName != null))
            //    {
                    if (UploadPhoto(LargestWidthPX, MapPath))
                    {
                        //OutputMessage = "Photo was successfully uploaded.";
                        return true;
                    }
                //}
                //else
                //{
                //    OutputMessage = "No photo supplied. Please selecte a photo.";
                //    return false;
                //}
            //}
            //else
            //{
            //    OutputMessage = "The photo is not a supported type. Photo must be .jpg, .png or .gif";
            //    return false;
            //}
            return false;
        }

        protected bool UploadPhoto(int LargestWidthPX, string MapPath)
        {
            Path = MapPath;
            photo.SaveAs(HttpContext.Current.Server.MapPath(Path + ImageName.Replace(" ", "").Replace("%20", "")));
            NewWidth = LargestWidthPX;

            if (resizeImage())
            {
                NewWidth = 257;
                //if (resizeImage())
                //{
                //    NewWidth = 100;
                //    if (resizeImage())
                //    {
                //        File.Delete(HttpContext.Current.Server.MapPath(Path + photo.FileName.Replace(" ", "").Replace("%20", "")));
                //        return true;
                //    }
                //}
                File.Delete(HttpContext.Current.Server.MapPath(Path + photo.FileName.Replace(" ", "").Replace("%20", "")));
                  return true;
            }
            return false;
        }
        protected bool resizeImage()
        {
            string originalFileName = null;
            string newFileName = null;
            Bitmap tmpImage = default(Bitmap);
            Bitmap newImage = default(Bitmap);
            Graphics g = default(Graphics);
            int newHeight = 0;
            FileStream fs = default(FileStream);
            originalFileName = HttpContext.Current.Server.MapPath(Path + ImageName.Replace(" ", "").Replace("%20", ""));
            if (File.Exists(originalFileName))
            {
                try
                {
                    fs = new FileStream(originalFileName, FileMode.Open);
                    tmpImage = (Bitmap)Bitmap.FromStream(fs);
                    fs.Close();

                    //newHeight = (NewWidth * tmpImage.Height) / tmpImage.Width;
                   // newImage = new Bitmap(NewWidth, newHeight);

                    newHeight = 224;
                    newImage = new Bitmap(257, 224);

                    g = Graphics.FromImage(newImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                     g.DrawImage(tmpImage, 0, 0, NewWidth, newHeight);
                    g.DrawImage(tmpImage, 0, 0, 257, 224);
                    g.Dispose();

                    newFileName = NewWidth.ToString() + "_" + ImageName.Replace(" ", "").Replace("%20", "");
                    newImage.Save(HttpContext.Current.Server.MapPath(Path + newFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
                    newImage.Dispose();
                    tmpImage.Dispose();

                    tmpImage = null;
                    newImage = null;
                    g = null;
                    return true;
                }
                catch (Exception ex)
                {
                    OutputMessage = "There was an error processing the request and we have been notified. Please try again later.";
                    tmpImage = null;
                    newImage = null;
                    g = null;
                    return false;
                }
            }
            else
            {
                OutputMessage = "There was an error processing the request and we have been notified. Please try again later.";
                tmpImage = null;
                newImage = null;
                g = null;
                return false;
            }
        }

    }






in C# code


 if (uploader.StartUploadProcess(FileUpload1, 600, "~/CroppedImages/"))
                {
                    x = FileUpload1.PostedFile.FileName;

                }


it will crop and save into that folder


No comments:

Post a Comment