我正在使用MVC3通过webform发送图像集。控制器接收此发布的图像并将其名称保存到db中。
[HttpPost]
public ActionResult Edit(MyViewModel data, IEnumerable<HttpPostedFileBase> postedImages)
{
if (ModelState.IsValid)
{
using (session...and transaction...)
{
MyModel model = session.Get<MyModel>(data.Id);
data.SendToDomainModel(model, session);
foreach (var image in postedImages)
{
if ((image != null) && (image.ContentLength > 0))
{
Photo photo = new Photo();
var fileName = Path.GetFileName(image.FileName);
// path used to save actuall image to the hdd path
var pathToSave = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);
// path used to save image path inside db column
var path = Path.Combine("/Content/uploads/" + fileName);
photo.MyModel= session.Load<MyModel>(model.Id);
photo.Path = path;
photo.Name = fileName;
image.SaveAs(pathToSave);
model.Photos.Add(photo);
}
}
// commit transaction ..
// save session ..
}
return RedirectToAction("Index");
}
else { return View(data); }
}
如何使用图像采集中的第一张图像并使用文件名前缀“firstImage”进行复制并裁剪为50x50像素尺寸?
由于
答案 0 :(得分:1)
使用System.Drawing GetThumbnailImage
示例:
Image thumb=image.GetThumbnailImage(50, 50, null, IntPtr.Zero);
对于没有the pitfalls listed here的更优化方法,请参阅: