asp.net-mvc-5 – asp net mvc 5 resize image

Question:

I'm in need of help resizing the image I've uploaded from the user, 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>

Answer:

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
    }
Scroll to Top
AllEscort