Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
1.57/5 (3 votes)
See more:
my .CS file Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Services.MyClassFiles;
using System.Drawing;

using System.Web.UI.WebControls;

namespace MyClasses
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public System.Drawing.Image GetImage1(string Path, int Height, int width, Double upWidth, Double upHeight)
        {

            ImageResize1 dataservice = new ImageResize1();
            System.Drawing.Image images = dataservice.ScaleImage(System.Drawing.Image.FromFile(Path), Height, width, upWidth, upHeight);
           
            return images;
            Bitmap newBmp = new Bitmap(images.Width, images.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        protected void btn_save_Click(object sender, System.EventArgs e)
        {
            ImageResize1 dataservice = new ImageResize1();

            if (fileupload1.HasFile)
            {

                string fname = ""; string fpath = "";

                fname = fileupload1.FileName;
                fpath = Server.MapPath("~/image/") + fname;


                fileupload1.SaveAs(fpath);

                Bitmap upBmp = (Bitmap)Bitmap.FromStream(fileupload1.PostedFile.InputStream);
                Double upWidth = upBmp.Width;
                Double upHeight = upBmp.Height;
                System.Drawing.Image images = GetImage1(fpath, 500, 500, upWidth, upHeight);
                System.Drawing.Image images1 = GetImage1(fpath, 50, 50, upWidth, upHeight);
                System.Drawing.Image images2 = GetImage1(fpath, 200, 500, upWidth, upHeight);

                String upName = fname.Substring(0, fname.IndexOf("."));
                string repath = "~/thumb/" + upName + ".jpg";
                string repath1 = "~/small/" + upName + ".jpg";
                string repath2 = "~/d/" + upName + ".jpg";

                images.Save(MapPath(repath), System.Drawing.Imaging.ImageFormat.Jpeg);
                images1.Save(MapPath(repath1), System.Drawing.Imaging.ImageFormat.Jpeg);
                images2.Save(MapPath(repath2), System.Drawing.Imaging.ImageFormat.Jpeg);
                img.ImageUrl = repath2;
            }

        }

    }
}

My Class file code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Services.MyClassFiles
{
    public class ImageResize1
    {

        public System.Drawing.Image ScaleImage(System.Drawing.Image srcImage, int newWidth, int newHeight, Double upWidth,Double upHeight )
        {

            int newimagewidth = newWidth;
            int newimageheight = newHeight;
            int x = 0;
            int y = 0;
            Double reduce;
           

            if (upWidth > upHeight)
            {
                reduce = newimagewidth / upWidth;
                newimageheight = ((Int32)(upHeight * reduce));
                y = ((Int32)((newHeight - newimageheight) / 2));
                x = 0;
            
            }
            if (upWidth < upHeight)
            {

                reduce = newimageheight / upHeight;
                newimagewidth = ((Int32)(upWidth * reduce));

                x = ((Int32)((newWidth - newimagewidth) / 2));
                y = 0;
            
            }
            //if (Double upHeight = Double upWidth)
            //{
            //    reduce = newimageheight/ upHeight;
            //    newimagewidth = ((Int32)(upWidth * reduce));
            //    x = ((Int32)((newWidth - newimagewidth) / 2));
            //    x = ((Int32)((newHeight - newimageheight) / 2));
            //}

            Bitmap newBmp = new Bitmap(newimagewidth, newimageheight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            newBmp.SetResolution(72, 72);
            //System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
            using (Graphics gr = Graphics.FromImage(newBmp))
            {

                //gr.Clear(Color.White);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gr.DrawImage(srcImage, new Rectangle(x, y, newimagewidth, newimageheight));
            }

            return newBmp;
        }

      
    }
}
Posted
Updated 22-Jan-14 0:58am
v2

1 solution

In Class File code


C#
gr.DrawImage(srcImage, new Rectangle(x, y, newimagewidth, newimageheight));



Replace with

C#
gr.DrawImage(srcImage, new Rectangle(0, 0, newimagewidth, newimageheight));
 
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