上传后和将其保存到数据库之前调整图像大小

Resize an image after upload and before saving it into the db

我有一个简单的 Web 应用程序 (ASP.NET MVC 5 C#),它允许用户上传多个文件(实际上是图像)。

目前效果不错,图片都存入数据库了,以后可以看。

但我想在将图像保存到数据库之前调整图像的大小,因为用户可以上传非常大的图像。

这是我的控制器:

public ActionResult Create(Annonce annonce, IEnumerable<HttpPostedFileBase> photos)
    {
        if (ModelState.IsValid)
        {
            // Read each uploaded files and add if into the collection
            foreach (HttpPostedFileBase fichier in photos)
            {                                                                                   
                if (fichier != null && fichier.ContentLength > 0)
                {
                    // Making a new object
                    var photo = new Photo
                    {
                        FileName = System.IO.Path.GetFileName(fichier.FileName),
                        ContentType = fichier.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(fichier.InputStream))
                    {
                        photo.Content = reader.ReadBytes(fichier.ContentLength);
                    }
                    // Add the current image to the collection
                    annonce.Photos.Add(photo);
                }
            }

            db.Annonces.Add(annonce);
            db.SaveChanges();
            return RedirectToAction("Details", new { id = annonce.ID });
        }

        return View(annonce);
    }

如何调整图像大小并仍然能够将它们保存到数据库中? 甚至可能吗?

谢谢!

此代码将执行高质量的大小调整。(意味着您不会损失太多)

public static Bitmap ResizeImage(Image image, int width, int height)
 {
   var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);

destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (var graphics = Graphics.FromImage(destImage))
{
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

    using (var wrapMode = new ImageAttributes())
    {
        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
        graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
    }
}

return destImage;  
}

调用 ResizeImage() 并将其分配给一个位图,您将把它插入到 database.goodluck

您可以将其转换为字节数组,然后将其作为字节类型存储在您的数据库中

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return  ms.ToArray();
 }

你可以做同样的事情,但将其倒置以将其显示为来自数据库的图像:

public Image byteArrayToImage(byte[] byteArrayIn)
   {
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
    }

您还可以按照下面的 link 方法使用 ImageResizer: http://www.c-sharpcorner.com/article/image-resize-in-asp-net-mvc-using-image-resizer/