Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i working on mvc application, in which i am uploading an image, first croping the image and then upload the image on server. when i crop the image and pass the image in code behind, the image size getting increase in size. example. if i am select the 50KB image and upload the image the it's size getting increase 1MB to more than 1MB.

What I have tried:

1)
public JsonResult UploadEventImage(string uploadedFile)
{
Bitmap bmp= null;

 

                byte[] byteBuffer = Convert.FromBase64String(uploadedFile.Replace("data:image/png;base64,", ""));

                MemoryStream memoryStream = new MemoryStream(byteBuffer);

 

                memoryStream.Position = 0;


                bmp= (Bitmap)Bitmap.FromStream(memoryStream);

                memoryStream.Close();

                memoryStream = null;

                byteBuffer = null;

 

                if (bmp!= null)

                {
                    string fileName = "image1.jpg";

                    fileName = "~/Content/img/" + fileName;
                    bmp.Save(Server.MapPath(fileName), System.Drawing.Imaging.ImageFormat.Bmp);

                    }

}

2)  public JsonResult UploadEventImage(string uploadedFile){ <pre>Bitmap bmp= null;

 

                byte[] byteBuffer = Convert.FromBase64String(uploadedFile.Replace("data:image/png;base64,", ""));

                MemoryStream memoryStream = new MemoryStream(byteBuffer);

 

                memoryStream.Position = 0;

 

                bmp= (Bitmap)Bitmap.FromStream(memoryStream);

 

 

                Image img = bmp;

                Bitmap resizedImg = new Bitmap(img.Width, img.Height);

 

                double ratioX = (double)resizedImg.Width / (double)img.Width;

                double ratioY = (double)resizedImg.Height / (double)img.Height;

                double ratio = ratioX < ratioY ? ratioX : ratioY;

 

                int newHeight = Convert.ToInt32(img.Height * ratio);

                int newWidth = Convert.ToInt32(img.Width * ratio);

 

                using (Graphics g = Graphics.FromImage(resizedImg))

                {

                    g.DrawImage(img, 0, 0, newWidth, newHeight);

                }
 if (bmp!= null)

                {

                    string fileName = "image1.jpg";

                    fileName = "~/Content/img/" + fileName;

                   

                    resizedImg.Save(Server.MapPath(fileName));
}
}
Posted
Updated 28-Nov-18 7:32am

1 solution

The first thing to note is that you are using totally the wrong file format: JPG is a lossy compression, and every time you save a file as JPG you are always going to lose quality. Save too often, and the image will become unrecognisable! But small. Very small.

Consider using BMP, GIF, or PNG - BMP is not compressed, but GIF and PNG use a lossless compression algorithm.
 
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