MVC上传图片

View:

 

@Html.BeginForm("UpLoad", "UpImage", FormMethod.Post, new { enctype = "multipart/form-data" });%>
    <div>
        <input type="file" name="File1" size="30" />
        <input type="submit" value="上传" />
    </div>
    <%Html.EndForm(); 


 

Controller:

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Mvc;
 
namespace FirstMVC.Controllers
{
    public class UpImageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult UpLoad()
        {
            if (UpLoadImage("~/UpImgs/"))
            {
                return Content("上传成功");
            }
            else
            {
                return Content("请确认上传的是图片");
            }
        }
        public bool UpLoadImage(string imgPath)
        {
            bool f = false;
            string fileName, fileExtension, GuidFileName;
            HttpFileCollectionBase files = Request.Files;//组织客户ì端上载的文件
            try
            {
                HttpPostedFileBase postedFile = files[0];
                fileName = Path.GetFileName(postedFile.FileName);
                if (fileName != "")
                {
                    fileExtension = Path.GetExtension(fileName);
                    GuidFileName = Guid.NewGuid().ToString().Replace("-", "") + fileExtension;
                    if (postedFile.ContentType.ToString().IndexOf("image").ToString() != "-1")
                    {
                        postedFile.SaveAs(Request.MapPath(imgPath) + GuidFileName);
                        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(imgPath) + GuidFileName);
 
                        #region 加图片水印
 
                        //string watermarkPath = "~/UpImgs/";
                        //System.Drawing.Image copyrightImg = System.Drawing.Image.FromFile(Server.MapPath(watermarkPath) + "watermark.jpg");
                        设置颜色矩阵
                        //int lucencyPercent = 40;
                        //float[][] matrixItems = {new float[]{1,0,0,0,0},
                        //new float[]{0,1,0,0,0},
                        //new float[]{0,0,1,0,0},
                        //new float[]{0,0,0,(float)lucencyPercent/100f,0},
                        //new float[]{0,0,0,0,1}
                        //};
                        //ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
                        //ImageAttributes imgAttr = new ImageAttributes();
                        //imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                        绘制阴影图象
                        //Graphics g = Graphics.FromImage(image);
                        //g.DrawImage(copyrightImg, new Rectangle(image.Width - copyrightImg.Width, image.Height - copyrightImg.Height, copyrightImg.Width, copyrightImg.Height), 0, 0, copyrightImg.Width, copyrightImg.Height, GraphicsUnit.Pixel, imgAttr);
                        //g.Dispose();
                        //image.Save(Server.MapPath(watermarkPath) + "watermark_" + GuidFileName);
                        //image.Dispose();
 
                        #endregion
 
                        #region 加文字水印
 
                        //Graphics g = Graphics.FromImage(image);
                        //g.DrawImage(image, 0, 0, image.Width, image.Height);
                        //Font font = new Font("Arial", 12);
                        //Brush b = new SolidBrush(Color.Chocolate);
                        //string addText = "秦迷空间";
                        //g.DrawString(addText, font, b, 10, 10);
                        //g.Dispose();
 
                        #endregion
 
                        image.Save(Server.MapPath(imgPath) + "Original_" + GuidFileName);
                        image.Dispose();
 
                        #region 生|¨2成¨|缩?略?图a?
 
                        int towidth, toheight;
                        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath(imgPath) + GuidFileName);
 
                        int ow = int.Parse(originalImage.Width.ToString());
                        int oh = int.Parse(originalImage.Height.ToString());
                        double dw = Convert.ToDouble(originalImage.Width.ToString());
                        double dh = Convert.ToDouble(originalImage.Height.ToString());
                        if (dw / dh > 4 / 3)
                        {
                            towidth = 200;
                            toheight = Convert.ToInt32(150 * dh / dw);
                        }
                        else
                        {
                            towidth = Convert.ToInt32(150 * dw / dh);
                            toheight = 150;
                        }
                        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
                        Graphics g1 = Graphics.FromImage(bitmap);
                        g1.InterpolationMode = InterpolationMode.High;
                        g1.SmoothingMode = SmoothingMode.HighQuality;
                        g1.Clear(Color.Transparent);
                        g1.DrawImage(originalImage, 0, 0, towidth, toheight);
 
                        g1.Dispose();
                        originalImage.Dispose();
                        originalImage = (System.Drawing.Image)bitmap.Clone();
                        originalImage.Save(Server.MapPath(imgPath) + "Thumb_" + GuidFileName, ImageFormat.Jpeg);
                        bitmap.Dispose();
 
                        #endregion
 
                        //删除原始图片
                        if (System.IO.File.Exists(Server.MapPath(imgPath) + GuidFileName))
                        {
                            System.IO.File.Delete(Server.MapPath(imgPath) + GuidFileName);
                        }
                        f = true;
                    }
                    else
                    {
                        f = false;
                    }
 
                }
                else
                {
                    f = false;
                }
            }
            catch
            {
                f = false;
            }
            return f;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值