Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my client wants the image should be (380*380) resolution i have done this through the below given code...Now size should be 50-150 kb... how i can compress the image b/w 50-150kb

C#
  private Bitmap Resize_Image(Stream streamImage, int maxWidth, int maxHeight)
{

    Bitmap originalImage = new Bitmap(streamImage);

    int newWidth = originalImage.Width;
    int newHeight = originalImage.Height;

    if (originalImage.Width > maxWidth)
    {
        newWidth = maxWidth;
    }
    if (originalImage.Height > maxHeight)
    {
        newHeight = maxHeight;
    }
    return new Bitmap(originalImage, newWidth, newHeight);
}


protected void btnUpload_Click(object sender, EventArgs e)
{

    if (imgUpload.HasFile)
    {
        //string img = string.Empty;
        Bitmap bmpImg = null;
        try
        {

            //int fileSize = imgUpload.PostedFile.ContentLength;
           // int maxFileSize = 150;
           // if (maxFileSize > fileSize)
           // {
           //     maxFileSize = fileSize;
           // }
           // else
           // {
            //    maxFileSize = fileSize;
           // }

            bmpImg = Resize_Image(imgUpload.PostedFile.InputStream, 380, 380);
            string fileName = imgUpload.PostedFile.FileName;
            string saveLocation = Server.MapPath("UploadImages/" + fileName);
            bmpImg.Save(saveLocation);
  
        }
        catch (Exception ex)
        {
            Response.Write("Error occured: " + ex.Message.ToString());
        }
        finally
        {
            img = string.Empty;
            bmpImg.Dispose();

        }
    }
}
Posted
Updated 14-May-14 21:12pm
v2

1 solution

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