在将图像保存到文件夹之前调整图像大小 c# ASP.NET
Resize image before saving it to folder c# ASP.NET
我正在尝试调整将通过文件输入上传的图像的大小,然后再将其保存到定向文件夹。我尝试了很多可能的方法,但我仍然无法得到它。请帮帮我!
查看
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="btnFileUpload" class="zone" runat="server"/>
}
Ajax
$('#btnFileUpload').fileupload({
url: '@Url.Action("UploadFile")',
dataType: 'json',
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
},
success: function (e, data) {
//window.location.reload();
console.log("Done");
console.log(e);
});
控制器
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
string _FileName = "";
string _path = "";
try
{
if (file.ContentLength > 0)
{
_FileName = Path.GetFileName(DateTime.Now.ToBinary() + "-" + file.FileName);
_path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine("File Uploaded Successfully!!");
return Json(imageUrls);
}
catch
{
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine("File upload failed!!");
return Json(ViewBag.Message);
}
}
这个任务有很多很棒的库可以处理你需要的一切。
我个人使用ImageProcessor。而且我知道还有更多质量相同甚至更好的产品。如果你想要我可以帮你使用它,但是文档应该足够了。
--编辑--
这是使用 ImageProcessor 的示例代码,看看是否有帮助:
var imageFactory = new ImageFactory(true);
imageFactory.Load(inputStream).Resize(
new ResizeLayer(new Size(128, 128), ResizeMode.Max)).
Save(path);
另外不要忘记安装所需的 nuget 包:)
这里是你的场景的实现
我认为这会起作用
[HttpPost]
public ActionResult UploadFile (HttpPostedFileBase file) {
string _FileName = "";
string _path = "";
try {
if (file.ContentLength > 0) {
_FileName = Path.GetFileName (DateTime.Now.ToBinary () + "-" + file.FileName);
_path = Path.Combine (Server.MapPath ("~/UploadedFiles"), _FileName);
byte[] fileData = null;
using (var binaryReader = new BinaryReader (file.InputStream)) {
fileData = binaryReader.ReadBytes (file.ContentLength);
}
HandleImageUpload (fileData, _path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine ("File Uploaded Successfully!!");
return Json (imageUrls);
} catch {
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine ("File upload failed!!");
return Json (ViewBag.Message);
}
}
private Image RezizeImage (Image img, int maxWidth, int maxHeight) {
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img) {
Double xRatio = (double) img.Width / maxWidth;
Double yRatio = (double) img.Height / maxHeight;
Double ratio = Math.Max (xRatio, yRatio);
int nnx = (int) Math.Floor (img.Width / ratio);
int nny = (int) Math.Floor (img.Height / ratio);
Bitmap cpy = new Bitmap (nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage (cpy)) {
gr.Clear (Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage (img,
new Rectangle (0, 0, nnx, nny),
new Rectangle (0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private MemoryStream BytearrayToStream (byte[] arr) {
return new MemoryStream (arr, 0, arr.Length);
}
private void HandleImageUpload (byte[] binaryImage, string path) {
Image img = RezizeImage (Image.FromStream (BytearrayToStream (binaryImage)), 103, 32);
img.Save (path, System.Drawing.Imaging.ImageFormat.jpg);
}
resize 代码取自此thread
我正在尝试调整将通过文件输入上传的图像的大小,然后再将其保存到定向文件夹。我尝试了很多可能的方法,但我仍然无法得到它。请帮帮我!
查看
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="btnFileUpload" class="zone" runat="server"/>
}
Ajax
$('#btnFileUpload').fileupload({
url: '@Url.Action("UploadFile")',
dataType: 'json',
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
},
success: function (e, data) {
//window.location.reload();
console.log("Done");
console.log(e);
});
控制器
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
string _FileName = "";
string _path = "";
try
{
if (file.ContentLength > 0)
{
_FileName = Path.GetFileName(DateTime.Now.ToBinary() + "-" + file.FileName);
_path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine("File Uploaded Successfully!!");
return Json(imageUrls);
}
catch
{
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine("File upload failed!!");
return Json(ViewBag.Message);
}
}
这个任务有很多很棒的库可以处理你需要的一切。
我个人使用ImageProcessor。而且我知道还有更多质量相同甚至更好的产品。如果你想要我可以帮你使用它,但是文档应该足够了。
--编辑--
这是使用 ImageProcessor 的示例代码,看看是否有帮助:
var imageFactory = new ImageFactory(true);
imageFactory.Load(inputStream).Resize(
new ResizeLayer(new Size(128, 128), ResizeMode.Max)).
Save(path);
另外不要忘记安装所需的 nuget 包:)
这里是你的场景的实现
我认为这会起作用
[HttpPost]
public ActionResult UploadFile (HttpPostedFileBase file) {
string _FileName = "";
string _path = "";
try {
if (file.ContentLength > 0) {
_FileName = Path.GetFileName (DateTime.Now.ToBinary () + "-" + file.FileName);
_path = Path.Combine (Server.MapPath ("~/UploadedFiles"), _FileName);
byte[] fileData = null;
using (var binaryReader = new BinaryReader (file.InputStream)) {
fileData = binaryReader.ReadBytes (file.ContentLength);
}
HandleImageUpload (fileData, _path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine ("File Uploaded Successfully!!");
return Json (imageUrls);
} catch {
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine ("File upload failed!!");
return Json (ViewBag.Message);
}
}
private Image RezizeImage (Image img, int maxWidth, int maxHeight) {
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img) {
Double xRatio = (double) img.Width / maxWidth;
Double yRatio = (double) img.Height / maxHeight;
Double ratio = Math.Max (xRatio, yRatio);
int nnx = (int) Math.Floor (img.Width / ratio);
int nny = (int) Math.Floor (img.Height / ratio);
Bitmap cpy = new Bitmap (nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage (cpy)) {
gr.Clear (Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage (img,
new Rectangle (0, 0, nnx, nny),
new Rectangle (0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private MemoryStream BytearrayToStream (byte[] arr) {
return new MemoryStream (arr, 0, arr.Length);
}
private void HandleImageUpload (byte[] binaryImage, string path) {
Image img = RezizeImage (Image.FromStream (BytearrayToStream (binaryImage)), 103, 32);
img.Save (path, System.Drawing.Imaging.ImageFormat.jpg);
}
resize 代码取自此thread