Image Resize

Dear Team,

I want to resize image also when i resize image the image weight(size) also reduce

means if a image size is 500/500 (250kb) when i resize the same image dynamically to 300/300 ( 150kb) its size also reduce.

one more thing if i want the image width fixed 400 and length will set according the width.

below i pasted codes which i got from some article, i like this, but its only show how to resize the image but with ratio, my requirement is with fixed width also i want to reduce the size of image dynamically.

so what i want

1. resize image
2. reduce size
3. resize image with fixed width

hope you understand my requirement.

<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
    // This function return the tag string to resize an image at the desired MaxWidth and MaxHeight, maintaining the image Height/Width Ratio
    string COMMONFUNCTIONS_IMAGES_RESIZE_TO_TAG(System.Drawing.Image img, int MaxWidth, int MaxHeight)
            if (img.Width > MaxWidth || img.Height > MaxHeight)
                double widthRatio = (double) img.Width / (double) MaxWidth;
                double heightRatio = (double) img.Height / (double) MaxHeight;
                double ratio = Math.Max(widthRatio, heightRatio);
                int newWidth = (int) (img.Width / ratio);
                int newHeight = (int) (img.Height / ratio);
                return " width=\"" + newWidth.ToString() + "\"" + " height=\"" + newHeight.ToString() + "\" ";
                return "";
        private void LoadImage()
            // sets the name of the image
            string imageUrl = "emanuelebriano.gif";
            // gets width and height
            int Img_Width = Int32.Parse(txtWidth.Value);
            int Img_Height = Int32.Parse(txtHeight.Value);
            // gets the image width and height tag
            string s_ImageSize = " width=\"0\" height=\"0\" ";
            if (File.Exists(Server.MapPath(imageUrl)))
               System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
               s_ImageSize = COMMONFUNCTIONS_IMAGES_RESIZE_TO_TAG(img, Img_Width, Img_Height);
            // draw the image
            divImage.InnerHtml = "<img src=\"" + imageUrl + "\" border='1' " + s_ImageSize  + " />";
            divDim.InnerHtml = s_ImageSize;
        void Button1_Click(object sender, EventArgs e) {
<!-- Code by Emanuele Briano --- -->
    <form runat="server">
        <div id="divImage" runat="server">Image will be loaded here
        <div id="divDim" runat="server">
        <br />
        <br />
        <br />
        <br />
            <input id="txtWidth" type="text" value="300" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;
            <input id="txtHeight" type="text" value="100" runat="server" />
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Load"></asp:Button>
        <div>Code by <a href=">Emanuele Briano</a>

Open in new window

Who is Participating?
virafhConnect With a Mentor Author Commented:
buddy i don't want to save the image.

i read the big image from the server and for thumbnail i want to resize and also reduce image size.
You can use something like below code. Don't forget include using System.Drawing, using System.Drawing.Drawing2D, using System.Drawing.Imaging. Good example is in PersonalWebSite Starter Kit.

protected void Image_Save(object sender, EventArgs e){ 
FileStream theFile = File.Create(Server.MapPath("Images/image-001.jpg"));
BinaryWriter theWriter = new BinaryWriter(theFile);
theWriter.Write(ResizeImageFile(FileUploadControlName.FileBytes, 600));
private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
using (Graphics canvas = Graphics.FromImage(newImage))
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Jpeg);
return m.GetBuffer();
private static Size CalculateDimensions(Size oldSize, int targetSize)
Size newSize = new Size();if (oldSize.Height > oldSize.Width)
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
return newSize;

Open in new window

Couple of fundamental corrections :
Image Size is always liked to the resolution ,if you reduce the resolution (Pixels)
then accordingly the Size decrease ,and vice versa.

Also ,length is always determined by the width of the Pic as determined by Windows,
you cannot have a arbitrary height for a fixed width.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.