使用js和mvc裁剪图像

时间:2022-04-26 17:27:34

I'm sending collection of images trough webform using MVC3. Controller receiving this posted images and save it's name into db.

我正在使用MVC3通过webform发送图像集。控制器接收此发布的图像并将其名称保存到db。

[HttpPost]
public ActionResult Edit(MyViewModel data, IEnumerable<HttpPostedFileBase> postedImages)
{
   if (ModelState.IsValid)
   {
      using (session...and transaction...)
      {
         MyModel model = session.Get<MyModel>(data.Id);                           
         data.SendToDomainModel(model, session);                     
         foreach (var image in postedImages)
         {
            if ((image != null) && (image.ContentLength > 0))
            {
                Photo photo = new Photo();
                var fileName = Path.GetFileName(image.FileName);
                // path used to save actuall image to the hdd path
                var pathToSave = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);
                // path used to save image path inside db column
                var path = Path.Combine("/Content/uploads/" + fileName);
                photo.MyModel= session.Load<MyModel>(model.Id);
                photo.Path = path;
                photo.Name = fileName;
                image.SaveAs(pathToSave);
                model.Photos.Add(photo);
              }
           }
           // commit transaction ..
           // save session ..
       }
       return RedirectToAction("Index");
    }
    else { return View(data); }
}

How can I use first image from image collection and copy that with filename prefix "firstImage" and crop to 50x50px dimension?

如何使用图像集中的第一张图像并使用文件名前缀“firstImage”复制并裁剪为50x50像素尺寸?

Thanks

谢谢

1 个解决方案

#1


1  

Simple, non-optimized resizing can be acheived using System.Drawing's GetThumbnailImage

可以使用System.Drawing的GetThumbnailImage实现简单,非优化的大小调整

Example:

例:

Image thumb=image.GetThumbnailImage(50, 50, null, IntPtr.Zero);

For a more optimized method that doesn't have the pitfalls listed here see:

对于没有此处列出的陷阱的更优化的方法,请参阅:

This SO answer

这个答案

#1


1  

Simple, non-optimized resizing can be acheived using System.Drawing's GetThumbnailImage

可以使用System.Drawing的GetThumbnailImage实现简单,非优化的大小调整

Example:

例:

Image thumb=image.GetThumbnailImage(50, 50, null, IntPtr.Zero);

For a more optimized method that doesn't have the pitfalls listed here see:

对于没有此处列出的陷阱的更优化的方法,请参阅:

This SO answer

这个答案