Exemplo n.º 1
0
        public ActionResult Crop(Guid id)
        {
            if (!db.Resources.Any(x => x.ID == id)) throw new HttpException(404, "Resource not found.");
            Resource res = db.Resources.Single(x => x.ID == id);
            if (!res.Type.StartsWith("image")) return Content("You cannot crop a non-image resource!");

            string path = Path.Combine(Server.MapPath("~/ResourceUploads"), id.ToString());
            FileStream stream = new FileStream(path, FileMode.Open);
            if (stream.Length == 0) throw new HttpException(503, "An internal server error occured whilst fetching the resource.");

            byte[] streamBytes = new byte[stream.Length];
            stream.Read(streamBytes, 0, (int)stream.Length);
            stream.Close();

            WebImage img = new WebImage(streamBytes);
            CropForm form = new CropForm
            {
                ResourceID = id,
                Type = res.Type,
                Source = res.Source,
                SourceTextColorID = res.SourceTextColorID,
                OrigWidth = img.Width,
                OrigHeight = img.Height
            };
            return View(form);
        }
Exemplo n.º 2
0
        public ActionResult Crop(CropForm form)
        {
            if (form.x <= 0 && form.y <= 0 && form.x2 <= 0 && form.y2 <= 0)
            {
                ModelState.AddModelError("", "You must provide a selection!");
                return View(form);
            }

            if (db.Resources.Any(x => x.Title == form.Title))
            {
                ModelState.AddModelError("Title", "Another Resource has this Title.");
                return View(form);
            }

            string oldImagePath = Path.Combine(Server.MapPath("~/ResourceUploads"), form.ResourceID.ToString());
            FileStream stream = new FileStream(oldImagePath, FileMode.Open);

            WebImage image = new WebImage(stream);

            int width = image.Width;
            int height = image.Height;

            image.Crop((int)Math.Ceiling(form.y), (int)Math.Ceiling(form.x), height - (int)Math.Ceiling(form.y2), width - (int)Math.Ceiling(form.x2));
            //image.Crop((int)form.y, (int)form.x, (int)form.y2, (int)form.x2);

            Resource newResource = new Resource
            {
                ID = Guid.NewGuid(),
                Title = form.Title,
                CreatorID = SiteAuthentication.GetUserCookie().ID,
                DateAdded = DateTime.Now,
                Type = form.Type,
                Source = form.Source,
                SourceTextColorID = form.SourceTextColorID
            };

            string newImagePath = Path.Combine(Server.MapPath("~/ResourceUploads"), newResource.ID.ToString());

            db.Resources.Add(newResource);

            image.Save(newImagePath, null, false);

            db.SaveChanges();

            return View("_CloseAndRefreshParent");
        }