ImageResize() public method

Metoda která změní velikost obrázku a rovnou ho uploaduje
public ImageResize ( System.Web.HttpPostedFileBase file, string filePath, int maximumWidth, int maximumHeight ) : void
file System.Web.HttpPostedFileBase Fyzicky reprezentovaný soubor
filePath string Cesta k souboru
maximumWidth int Maximální šířka podle které se dyžtak ořízne
maximumHeight int Maximální výška podle které se dyžtak ořízne
return void
Example #1
0
        public ActionResult Edit(Felbook.Models.User model)
        {
            TryUpdateModel(CurrentUser);

            //upload změna profilového obrázku
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
            int userId = CurrentUser.Id;
            string fileDir = "../Web_Data/profile_images/";
            //název souboru je vždy stejný
            string fileName = "profileimage.png";
            string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
            string fileDirPath = Path.GetDirectoryName(fileFullPath);
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (model.OldPassword != null && String.Equals(model.Password, model.ConfirmPassword))
            {
                if (CurrentUser.CheckPassword(model.OldPassword))
                {
                    CurrentUser.ChangePassword(model.Password);
                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }

            if (ModelState.IsValid)
            {
                if (uploadImage == true)
                {
                    try
                    {
                        System.IO.File.Delete(fileFullPath);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Unexpected file error.");
                        return View("Index", "Profile", new { username = model.Username });
                    }
                    imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                }

                Model.UserService.Edit(CurrentUser);
                return View("Index", "Profile", new { username = model.Username });
            }
            //v případě nějaké chyby se vrátí tohle
            return View("Edit", "../Profile", new { username = model.Username });
        }
Example #2
0
        public ActionResult Edit(Felbook.Models.User model) //FormCollection collection
        {
            TryUpdateModel(CurrentUser);

            //upload změna profilového obrázku
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase          imageToUpload = Request.Files["profileimage"];
            int    userId  = CurrentUser.Id;
            string fileDir = "../Web_Data/profile_images/";
            //název souboru je vždy stejný
            string fileName     = "profileimage.png";
            string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
            string fileDirPath  = Path.GetDirectoryName(fileFullPath);
            bool   uploadImage  = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (model.OldPassword != null && String.Equals(model.Password, model.ConfirmPassword))
            {
                if (CurrentUser.CheckPassword(model.OldPassword))
                {
                    CurrentUser.ChangePassword(model.Password);
                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }

            if (ModelState.IsValid)
            {
                //zpráva pro uživatele že editoval profil bez problému
                ViewData["EditResult"] = "The edit is successful.";

                if (uploadImage == true)
                {
                    try
                    {
                        System.IO.File.Delete(fileFullPath);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Unexpected file error.");
                        return(View(CurrentUser));
                    }
                    imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                }

                Model.UserService.Edit(CurrentUser);
                return(View(CurrentUser));
            }
            //v případě nějaké chyby se vrátí tohle
            return(View(CurrentUser));
        }
Example #3
0
        public ActionResult Register(RegisterModel model)
        {
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    //upload user profile image
                    User actualUser = Model.UserService.FindByUsername(model.UserName);
                    int userId = actualUser.Id;
                    string fileDir = "../Web_Data/profile_images/";
                    //název souboru je vždy stejný
                    string fileName = "profileimage.png";
                    string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
                    string fileDirPath = Path.GetDirectoryName(fileFullPath);

                    try
                    {
                        //pokusíme se vytvořit adresář
                        Directory.CreateDirectory(fileDirPath);
                    }
                    //jednotlivě odchytávám chyby
                    catch (UnauthorizedAccessException)
                    {
                        ModelState.AddModelError("file", "Upload wasn´t successful");
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Some uknown error");
                    }

                    if (uploadImage == true)
                    {
                        imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                    }
                    else
                    {
                        //zjistím si cesty k souboru
                        string sourceFile = Path.Combine(HttpContext.Server.MapPath(fileDir + "/default/"), fileName);
                        string destFile = System.IO.Path.Combine(fileDirPath, fileName);

                        //kopíruje to soubor
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
                    return RedirectToAction("Index", "Profile", new { username = actualUser.Username });
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }
Example #4
0
        public ActionResult Register(RegisterModel model)
        {
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase          imageToUpload = Request.Files["profileimage"];
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    //upload user profile image
                    User   actualUser = Model.UserService.FindByUsername(model.UserName);
                    int    userId     = actualUser.Id;
                    string fileDir    = "../Web_Data/profile_images/";
                    //název souboru je vždy stejný
                    string fileName     = "profileimage.png";
                    string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
                    string fileDirPath  = Path.GetDirectoryName(fileFullPath);

                    try
                    {
                        //pokusíme se vytvořit adresář
                        Directory.CreateDirectory(fileDirPath);
                    }
                    //jednotlivě odchytávám chyby
                    catch (UnauthorizedAccessException)
                    {
                        ModelState.AddModelError("file", "Upload wasn´t successful");
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Some uknown error");
                    }

                    if (uploadImage == true)
                    {
                        imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                    }
                    else
                    {
                        //zjistím si cesty k souboru
                        string sourceFile = Path.Combine(HttpContext.Server.MapPath(fileDir + "/default/"), fileName);
                        string destFile   = System.IO.Path.Combine(fileDirPath, fileName);

                        //kopíruje to soubor
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
                    return(RedirectToAction("Index", "Profile", new { username = actualUser.Username }));
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return(View(model));
        }