1

I am uploading a user image in mvc2 web application. User can upload an image of any size e.g. 10MB or more. The uploaded images are getting stored in database AS IMAGE datatype. But before getting stored in the database I want to reduce its size to 4MB. How do I do this?

I have reduced the size the bytes of the image with the following code:

   if (file.ContentLength > 0)
        {

                        //Create byte Array with file len
                        var imgByte = new Byte[file.ContentLength];
                        //force the control to load data in array
                        file.InputStream.Read(imgByte, 0, file.ContentLength);



    System.IO.MemoryStream newImageStream = 
               new System.IO.MemoryStream(imgByte, 0, imgByte.Length); 

                        Image image = Image.FromStream(newImageStream, true); 

                        Bitmap resized = new Bitmap(image, new Size(800,600));



                System.IO.MemoryStream stream = new System.IO.MemoryStream();

                resized.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

                        var imgBytes = new Byte[stream.Length];



                        customParentalHealthUser.ImageBytes = imgBytes;

                        stream.Close();
                        stream.Dispose();
                        resized.Dispose();
                    }

But while displaying the image, images are not showing up even though the reduced size is getting stored in the DB. I think while reducing the size, image is getting corrupt or else.

Kindly Suggest?

Thanks

2 Answers 2

3

You never write down the stream into the byte array you are creating:

var imgBytes = new Byte[stream.Length];

Then what?

Use ToArray() to get the actual byte array instead:

var imgBytes = stream.ToArray();
Sign up to request clarification or add additional context in comments.

Comments

1

You can either rescale the image or save it as JPEG with a higher compression level.

If the image is larger than the expected displaying size I would start with rescaling the image. Then apply higher compression if necessary.

4 Comments

Can you please provide the code fot that? here is my code: if (Request != null) { HttpPostedFileBase file = Request.Files["uploadimage"]; if (file != null) if (file.ContentLength > 0) { var imgByte = new Byte[file.ContentLength]; file.InputStream.Read(imgByte, 0, file.ContentLength); customParentalHealthUser.ImageBytes = imgByte; } }
@Tarun, there are already about 100 examples of how to resize images in c#.
But i need to reduce the bytes size of the image as I am storing the image in the database as bytes array?
@Tarun, if you resize to a smaller image the file size will also shrink.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.