Exemple #1
0
        public ActionResult ResizeImage(HttpPostedFileBase imageToUpload)
        {
            if (imageToUpload != null)
            {
                //save image
                string filename = Path.GetFileNameWithoutExtension(imageToUpload.FileName);
                filename = DateTime.Now.ToString("yyyyMMddhhmmss") + filename;
                using (var image = Image.FromFile(imageToUpload.FileName))
                    using (var newImage = ImageCreator.ScaleImage(image, 800, 600))
                    {
                        newImage.Save(Server.MapPath("../Images/" + filename + ".jpg"), ImageFormat.Jpeg);
                        //newImage.Save(@"c:\test.png", ImageFormat.Png);
                    }

                using (MemoryStream ms = new MemoryStream())
                {
                    imageToUpload.InputStream.CopyTo(ms);
                    //return File(ImageCreator.ResizeImage(ms.GetBuffer(), 800, 600),"image /jpeg");
                    return(File(ImageCreator.ScaleImage(ms.GetBuffer(), 800, 600, ImageFormat.Jpeg), "image/jpeg"));
                }
            }

            return(View());
        }