繁体   English   中英

上传的asp.net mvc制作拇指图像

[英]asp.net mvc on upload make thumb image

在图像上传中,我想使用不同的名称和调整尺寸来制作该图像的副本。

[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
}

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

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

Byte [] image1 = new Byte [photo.ContentLength - 1]; HttpPostedFileBase file = photo.PostedFile; file.InputStream.Read(image1,0,file.ContentLength - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(image1);

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

Graphic graphic = Graphics.FromImage(image); graphic.DrawImage(image,0,0,image.Width,image.Height);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM