Felbook.Helpers.ImageHelper.ImageResize C# (CSharp) Method

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
        public void ImageResize(HttpPostedFileBase file, string filePath, int maximumWidth, int maximumHeight)
        {
            if (file != null && file.FileName != "")
            {
                int maxWidth = maximumWidth; //maximální šířka
                int maxHeight = maximumHeight; //maximální výška

                string strExtension = Path.GetExtension(file.FileName);
                if (strExtension.ToUpper() == ".JPG" || strExtension.ToUpper() == ".GIF" || strExtension.ToUpper() == ".PNG")
                {
                    // změní velikost obrázku
                    Image imageToBeResized = Image.FromStream(file.InputStream);
                    int imageHeight = imageToBeResized.Height;
                    int imageWidth = imageToBeResized.Width;
                    imageHeight = (imageHeight * maxWidth) / imageWidth;
                    imageWidth = maxWidth;

                    if (imageHeight > maxHeight)
                    {
                        imageWidth = (imageWidth * maxHeight) / imageHeight;
                        imageHeight = maxHeight;
                    }

                    Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
                    bitmap.Save(filePath);
                }
            }
        }

Usage 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 });
        }
All Usage Examples Of Felbook.Helpers.ImageHelper::ImageResize