【问题标题】:ASP.NET MVC3 resizing images for user profileASP.NET MVC3 为用户配置文件调整图像大小
【发布时间】:2011-06-14 13:35:32
【问题描述】:

我想用图片创建用户个人资料。该用户可以上传他的照片,在他的个人资料中会有这张照片,在论坛中会有从原始照片创建的小图像。我没有显示图像但调整大小的问题。我在控制器中有此代码,用户可以在其中更改他的信息(姓名,年龄,...)并可以上传照片:

[HttpPost, ValidateInput(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();
    }
}

我的观点是这样的:

@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>
}

正如我所说,我只需要帮助将代码添加到控制器,从原始图像创建小图像并将其保存到。感谢您的帮助

【问题讨论】:

标签: c# asp.net-mvc-3 image-resizing user-profile


【解决方案1】:

在 C# 中重新调整图像大小的示例数不胜数。因此,只需选择一种您喜欢的方法。例如,这是@Craig Stuntz 链接的一个作为对您的问题的评论。如果您不喜欢这种方法,只需选择 another one 并适应即可。

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);
            }
        }
    }
}

【讨论】:

  • 我不知道如何将这些代码与我的文件变量一起使用,现在我知道了,谢谢。
  • 除非您在编码器上设置质量参数,否则您将获得相当大的 jpeg... 90 是最好的,虽然 80/85 可能对于缩略图是可以接受的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多