0

画像付きのユーザープロファイルを作成したい。そのユーザーは自分の写真をアップロードできます。プロフィールにはこの写真があり、フォーラムには元の写真から作成された小さな画像があります。画像の表示は問題ありませんが、サイズ変更は問題ありません。私はコントローラーにこのコードを持っており、ユーザーは自分の情報(名前、年齢など)を変更したり、写真をアップロードしたりできます。

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

私が言ったように、私は元の画像から小さな画像を作成して保存するコードをコントローラーに追加するのに助けが必要です。助けてくれてありがとう

4

1 に答える 1

1

C#で画像のサイズを変更する例は数え切れないほどあります。だからあなたが好きな方法を1つ選んでください。たとえば、質問へのコメントとして@CraigStuntzによってリンクされたものがあります。この方法が気に入らない場合は、別の方法を選択して適応させてください。

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);
            }
        }
    }
}
于 2011-06-14T13:46:40.490 に答える