Asp net mvc 5 resize image

Asked

Viewed 96 times

3

I am in need of a help to resize the image I received by uploading the user, 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>

1 answer

1

Yes.

Utilize 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
    }
  • Thanks Gypsy for the quick answer, but I was left with a doubt about how the model should be: on this line Oce puts a file if (meuModel.Typeimagefile1 != null && meuModel.File.Typeimagefile1 > 0) that got a little confused.

  • Yes, I pasted something wrong. I updated the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.