From:一条被猫抛弃的他乡流浪狗!

C#图片压缩

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace UpLoadImg
{
    using System.Drawing;
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断当前请求方式
            string method = Request.HttpMethod;
            //判断
            if (method == "POST")
            {
                //1.0接收上传过来的文件
                HttpFileCollection files = Request.Files;//如果上传页面有多个上传文本框,那么返回回来的是一个集合,如果只有一个,那么这个Files中只有一条数据
                //2.0判断文件是否存在 
                if (files.Count > 0)
                {
                    //得到上传过来的文件
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)//ContentLength上传过来流文件的长度==》
                    {
                        #region 1.0生成图片的名称以及上传的路径
                        //得到原图的后缀
                        string extName = System.IO.Path.GetExtension(file.FileName);
                        //生成新的名称
                        string newName = Guid.NewGuid() + extName;

                        //得到上传路径
                        string imgPath = Server.MapPath("/upload/img/") + newName;
                        string thumPath = Server.MapPath("/upload/thum/") + newName;
                        #endregion

                        //参数设置
                        

                        #region 2.0上传图片和生成缩略图

                        //判断文本类型ContentType
                        if (file.ContentType.Contains("image/"))
                        {
                            //将上传过来的流转成图片
                            using (Image img = Image.FromStream(file.InputStream))
                            {
                                //生成缩略图
                                Image thumImg = img.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
                                thumImg.Save(thumPath);

                                #region 2.1打水印
                                //给原图准备一个画家
                                using (Graphics g = Graphics.FromImage(img))
                                {
                                    //1.0得到水印图片
                                    using (Image waterImg = Image.FromFile(Server.MapPath("/upload/1.png")))
                                    {
                                        var x = img.Width - waterImg.Width;
                                        var y = img.Height - waterImg.Height;
                                        //第一个矩形:指定水印显示大小(后两个)和位置(前两个)
                                        //第二个矩形:指定水印显示部分的大小
                                        //把整个水印图片打到右下角
                                        g.DrawImage(waterImg, new Rectangle(x, y, waterImg.Width, waterImg.Height), new Rectangle(0,0, waterImg.Width, waterImg.Height), GraphicsUnit.Pixel);
                                    }
                                }
                                #endregion
                                img.Save(imgPath);
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('请选择图片进行上传');window.location='/Admin/Product/Index.aspx'</script>");
                            Response.End();
                        }
                        #endregion
                     
                    }
                    else
                    {
                        Response.Write("<script>alert('请选择文件之后再上传');window.location='/Admin/Product/Index.aspx'</script>");
                        Response.End();
                    }
                }

            }
        }
    }
}

  

posted @ 2016-06-02 19:55  ICE_Inspire  阅读(218)  评论(0)    收藏  举报