我使用asp.net mvc 4
建立一個網站,用戶可以在上傳之前裁剪圖像,但由於某種原因,它不是裁剪。這裏是我的代碼,WebImage作物不在asp.net工作mvc
控制器
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult AdminProfilePic(HttpPostedFileBase file, int Top, int Left, int Bottom, int Right)
{
if (file != null)
{
string picName = User.Identity.Name;
WebImage img = new WebImage(file.InputStream);
string picExt = Path.GetExtension(file.FileName);
if (picExt == ".jpg" || picExt == ".gif" || picExt == ".jpeg" || picExt == ".png")
{
picExt = "PNG";
string path = System.IO.Path.Combine(Server.MapPath("~/Images/Owners/"), picName);
img.Crop(Top, Left, Bottom, Right);
img.Save(path, picExt);
TempData["pp_success"] = "Your profile picture has been updated successfully!";
return RedirectToAction("AdminProfilePic");
}
else
{
TempData["pp_fail"] = "Error! Please upload a valid image file only!";
return RedirectToAction("AdminProfilePic");
}
}
else
{
TempData["pp_fail"] = "Error! No File was selected!";
return RedirectToAction("AdminProfilePic");
}
}
查看
@using (Html.BeginForm("AdminProfilePic", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Error! Please provide valid information!")
<input type="file" name="file" id="file01" style="width: 100%;" /><br />
<img id="blah01" src="#" alt="your image" /><div id="cropper"></div>
@Html.TextBox("Top", null, new { id = "Top" })<br />
@Html.TextBox("Left", null, new { id = "Left" })<br />
@Html.TextBox("Bottom", null, new { id = "Bottom" })<br />
@Html.TextBox("Right", null, new { id = "Right" })<br />
<input type="submit" class="btn btn-success" value="Update Profile Picture" />
}
JS
$(document).ready(function() {
var img_left = 0;
var img_top = 0;
$("#blah01").load(function() {
var imgCor = $("#blah01").position();
img_left = imgCor.left;
img_top = imgCor.top;
});
$("#cropper").draggable({
containment: "#blah01", scroll: false,
stop: function() {
var getCor = $("#cropper").position();
$("#Top").val(getCor.top - img_top);
$("#Left").val(getCor.left - img_left);
$("#Bottom").val(getCor.top - img_top + 200);
$("#Right").val(getCor.left - img_left + 160);
}
});
});
有什麼不對勁以米y代碼?如何使用文本框中的給定值使用WebImage Crop()
函數裁剪圖像?嚴重需要這個幫助!謝謝。
你得到的錯誤是什麼? –
我沒有得到任何錯誤。它不是剪裁,而是上傳整個圖像。它應該根據文本框的值裁剪,然後上傳裁剪後的圖像。 –
不,根據你的代碼,我相信視圖是上傳整個圖像,然後在服務器上根據作物的尺寸裁剪圖像 –