my .CS file Code
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
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;
}
Bitmap newBmp = new Bitmap(newimagewidth, newimageheight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
using (Graphics gr = Graphics.FromImage(newBmp))
{
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;
}
}
}