我想上传多个图像并将其保存到我的应用程序中的一个文件夹中,并将相同图像的缩略图保存在一个名为 Thumbnail 的文件夹中。我成功地将图像上传并保存在图像文件夹中。我也想保存它的缩略图。
在视图中
<form action="" method="post" enctype="multipart/form-data">
@Html.Label("Select the property : ");
@Html.DropDownList("Address", new SelectList(ViewBag.Address as System.Collections.IEnumerable), "---Select---",new { id = "add"})
<label for="file">Filename:</label>
<input type="file" name="files" id="file" multiple="true"/>
<input type="submit" />
在控制器中
public class HomeController : Controller
{
PropertyAssessmentSystemEntities db = new PropertyAssessmentSystemEntities();
public ActionResult Index()
{
var q = from pi in db.PropertyInfoes
select pi.Full_Address;
ViewBag.Address = q.ToList();
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files,FormCollection c)
{
int no = 0,no1 =0;
string path = Path.Combine(Server.MapPath("~/Images"), c["Address"].Replace(' ', '_'));
string fn = "", fn1 = "";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
else
no = Directory.GetFiles(path).Length;
foreach (var file in files)
{
string fname = "img_" + no++ + file.FileName.Substring(file.FileName.LastIndexOf('.'));
string fname1 = "img_" + no1++ + file.FileName.Substring(file.FileName.LastIndexOf('.'));
fname1 = Path.Combine("Thumbnail", fname1);
fn1 = Path.Combine(path, fname1);
if (!Directory.Exists(fn1))
Directory.CreateDirectory(fn1);
else
no = Directory.GetFiles(fn1).Length;
fn = Path.Combine(path, fname);
file.SaveAs(fn);
Stream strm = new FileStream(fn, FileMode.OpenOrCreate);
GenerateThumbnails(0.05, strm, fn1);
}
return RedirectToAction("Index");
}
private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath,image.RawFormat);
} }}
执行此操作时,thumbnailImg.Save(targetPath,image.RawFormat);
在 GDI+ 中发生一般错误中发生错误。任何人都可以帮我解决这个问题吗?