user1765862
user1765862

Reputation: 14205

cropping image using js and mvc

I'm sending collection of images trough webform using MVC3. Controller receiving this posted images and save it's name into 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?

Thanks

Upvotes: 0

Views: 3639

Answers (1)

Kaizen Programmer
Kaizen Programmer

Reputation: 3818

Simple, non-optimized resizing can be acheived using System.Drawing's 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

Upvotes: 1

Related Questions