Example #1
0
        public static string SaveAndCropColunista(HttpPostedFileBase image, string path, int cropX = 0, int cropY = 0, int cropWidth = 0, int cropHeight = 0, bool keepFileName = false, string fileNameKeep = "")
        {
            var fileName = keepFileName ? (!string.IsNullOrEmpty(fileNameKeep) ? fileNameKeep : GetFileName(image.FileName, path)) : GetFileName(image.FileName, path);
            var mimeType = MimeMapping.GetMimeMapping(fileName);

            using (var origem = Image.FromStream(image.InputStream, true, true))
            {
                //var media = Imager.Resize(origem, 620, 200, true);
                Image media = null;

                int startX = cropX;
                int startY = cropY;

                media = Imager.Crop(origem, new Rectangle(startX, startY, cropWidth, cropHeight));

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var imageCodecInfo = Imager.GetEncoderInfo(mimeType);

                // Imager.SaveJpeg(path + fileName, media != null ? media : origem);
                Imager.Save(path + fileName, media != null ? media : origem, imageCodecInfo);
            }

            return(fileName);
        }
Example #2
0
        public static Image CropJpg(string filename, string newfilename, int x, int y, int width, int height)
        {
            using (var image = Image.FromFile(filename))
            {
                var img = Imager.Crop(image, new Rectangle(x, y, width, height));

                Imager.SaveJpeg(newfilename, img);
                return(img);
            }
        }
Example #3
0
        public static Image CropPng(string filename, string newfilename, int x, int y, int width, int height)
        {
            using (var image = Image.FromFile(filename))
            {
                var img = Imager.Crop(image, new Rectangle(x, y, width, height));

                img.Save(newfilename, System.Drawing.Imaging.ImageFormat.Png);
                return(img);
            }
        }
Example #4
0
 public void MakeImages(string filename, int x, int y, int w, int h)
 {
     using (var image = Image.FromFile(tempPath + filename))
     {
         var img     = Imager.Crop(image, new Rectangle(x, y, w, h));
         var resized = Imager.Resize(img, 200, 150, true);
         var small   = Imager.Resize(img, 100, 75, true);
         var mini    = Imager.Resize(img, 45, 34, true);
         Imager.SaveJpeg(mealsPath + filename, resized);
         Imager.SaveJpeg(mealsPath + "s" + filename, small);
         Imager.SaveJpeg(mealsPath + "m" + filename, mini);
     }
 }
 public ActionResult Crop(int x, int y, int w, int h, int id)
 {
     using (var image = Image.FromFile(@ConfigurationManager.AppSettings["storagePath"] + @"\Meals\Temp\" + id + ".jpg"))
     {
         var img     = Imager.Crop(image, new Rectangle(x, y, w, h));
         var resized = Imager.Resize(img, 200, 150, true);
         var small   = Imager.Resize(img, 100, 75, true);
         var mini    = Imager.Resize(img, 45, 34, true);
         Imager.SaveJpeg(@ConfigurationManager.AppSettings["storagePath"] + @"\Meals\" + id + ".jpg", resized);
         Imager.SaveJpeg(@ConfigurationManager.AppSettings["storagePath"] + @"\Meals\" + id + "s.jpg", small);
         Imager.SaveJpeg(@ConfigurationManager.AppSettings["storagePath"] + @"\Meals\" + id + "m.jpg", mini);
         s.HasPic(id);
     }
     return(RedirectToAction("Picture", new { id }));
 }
Example #6
0
        public static string SaveAndCropImage(HttpPostedFileBase image, string path, int cropX = 0, int cropY = 0, int cropWidth = 0, int cropHeight = 0, bool keepFileName = false, string fileNameKeep = "")
        {
            var fileName = keepFileName ? (!string.IsNullOrEmpty(fileNameKeep) ? fileNameKeep : GetFileName(image.FileName, path)) : GetFileName(image.FileName, path);
            var mimeType = MimeMapping.GetMimeMapping(fileName);

            using (var origem = Image.FromStream(image.InputStream, true, true))
            {
                //var media = Imager.Resize(origem, 620, 200, true);
                Image media = null;


                double croppedHeightToWidth = (double)cropHeight / cropWidth;
                double croppedWidthToHeight = (double)cropWidth / cropHeight;
                int    startX = cropX;
                int    startY = cropY;


                #region
                if (origem.Width > cropWidth && origem.Height > cropHeight)
                {
                    if (cropWidth > 0 && cropHeight > 0)
                    {
                        //try to get the center of image
                        #region GetCenter
                        if (origem.Width > origem.Height)
                        {
                            cropWidth = (int)(Math.Round(origem.Height * croppedWidthToHeight));

                            if (cropWidth < origem.Width)
                            {
                                cropHeight = origem.Height;
                                startX     = (origem.Width - cropWidth) / 2;
                            }
                            else
                            {
                                cropHeight = (int)Math.Round(origem.Height * ((double)origem.Width / cropWidth));
                                cropWidth  = origem.Width;
                                startY     = (origem.Height - cropHeight) / 2;
                            }
                        }
                        else
                        {
                            cropHeight = (int)(Math.Round(origem.Width * croppedHeightToWidth));
                            if (cropHeight < origem.Height)
                            {
                                cropWidth = origem.Width;
                                startY    = (origem.Height - cropHeight) / 2;
                            }
                            else
                            {
                                cropWidth  = (int)Math.Round(origem.Width * ((double)origem.Height / cropHeight));
                                cropHeight = origem.Height;
                                startX     = (origem.Width - cropWidth) / 2;
                            }
                        }
                        #endregion
                    }
                    media = Imager.Crop(origem, new Rectangle(startX, startY, cropWidth, cropHeight));
                }
                #endregion


                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var imageCodecInfo = Imager.GetEncoderInfo(mimeType);

                // Imager.SaveJpeg(path + fileName, media != null ? media : origem);
                Imager.Save(path + fileName, media != null ? media : origem, imageCodecInfo);
            }

            return(fileName);
        }
Example #7
0
        public IHttpActionResult PutBlog(bool currentBlog, bool editHeader, EditBlogHeader header)
        {
            var blogId = CurrentUser.CurrentBlogId;

            if (CurrentUser.CurrentBlogId == null)
            {
                return(NotFound());
            }

            var blog = db.Blogs.Find(blogId);

            if (blog == null)
            {
                return(NotFound());
            }

            byte[] imageBytes = System.Convert.FromBase64String(header.File);

            Image image;

            using (MemoryStream ms = new MemoryStream(imageBytes))
            {
                image = Image.FromStream(ms);
            }
            int w = 1140;
            int h = 279;

            var resized = Imager.Crop(image, new Rectangle((int)header.X, (int)header.Y, w, h));

            string fileName = "header." + header.FileName.Split('.').Last();
            string path     = ServerTools.Paths.MediaFolderBlogsPath(blog.UrlName, fileName);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            resized.Save(path);

            blog.HeaderUrl = ServerTools.RelativePath(path);

            db.Entry(blog).State = EntityState.Modified;
            db.SaveChanges();

            return(Ok(new{ HeaderUrl = blog.HeaderUrl }));

            /*if (!ModelState.IsValid)
             * {
             *  return BadRequest(ModelState);
             * }*/
/*
 *          if (id != blog.Id)
 *          {
 *              return BadRequest();
 *          }
 *
 *          Models.Blog blogModel = db.Blogs.Find(id);
 *
 *          if (blogModel == null)
 *              return NotFound();
 *
 *          if (!BelongsToCurrentUser(blogModel))
 *              return Unauthorized();
 *
 *          Mapper.Map(blog, blogModel);
 *
 *          db.Entry(blogModel).State = EntityState.Modified;
 *
 *          try
 *          {
 *              db.SaveChanges();
 *          }
 *          catch (DbUpdateConcurrencyException)
 *          {
 *              if (!BlogExists(id))
 *              {
 *                  return NotFound();
 *              }
 *              else
 *              {
 *                  throw;
 *              }
 *          }*/

            //  return StatusCode(HttpStatusCode.NoContent);
        }