【问题标题】:asp.net mvc on upload make thumb image上传时的asp.net mvc制作拇指图像
【发布时间】:2013-03-19 16:47:48
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      Byte[] image1 = new Byte[photo.ContentLength - 1]; HttpPostedFileBase 文件 = 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);

      图形图形 = Graphics.FromImage(image); graphics.DrawImage(image, 0, 0, image.Width, image.Height);

      【讨论】:

        猜你喜欢
        • 2012-03-23
        • 2014-12-23
        • 2017-10-20
        • 2015-06-15
        • 2012-05-11
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        相关资源
        最近更新 更多