上传到文件夹后,我正在调整图像大小。我已经以这种格式 /Content/Uploads//e1f1d755-2b6c-4ec0-8b5e-0a5fa569292c.Img0001.jpg 将路径存储在 db 中,但是在调整图像大小时,我给出了相同的路径我已将其保存在变量路径中。但它以这种形式出现 C:\Content\Uploads\e1f1d755-2b6c-4ec0-8b5e-0a5fa569292c.Img0001.jpg 导致保存调整大小的图像时出错。找不到路径 C 的一部分:\Content\Uploads\e1f1d755-2b6c-4ec0-8b5e-0a5fa569292c.Img0001.jpg 这是我的代码:
[HttpPost]
public ActionResult UploadPic(FileManagement fmanage, HttpPostedFileBase file)
{
var path = String.Empty;
var directory = "/Content/Uploads/";
if (file != null && file.ContentLength > 0)
{
String FileExtn = System.IO.Path.GetExtension(file.FileName).ToLower();
if (!(FileExtn == ".jpg" || FileExtn == ".png" || FileExtn == ".gif"))
{
ViewBag.error = "Only jpg, gif and png files are allowed!";
}
else
{
var fileName = string.Format("{0}.{1}", Guid.NewGuid(), Path.GetFileName(file.FileName));
path = Path.Combine(directory + "/", fileName);
file.SaveAs(Server.MapPath(path));
using (var input = new Bitmap(file.InputStream))
{
int width;
int height;
if (input.Width > input.Height)
{
width = 128;
height = 128 * input.Height / input.Width;
} //if
else
{
height = 128;
width = 128 * input.Width / input.Height;
} //else
using (var thumb = new Bitmap(width, height))
using (var graphic = Graphics.FromImage(thumb))
{
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphic.DrawImage(input, 0, 0, width, height);
using (var output = System.IO.File.Create(path))
{
thumb.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
} //using
} //using
}
}
如何在存储调整大小的图像时拥有相同的路径。在此先感谢您的帮助。