C#上传图像并保存为图像的位图大小增加 [英] C# Upload image and saving as Bitmap Increase size of the image

查看:102
本文介绍了C#上传图像并保存为图像的位图大小增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关上传图像我使用的客户端plupload。然后在我的控制,我旁边的逻辑:

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方式:

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;
        }
    }
}

这是我在这里,图像尺寸增大的问题。
我上传的图片1.62MB的这个控制器后调用并创建实例,如果位图,然后保存为位图和FILESTREAM读取位fileStream.ToArray();我在位。

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.

推荐答案

的目标是什么吗?你只是想上传图片?是否需要进行验证作为图像?或者,你只是想上传的文件?

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))

...要转换为位图。在这里你说的是位图使用什么格式(RAW)。

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

 var format = oldImage.RawFormat;

如果您只是想移动的文件(上传),您可以运行内存流到FILESTREAM对象,并保存位。

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

如果你想上的图像是否为空,等了几个检查,你可以试试这个页面(的 HTTP://www.$c$cproject.com/Articles/1956/NET-Image-Uploading ),但实现它仍然是把它的形象,这是不你的愿望,如果你只是想保存原样。

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".

这篇关于C#上传图像并保存为图像的位图大小增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆