Solved

Image Resize

Posted on 2009-07-13
3
925 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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