i've been looking into the awesome imageresizing.net tool, and am successfully using the URL API on my site.

However, i'm keen to look into the managed API, and resize a photo AS it is uploaded. I have started off by building a very basic upload page that uses the WebImage helper:

@using ImageResizer;

@{  
WebImage photo = null;
var newFileName = "";
var imagePath = "";

if(IsPost){
    photo = WebImage.GetImageFromRequest();
    if(photo != null){
        newFileName = Guid.NewGuid().ToString() + "_" +
            Path.GetFileName(photo.FileName);
        imagePath = @"images\" + newFileName;

        photo.Save(@"~\" + imagePath);
    }
}
}

I want to use the ImageResizing tool in this upload, to resize/crop to a specific size.

Below is the code that i need to insert (i have tested this by running on an existing image, and it works fine):

ImageResizer.ImageBuilder.Current.Build(imagePath,imagePath, 
new ResizeSettings("width=620&height=405&mode=crop"));

Any idea house i can merge the two, and resize BEFORE i save?

有帮助吗?

解决方案

You were so close! Server.MapPath is the missing piece you're looking for.

@{
    if (IsPost) {
        var photo = WebImage.GetImageFromRequest();
        if (photo != null) {
            var filename = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName);
            var localPath = Server.MapPath("~/") + @"images\" + filename;
            photo.Save(localPath);           
            ImageResizer.ImageBuilder.Current.Build(localPath, localPath, new ImageResizer.ResizeSettings("width=620&height=405&mode=crop"));
        }
    }
}

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>My Site's Title</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="mahfile">
            <input type="submit">
        </form>
    </body>
</html>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top