如何在二进制上传方法中调整 MVC.NET 中的图像大小?
How to resize of image in MVC.NET in Binrary upload method?
我的网站有一部分用于从客户端上传图片 side.I 显示有问题 images.Because 图片大小 bollix.How 我可以在上传图片时调整图片大小吗?
这是我的代码:
[HttpPost]
public ActionResult Create(int id,HttpPostedFileBase photoFile)
{
if (photoFile != null )
{
Photo photo = new Photo();
photo.Part = db.Parts.Find(id);
photo.PhotoContent = photoFile.ContentType;
photo.PhotoByte = new byte[photoFile.ContentLength];
photoFile.InputStream.Read(photo.PhotoByte, 0, photoFile.ContentLength);
db.Photos.Add(photo);
db.SaveChanges();
return RedirectToAction("Index", new { id = photo.Part.Id });
}
return View();
}
Stream str = new MemoryStream((Byte[])photo);
Bitmap loBMP = new Bitmap(str);
Bitmap bmpOut = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, 100, 100);
g.DrawImage(loBMP, 0, 0, 100, 100);
MemoryStream ms = new MemoryStream();
bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
bmpOut.Dispose();
ms.Close();
Response.BinaryWrite(bmpBytes);
Response.End();
我的网站有一部分用于从客户端上传图片 side.I 显示有问题 images.Because 图片大小 bollix.How 我可以在上传图片时调整图片大小吗?
这是我的代码:
[HttpPost]
public ActionResult Create(int id,HttpPostedFileBase photoFile)
{
if (photoFile != null )
{
Photo photo = new Photo();
photo.Part = db.Parts.Find(id);
photo.PhotoContent = photoFile.ContentType;
photo.PhotoByte = new byte[photoFile.ContentLength];
photoFile.InputStream.Read(photo.PhotoByte, 0, photoFile.ContentLength);
db.Photos.Add(photo);
db.SaveChanges();
return RedirectToAction("Index", new { id = photo.Part.Id });
}
return View();
}
Stream str = new MemoryStream((Byte[])photo);
Bitmap loBMP = new Bitmap(str);
Bitmap bmpOut = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, 100, 100);
g.DrawImage(loBMP, 0, 0, 100, 100);
MemoryStream ms = new MemoryStream();
bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
bmpOut.Dispose();
ms.Close();
Response.BinaryWrite(bmpBytes);
Response.End();