How to Reduce Image FileSize without loosing Image quality in


Here I will explain how to resize an image without losing any quality from uploaded images using
Here I am using same concept to resize the image without losing quality just by changing scaling factor in previous post. In social networking sites after upload images they will reduce the size of the photo but the quality will be same after seen that I tried to write this post. I taken one of my pic that pic size is 3.62 MB before resize Image.//Image here.
Now I am trying to resize the image. For that first create one new website after that right click on that website select New Folder give name as Images because here I am using same name if you want to use another name you need to change the name in code also. After completion of adding Images folder to your website Design your aspx page like this :-

<html xmlns=””&gt;

<head id=”Head1″ runat=”server”>
<title>Generate a Thumbnails from Uploaded Image</title>
<form id=”form1″ runat=”server”>
<asp:FileUpload ID=”fileupload1″ runat=”server” />
<asp:Button ID=”btnsave” runat=”server” Text=”Upload” onclick=”btnsave_Click” />
<asp:DataList ID=”dtlist” runat=”server” RepeatColumns=”3″ CellPadding=”5″>
<asp:Image ID=”Image1″ ImageUrl='<%# Bind(“Name”, “~/Images/{0}”) %>’ runat=”server” />
<br />
<asp:HyperLink ID=”HyperLink1″ Text='<%# Bind(“Name”) %>’ NavigateUrl='<%# Bind(“Name”, “~/Images/{0}”) %>’ runat=”server”/>
<ItemStyle BorderColor=”Brown” BorderStyle=”dotted” BorderWidth=”3px” HorizontalAlign=”Center”
VerticalAlign=”Bottom” />

After that add following namespaces in code behind
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
After completion of adding namespaces write the following code
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
protected void BindDataList()
DirectoryInfo dir = new DirectoryInfo(MapPath(“Images”));
FileInfo[] files = dir.GetFiles();
ArrayList listItems = new ArrayList();
foreach (FileInfo info in files)
dtlist.DataSource = listItems;
protected void btnsave_Click(object sender, EventArgs e)
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string targetPath = Server.MapPath(“Images/” + filename);
Stream strm = fileupload1.PostedFile.InputStream;
var targetFile = targetPath;
//Based on scalefactor image size will vary
GenerateThumbnails(0.5, strm, targetFile);
private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
using (var image = Image.FromStream(sourcePath))
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
After completion of above run your application and upload one image and check the size of image after resize and before resize you will find huge difference in size but quality will be same as original one.
After run my application I upload pic to resize after completion process my pic size reduces drastically to 218 KB do you believe this but it’s true we can reduce image size as much as possible by using above code without losing quality of image and my image quality same as original one check it.
 //Image here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s