Solved

Image Resize

Posted on 2009-07-13
3
922 Views
Last Modified: 2012-06-27
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() + "\" ";

            }

            else

            {

                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) {

            LoadImage();

        }
 

</script>
 

<!-- Code by Emanuele Briano --- http://www.emanuelebriano.it -->
 

<html>

<head>

</head>

<body>

    <form runat="server">

        <div id="divImage" runat="server">Image will be loaded here

        </div>

        <div id="divDim" runat="server">

        </div>

        <br />

        <br />

        <br />

        <br />

        <div>maxWidth:

            <input id="txtWidth" type="text" value="300" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;

            maxHeight:

            <input id="txtHeight" type="text" value="100" runat="server" />

        </div>

        <div>

            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Load"></asp:Button>

        </div>

        <div>&nbsp;

        </div>

        <div>Code by <a href="http://www.emanuelebriano.it>Emanuele Briano</a>

        </div>

    </form>

</body>

</html>

Open in new window

0
Comment
Question by:virafh
3 Comments
 
LVL 6

Expert Comment

by:badalpatel
Comment Utility
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));
 

theWriter.Close();
 

theFile.Close();
 

}

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;
 

}
 

else
 

{
 

newSize.Width = targetSize;

newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
 

}

return newSize;

Open in new window

0
 

Accepted Solution

by:
virafh earned 0 total points
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:DreamsTech
Comment Utility
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.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now