MVC4 上传图片并生成缩略图_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > MVC4 上传图片并生成缩略图

MVC4 上传图片并生成缩略图

 2013/7/25 10:43:26  放哨De老鼠  博客园  我要评论(0)
  • 摘要:Views@using(Html.BeginForm("Create","img",FormMethod.Post,new{enctype="multipart/form-data"})){<tablewidth="600"border="0"><tr><tdwidth="83">图片:</td><tdwidth="507"><inputtype="file"name="filImgs"id="filImgs"/><
  • 标签:MVC 图片 上传

Views

class="brush:html;gutter:false;">@using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <table width="600" border="0">
  <tr>
    <td width="83">图片:</td>
    <td width="507">    <input type="file" name="filImgs" id="filImgs"/></td>
  </tr>
  <tr>
    <td>链接地址:</td>
    <td>   @Html.EditorFor(model => model.LinkUrl)</td>
  </tr>
        <tr>
    <td>  <b>排序: </b></td>
    <td>    @Html.EditorFor(model => model.Orderby)</td>
  </tr>
  <tr>
    <td> </td>
    <td>  <input type="button" onclick="Check(1)" value="新增" /></td>
  </tr>
</table>
}

 JS

javascript;gutter:false;">function Check(str) {
    var img = $("#filImgs").val();
    var Orderby = $("#Orderby").val();
    var reg = new RegExp("^[0-9]*$");
    if(img.length<=0&&str==1)
    {
        alert('请上传图片');
        return false;
    }
    else if (Orderby.length <= 0) {
        alert("排序不能为空!");
        return false;
    }
    else if (Orderby.length > 0 && !reg.test(Orderby)) {

        alert("请输入数字!");
        return false;


    }
    else {

        document.forms[0].submit();
    }
}

  

 

 Conrollers

 private EFHelpDB db = new EFHelpDB(); 
public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Img/Create

        [HttpPost]
      
        public ActionResult Create(tb_Img tb_img)
        {
            try
            {
                var file = Request.Files[0];
                var img = new upload().UpLoadImage(file, "/upfile/", "thumb_", 89, 71);
                if (img.IndexOf("thumb_") > -1)
                {
                    tb_img.ImgUrl = img;
                }

                db.Img.Add(tb_img);
                db.SaveChanges();
                return RedirectToAction("Index");

            }
            catch(Exception ex)
            {
                return View("index");
            }
            
        }

  

Models

public class tb_Img
    {
        public int ID { get; set; }
        public string LinkUrl { get; set; }
        public string ImgUrl { get; set; }
        public int Orderby { get; set; }
    }

  上传图片类

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
namespace MVC.Models
{
    public class upload
    {
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="fulFile">上传控件</param>
        /// <param name="DirectionName">文件所放的父文件夹</param>
        /// <returns>string</returns>
        public upload() { }

        /// <summary> 
        /// asp.net上传图片并生成缩略图 
        /// </summary> 
        /// <param name="upImage">HttpPostedFileBase控件</param> 
        /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param> 
        /// <param name="sThumbExtension">缩略图的thumb</param> 
        /// <param name="intThumbWidth">生成缩略图的宽度</param> 
        /// <param name="intThumbHeight">生成缩略图的高度</param> 
        /// <returns>缩略图名称</returns> 
        public string UpLoadImage(HttpPostedFileBase upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
        {
            string sThumbFile = "";
            string sFilename = "";

            if (upImage.FileName != null)
            {
             
                int nFileLen = upImage.ContentLength;
                if (nFileLen == 0)
                    return "没有选择上传图片";

                //获取upImage选择文件的扩展名 
                string extendName = System.IO.Path.GetExtension(upImage.FileName).ToLower();
                //判断是否为图片格式 
                if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
                    return "图片格式不正确";


                byte[] myData = new Byte[nFileLen];
                upImage.InputStream.Read(myData, 0, nFileLen);

                sFilename = System.IO.Path.GetFileName(upImage.FileName);
                int file_append = 0;
                //检查当前文件夹下是否有同名图片,有则在文件名+1 
                while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
                {
                    file_append++;
                    sFilename = System.IO.Path.GetFileNameWithoutExtension(upImage.FileName)
                        + file_append.ToString() + extendName;
                }
                System.IO.FileStream newFile
                    = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
                    System.IO.FileMode.Create, System.IO.FileAccess.Write);
                newFile.Write(myData, 0, myData.Length);
                newFile.Close();

                //以上为上传原图

                try
                {
                    //原图加载 
                    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
                    {
                        //原图宽度和高度 
                        int width = sourceImage.Width;
                        int height = sourceImage.Height;
                        int smallWidth;
                        int smallHeight;

                        //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高) 
                        if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                        {
                            smallWidth = intThumbWidth;
                            smallHeight = intThumbWidth * height / width;
                        }
                        else
                        {
                            smallWidth = intThumbHeight * width / height;
                            smallHeight = intThumbHeight;
                        }

                        //判断缩略图在当前文件夹下是否同名称文件存在 
                        file_append = 0;
                        sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(upImage.FileName) + extendName;

                        while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
                        {
                            file_append++;
                            sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(upImage.FileName) +
                                file_append.ToString() + extendName;
                        }
                        //缩略图保存的绝对路径 
                        string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;

                        //新建一个图板,以最小等比例压缩大小绘制原图 
                        using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
                        {
                            //绘制中间图 
                            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                            {
                                //高清,平滑 
                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                g.Clear(Color.Black);
                                g.DrawImage(
                                sourceImage,
                                new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
                                new System.Drawing.Rectangle(0, 0, width, height),
                                System.Drawing.GraphicsUnit.Pixel
                                );
                            }
                            //新建一个图板,以缩略图大小绘制中间图 
                            using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
                            {
                                //绘制缩略图 
                                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
                                {
                                    //高清,平滑 
                                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                    g.Clear(Color.Black);
                                    int lwidth = (smallWidth - intThumbWidth) / 2;
                                    int bheight = (smallHeight - intThumbHeight) / 2;
                                    g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                                    g.Dispose();
                                    bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //出错则删除 
                    System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
                    return "图片格式不正确";
                }
                //返回缩略图名称 
                return sThumbFile;
            }
            return "没有选择图片";
        }

    }
}

  





 

发表评论
用户名: 匿名