开发者

ASP.NET MVC3 resizing images for user profile

I want to create user profile with image. That user can upload his photo, in his profile there would be this photo and in forum there would be small image created from original photo. I have no problem with showing image but with resizing. I have this code in controller where user can change his information (name, age, ...) and can upload photo:

[HttpPost, ValidateInpu开发者_高级运维t(false)]
public ActionResult Upravit(int id, FormCollection collection, HttpPostedFileBase file)
{
    try
    {
        var user = repo.Retrieve(id);

        if (TryUpdateModel(user))
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Fotky/Profilove/"), (user.Name.Name + " " + user.Name.Surname + Path.GetExtension(fileName)));
                file.SaveAs(path);
                user.ImagePath = "/Fotky/Profilove/" + user.Name.Name + " " + user.Name.Surname + Path.GetExtension(fileName);

            }
            repo.Save(user);
            return RedirectToAction("Index");
        }
        return View();
    }
    catch
    {
        return View();
    }
}

and my view looks like this:

@using (Html.BeginForm("Upravit", "Uzivatel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>
        @Html.HiddenFor(model => model.UserID)
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        .
        .
        <input type="file" name="file" id="file" />
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

As I said I just need help with adding code to controller which create small image from original and save it to. Thanks for any help


There are like gazillion of examples out there of re-sizing images in C#. So just pick one method you like. Here's for for example the one linked by @Craig Stuntz as comment to your question. If you don't like this method just pick another one and adapt.

if (file != null && file.ContentLength > 0)
{
    var fileName = Path.GetFileName(file.FileName);
    // TODO: adjust the filenames here
    var path = Path.Combine(Server.MapPath("~/"), fileName);
    using (var input = new Bitmap(file.InputStream))
    {
        int width; 
        int height; 
        if (input.Width > input.Height) 
        { 
            width = 128; 
            height = 128 * input.Height / input.Width; 
        } 
        else 
        { 
            height = 128; 
            width = 128 * input.Width / input.Height; 
        }
        using (var thumb = new Bitmap(width, height))
        using (var graphic = Graphics.FromImage(thumb))
        {
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

            graphic.DrawImage(input, 0, 0, width, height);
            using (var output = System.IO.File.Create(path))
            {
                thumb.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜