asp net mvc 5 resize image

3

I need help resizing the image that I uploaded, do you have any suggestions?

Model:

public HttpPostedFileBase TypeImageFile1 { get; set; }

View:                                                                                                       

                            <div class="fileUpload btn btn-primary">
                                <span>Upload</span>
                                <input class="upload" id="TypeImageFile1" name="TypeImageFile1" type="file">
                            </div>
    
asked by anonymous 03.11.2015 / 20:34

1 answer

1

Yes.

Use the NuGet ImageResizer package .

Example:

    [HttpPost]
    public ActionResult Create(MeuModel meuModel)
    {
        if (meuModel.TypeImageFile1 != null && meuModel.TypeImageFile1.ContentLength > 0)
        {
            var fileName = Path.GetFileName(meuModel.TypeImageFile1.FileName);

            var path = Path.Combine(Server.MapPath("~/Upload/Imagens"), fileName); // Crie este diretório antes na sua aplicação.
            meuModel.TypeImageFile1.SaveAs(path);

            meuModel.NomeArquivo = fileName;
            ModelState.Remove("NomeArquivo");

            // Miniaturas
            ImageResizer.ImageJob i = new ImageResizer.ImageJob(meuModel.TypeImageFile1, 
                imagesDirectory + "/Uploads/Miniaturas/<guid>.<ext>", new ImageResizer.ResizeSettings(
                                        "width=250;height=250;format=jpg;mode=pad"));
            i.CreateParentDirectory = true;
            i.Build();
            meuModel.NomeArquivoMiniatura = i.FinalPath.Split('\').Last();
        }

        // Coloque aqui o restante da lógica do Controller
    }
    
03.11.2015 / 20:49