Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a webform contains a file upload control and a button, when uploading the image I want to resize it and decrease its physical size (in KB).

I use a method that Do the resizing but the image size still big, i tried to decrease the resolution using
C#
Bitmap.SetResolution( , )
but it didn't affect the size of the image in KB

Here's my aspx code:
ASP.NET
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>


and Here's the code behind:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        int Image_Width = 830;
        int Image_Height = 430;
        string ImageName = "f_" + DateTime.Now.Millisecond + DateTime.Now.DayOfYear + DateTime.Now.Minute + DateTime.Now.Hour;
        HttpPostedFile pf = FileUpload1.PostedFile;
        ImageName += Path.GetExtension(pf.FileName);
        System.Drawing.Image bm_Large = System.Drawing.Image.FromStream(pf.InputStream);
        bm_Large = ResizeBitmap((Bitmap)bm_Large, Image_Width, Image_Height);
        string pathToSave_Large = Server.MapPath("~/images/Test Ajax/" + ImageName);
        bm_Large.Save(pathToSave_Large);
        FileUpload1.PostedFile.InputStream.Close();
    }
}

public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
    Bitmap result = new Bitmap(nWidth, nHeight);
    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
        g.DrawImage(b, 0, 0, nWidth, nHeight);
    //result.SetResolution(30, 30);
    return result;
}
Posted

Tis site's seach would surely have returned a reference here[^].
But anyway: have a look at Bitmap's Save overloads[^]. You can compress a bitmap, if size matters (and you're going to lose image quality, though).
 
Share this answer
 
Refer - how to resize image without losing image quality or how to reduce image file size without losing quality using asp.net[^].
Quote:
After run my application I upload above 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900