Solved

Image Resize

Posted on 2009-07-13
3
923 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
ID: 24838308
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
ID: 24838344
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
ID: 24838487
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

911 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

18 Experts available now in Live!

Get 1:1 Help Now