mvc3 ImageResizer

2020-05-09 15:57发布

问题:

I downloaded the Nugent ImageResizer and I am trying to resize a picture on upload following an example on this page http://imageresizing.net/docs/managed but I can't seen to put this in a Var or Image variable so i can see it in the Path.Combine here is the code

var fileName = Path.GetFileName(file.FileName);
                var changename = getid + "_" + fileName;
          ImageBuilder.Current.Build(changename, changename,
                                                       new ResizeSettings("width=130&height=130"));

            var path = Path.Combine(Server.MapPath("~/uploads/profilepic"), changename);
                file.SaveAs(path);

How can I get the ImageBuilder inside a var or some type of image variable what i would like to do is something like this

        var resized=  ImageBuilder.Current.Build(changename, changename,
                                                       new ResizeSettings("width=130&height=130"));
var path = Path.Combine(Server.MapPath("~/uploads/profilepic"), resized);
                file.SaveAs(path);

all that im trying to do is put the ImageBuilder inside the Path.Combine without getting an error, any help would be appreciated .

回答1:

ImageResizer should be given the uploaded file and the output path directly

ImageResizer supports both GUIDs and path sanitization. NEVER use the uploaded filename as-is!

var i = new ImageJob(file, 
                    "~/uploads/profilepic/<guid>_<filename:A-Za-z0-9>.<ext>", 
                     new ResizeSettings("width=130&height=130&format=jpg"));
i.CreateParentDirectory = true; //Auto-create the uploads directory.
i.Build();

var newVirtualPath = ImageResizer.Util.PathUtils.GuessVirtualPath(i.FinalPath);