简体   繁体   中英

C# Upload image and saving as Bitmap Increase size of the image

For uploading image I am using plupload on client side. Then in my controlled I have next logic:

public ActionResult UploadFile()
{
    try
    {
       var file = Request.Files.Count > 0 ? Request.Files[0] : null;
       using (var fileStream = new MemoryStream())
       {
           using (var oldImage = new Bitmap(file.InputStream))
           {
               var format = oldImage.RawFormat;
               using (var newImage = ImageUtility.ResizeImage(oldImage, 800, 2000))
               {
                  newImage.Save(fileStream, format);
               }

              byte[] bits = fileStream.ToArray();
            }
       }
    {
    catch (Exception ex)
    {
    }
}

ImageUtility.ResizeImage Method:

public static class ImageUtility
{
    public static Bitmap ResizeImage(Bitmap image, int width, int height)
    {
        if (image.Width <= width && image.Height <= height)
        {
            return image;
        }

        int newWidth;
        int newHeight;
        if (image.Width > image.Height)
        {
            newWidth = width;
            newHeight = (int)(image.Height * ((float)width / image.Width));
        }
        else
        {
            newHeight = height;
            newWidth = (int)(image.Width * ((float)height / image.Height));
        }

        var newImage = new Bitmap(newWidth, newHeight);
        using (var graphics = Graphics.FromImage(newImage))
        {
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphics.FillRectangle(Brushes.Transparent, 0, 0, newWidth, newHeight);
            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }
    }
}

The issue which i have here that Image size is increased. I uploaded image of 1.62MB and after this controller is called and it creates instance if Bitmap and then save Bitmap to filestream and read bits with "fileStream.ToArray();" I am getting 2.35MB in "bits".

Can anyone tell me what's the reason of increasing the image size after I save it as bitmap. I need Bitmap because I need to check with and height of uploaded image and resize it if I need.

答案很简单,位图占用了更多的内存,无论以前的图像是什么格式,因为它是未压缩的,保存后保持未压缩的格式。

jpeg, png, gif, etc. are compressed and therefore use less bytes tha a bitmap which is uncompressed.

If you just want to save the original image, just save file.InputStream.

If you need to resize, you can use a library to apply jpg/png/etc compression and then save the result.

What is the goal here? Are you merely trying to upload an image? Does it need to be validated as an image? Or are you just trying to upload the file?

If upload is the goal, without any regard to validation, just move the bits and save them with the name of the file. As soon as you do this ...

using (var oldImage = new Bitmap(file.InputStream))

... you are converting to a bitmap. Here is where you are telling the bitmap what format to use (raw).

 var format = oldImage.RawFormat;

If you merely want to move the file (upload), you can run the memory stream to a filestream object and you save the bits.

If you want a few checks on whether the image is empty, etc, you can try this page ( http://www.codeproject.com/Articles/1956/NET-Image-Uploading ), but realize it is still putting it in an image, which is not your desire if you simply want to save "as is".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM