コード例 #1
0
        public ActionResult Resize(ImageViewModel viewModel)
        {
            viewModel.AttemptedSize = new Size(viewModel.AttemptedWidth, viewModel.AttemptedHeight);
            if (viewModel.AttemptedSize.IsEmpty)
                return RedirectToAction("Index");

            string imageDir = "Uploads/Images/";
            string originalImageName = "original.jpg";
            string imagePath = Server.MapPath("/Content/" + imageDir + originalImageName);
            using (var image = Image.FromFile(imagePath))
            {
                using (var ms = new MemoryStream())
                {
                    image.Save(ms, ImageFormat.Jpeg);
                    var img = new Imagery(ms.ToArray(), ImageFormat.Jpeg);
                    image.Dispose();
                    var imgEdit = new ImageManipulator();

                    img.SetResize(viewModel.AttemptedSize.Width, viewModel.AttemptedSize.Height);
                    byte[] imageResizeByte;
                    string error;
                    if (imgEdit.GetResizedImageByte(img, out imageResizeByte, out error))
                    {
                        byte[] optimizeImg = imgEdit.OptimizeImage(new MemoryStream(imageResizeByte), ImageFormat.Jpeg, viewModel.ImageQuality);
                        using (var newImageStream = new MemoryStream(optimizeImg))
                        {
                            using (var newImage = Image.FromStream(newImageStream))
                            {
                                string mapPath = Server.MapPath("/Content/");
                                string resizeName = "resized.jpg";
                                var fileInfo = new FileInfo(mapPath + imageDir + resizeName);
                                fileInfo.Delete();
                                newImage.Save(mapPath + imageDir + resizeName);
                            }
                        }
                    }
                }
            }

            return RedirectToAction("Index");
        }
コード例 #2
0
        public ActionResult Index(ImageViewModel viewModel)
        {
            var imageStream = viewModel.UploadFileImage.InputStream;
            string mapPath = Server.MapPath("/Content/");
            string imageDir = "Uploads/Images/";
            string originalName = "original.jpg";
            int widthLimit = 650;
            using (var image = Image.FromStream(imageStream))
            {
                if (this.IsImageExist(viewModel, originalName))
                {
                    var fileInfo = new FileInfo(mapPath + imageDir + originalName);
                    fileInfo.Delete();

                    byte[] resizeByte;
                    string error;
                    var img = new Imagery(imageStream, ImageFormat.Jpeg);
                    var imgEdit = new ImageManipulator();
                    img.SetResize(widthLimit);
                    if (imgEdit.GetResizedImageByte(img, out resizeByte, out error))
                    {
                        using (var ms = new MemoryStream(resizeByte))
                        {
                            var newImage = Image.FromStream(ms);
                            newImage.Save(mapPath + imageDir + originalName);
                            newImage.Dispose();
                            return RedirectToAction("Index");
                        }
                    }
                }
                image.Save(mapPath + imageDir + originalName);
            }

            return RedirectToAction("Index");
        }