在Asp.net MVC上上传制作拇指图像

本文关键字:图像 Asp net MVC | 更新日期: 2023-09-27 18:04:32

在图像上传时,我想复制该图像,保存为不同的名称并调整尺寸

[HttpPost]
public ActionResult Create(HttpPostedFileBase photo)
{
    string path =  System.Configuration.ConfigurationManager.AppSettings["propertyPhotoPath"].ToString(); 
    if ((photo != null) && (photo.ContentLength > 0))
    {
        var fileName = Path.GetFileName(photo.FileName);
        var pathToSaveOnHdd = Path.Combine(Server.MapPath(path), fileName);
        string dbPhotoPath = string.Format("{0}{1}", path, fileName);
    }
... 
//        to do: make image copy, change dimensions
}

在Asp.net MVC上上传制作拇指图像

要复制文件,您可以使用File.Copy方法。要调整图像的大小,有许多技术,包括GDI+, WIC, WPF(这里是similar post中的一个示例)或NuGet(如ImageResizer)。

可以将上传的文件从ActionController转换为字节,并更改流的大小,如下所示

Byte[] image1 = new Byte[photo.]ContentLength - 1];HttpPostedFileBase file = photo.PostedFile;file.InputStream。读取(image1, 0)文件。内容长度- 1);System.IO.MemoryStream ms = new System.IO.MemoryStream(image);

,您可以使用图形类重新绘制具有所需大小的图像,如下所示image = image . fromstream (ms);

Graphic = Graphics.FromImage(image);图形。DrawImage(image, 0,0, image.)宽度,image.Height);