Monday, 7 April 2014

Image Crop

  bool CompressImage(string filePath, AjaxControlToolkit.AsyncFileUpload FileUploadImage, Double bmpW, Double bmpH)
    {
        //const Double bmpW = 150;
        //const Double bmpH = 140;
        if (FileUploadImage.HasFile)
        {
            int newWidth = Convert.ToInt32(bmpW);
            int newHeight = Convert.ToInt32(bmpH);
            string temp = System.IO.Path.GetTempPath();
            string strFileExt = FileUploadImage.FileName.Split('.')[FileUploadImage.FileName.Split('.').Length - 1];
            Bitmap upBmp = new Bitmap(Bitmap.FromStream(FileUploadImage.PostedFile.InputStream));
            //Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            //newBmp.SetResolution(70, 70);
            double upWidth = upBmp.Width;
            double upHeight = upBmp.Height;
            float newX = 0, newY = 0;
            double reDuce = default(double);
            if (upWidth > upHeight)
            {
                reDuce = (newWidth / upWidth);
                newHeight = Convert.ToInt16(upHeight * reDuce);
                newY = Convert.ToInt16((bmpH - newHeight) / 2);
                newX = 0;
            }
            else if (upWidth < upHeight)
            {
                reDuce = Convert.ToDouble(newHeight / upHeight);
                newWidth = Convert.ToInt16(upWidth * reDuce);
                newX = Convert.ToInt16((bmpW - newWidth) / 2);
                newY = 0;
            }
            else if (upWidth == upHeight)
            {
                reDuce = Convert.ToDouble(newHeight / upHeight);
                newWidth = Convert.ToInt16(upWidth * reDuce);
                newX = Convert.ToInt16((bmpW - newWidth) / 2);
                newY = Convert.ToInt16((bmpH - newHeight) / 2);
            }
            Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            newBmp.SetResolution(70, 70);

            Graphics newGraphic = Graphics.FromImage(newBmp);
            try
            {
                newGraphic.Clear(Color.White);
                newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newGraphic.DrawImage(upBmp, 0, 0, newWidth, newHeight);
                newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Png);
            }
            catch (Exception ex)
            {
                string strMsg = ex.Message;
                return false;
            }
            finally
            {
                upBmp.Dispose();
                newBmp.Dispose();
                newGraphic.Dispose();
            }
        }
        return true;
    }

No comments:

Post a Comment

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]...