您的位置:程序门 -> .net技术 -> asp.net



asp.net如何实现图片压缩!如何实现文件传输!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


asp.net如何实现图片压缩!如何实现文件传输!
发表于:2007-03-06 08:56:02 楼主
大家好:
        请问asp.net中如何实现图片压缩!如何实现文件传输!
谢谢大家,请大家多多帮忙!
发表于:2007-03-06 09:06:461楼 得分:0
我写的上传控件看看对你有没有帮助

/*
  *   控件参数
  *  
  *   uploadpath               为图片保存的相对路径(使用该控件的页面相对于根目录的位置)
  *   issuccess                 返回图片是否保存成功,在使用页面中可以捕获该变量来决定是否进行下一步操作
  *   filename                   存储上传的图片名
  *   viewpicwidth           预览图片宽度
  *   viewpicheight         预览图片高度
  *   imgmaxsize               允许上传图片的大小(以k为单位,默认15k)
  *   upload()                   在使用页面中调用该方法即可保存图片
  *   makesmallpicture(...)   生成缩略图
  *  
  *   ---------------------------------------------------------------------------------
  *   功能说明:
  *  
  *   1.图片保存格式为:2007\1-19\49be57a0-2b2b-4e43-bd55-bf61f61cb559.jpg
  *   2.上传图片时有预览的功能
  *   3.修改时可以判断用户是否对图片做了修改,以防重覆上传
  *  
  *   ---------------------------------------------------------------------------------
  *   使用例子:
  *  
  *     [test.aspx.cs]
  *  
  *     //保存图片
  *     protected   void   button1_click(object   sender,   eventargs   e)
        {
                //缩略图名(路径+文件名)
                string   strsmallpicturename   =   "d:/test.jpg ";
                //配制上传控件的保存图片路径(该页面相对于根目录的路径为   ../   图片路径为   images/focuspic   )
                this.ucupload.uploadpath   =   "../ "   +   "images/focuspic ";
                //保存图片
                this.ucupload.upload();
                //判断是否保存成功
                if   (this.ucupload.issuccess   ==   "false ")
                {
                        showclientmessegebox( "图片上传失败 ");
                }
                //写进数据库
                //-----------------------------------------
                //保存   this.ucupload.filename   到数据库相应字段即可
                //-----------------------------------------

                //生成缩略图(高度以给定值200为准,宽度按比例)
                this.ucupload.makesmallpicture(server.mappath( "../ "   +   picconfigpath   +   this.ucupload.filename),   strsmallpicturename,   150,   200);
                //生成缩略图(宽度以给定值150为准,高度按比例)
                this.ucupload.makesmallpicture(server.mappath( "../ "   +   picconfigpath   +   this.ucupload.filename),   strsmallpicturename,   150,   200, "w ");
                //生成缩略图(以指定宽高缩略)
                this.ucupload.makesmallpicture(server.mappath( "../ "   +   picconfigpath   +   this.ucupload.filename),   strsmallpicturename,   150,   200,   "hw ");
                //生成缩略图并添加文字水印
                this.ucupload.makesmallpicture(server.mappath( "../ "   +   picconfigpath   +   this.ucupload.filename),   strsmallpicturename,   150,   200,   "h ",   "价值中国网 ",   " ");
                //生成缩略图并添加图片水印
                this.ucupload.makesmallpicture(server.mappath( "../ "   +   picconfigpath   +   this.ucupload.filename),   strsmallpicturename,   150,   200,   "h ",   " ",   "d:/btsearch.gif ");
        }
  *  
  *     //修改图片
  *     this.ucupload.filename   =   gdofocuspic.picurl;     //将数据库中图片的路径传给用户控件
  *    
  *  
  *   [test.aspx]
  *  
  *     <%@   register   src= "../usercontrols/upload.ascx "   tagname= "upload "   tagprefix= "uc1 "   %>
        <html   xmlns= "http://www.w3.org/1999/xhtml "   >
        <head   runat= "server ">
                <title> 无标题页 </title>
        </head>
        <body>
                <form   id= "form1 "   runat= "server ">
                        <div>
                                <uc1:upload   id= "ucupload "   runat= "server "   imgmaxsize= "10 "   viewpicwidth= "150 "   viewpicheight= "200 "> </uc1:upload>
                                <asp:button   id= "button1 "   runat= "server "   onclick= "button1_click "   text= "button "   />
                        </div>
                </form>
        </body>
        </html>
  *  
  */
发表于:2007-03-06 09:07:232楼 得分:0
using   system;
using   system.data;
using   system.configuration;
using   system.collections;
using   system.web;
using   system.web.security;
using   system.web.ui;
using   system.web.ui.webcontrols;
using   system.web.ui.webcontrols.webparts;
using   system.web.ui.htmlcontrols;
using   system.io;
using   system.configuration;
using   system.drawing;

public   partial   class   usercontrols_cvupload   :   system.web.ui.usercontrol
{
        protected   void   page_load(object   sender,   eventargs   e)
        {
                imgview.width   =   viewpicwidth;
                imgview.height   =   viewpicheight;
        }

        protected   void   lbtndelpic_click(object   sender,   eventargs   e)
        {
                if   (!string.isnullorempty(uploadpath   +   filename))
                {
                        string   filename   =   uploadpath   +   filename;
                        string   path   =   server.mappath(filename);
                        file.delete(path);
                }
        }

        #region   处理文件上传

        public   void   upload()
        {
                if   (this.upimage.value   !=   " "   &&   this.upimage.value   !=   null)
                {
                        string   strpicturepath   =   this.getmappath();
                        guid   g   =   guid.newguid();
                        datetime   time   =   datetime.now;
                        string   picturename   =   time.year   +   "/ "   +   time.month   +   "- "   +   time.day   +   "/ ";
                        string   strfullpath   =   this.upimage.value;
                        string   strfilename   =   strfullpath.substring(strfullpath.lastindexof( "\\ ")   +   1);
                        string   strfiletype   =   strfilename.substring(strfilename.lastindexof( ". ")   +   1);
                        string   strfiletype_tolower   =   strfiletype.tolower();
                        string   filename   =   g.tostring()   +   ". "   +   strfiletype;
                        filename   =   picturename   +   filename;
                        if   (strfiletype_tolower   ==   "jpg "   ¦ ¦   strfiletype_tolower   ==   "gif "   ¦ ¦   strfiletype_tolower   ==   "bmp ")
                        {
                                this.upimage.postedfile.saveas(server.mappath(strpicturepath)   +   g   +   ". "   +   strfiletype);
                        }
                }
                else
                {
                        issuccess   =   "false ";
                }
        }
        #endregion

        #region   创建图片文件夹并生成图片路径

        public   string   getmappath()
        {
                string   picturepath   =   uploadpath;

                datetime   time   =   datetime.now;
                string   year   =   time.year.tostring();
                string   day   =   time.month   +   "- "   +   time.day;
                string   basepath   =   server.mappath(picturepath).tostring();
                string   createpath   =   server.mappath(picturepath   +   "\\ "   +   year).tostring();
                if   (!directory.exists(createpath))
                {
                        directory.createdirectory(createpath);
                        directoryinfo   directory   =   new   directoryinfo(basepath);
                        string   path   =   year   +   "\\ "   +   day;
                        directory.createsubdirectory(path);
                }
                else
                {
                        string   nextpath   =   server.mappath(picturepath   +   "\\ "   +   year   +   "\\ "   +   day).tostring();
                        if   (!directory.exists(nextpath))
                        {
                                directoryinfo   directory   =   new   directoryinfo(basepath);
                                string   path   =   year   +   "\\ "   +   day;
                                directory.createsubdirectory(path);
                        }
                }
                return   picturepath   +   year   +   "/ "   +   day   +   "/ ";
        }
        #endregion
发表于:2007-03-06 09:07:323楼 得分:0
#region   控件参数

        //用于保存上传的文件
        public   string   filename
        {
                set  
                {  
                        session[ "filename "]   =   value;
                        string   strurl   =   httpcontext.current.request.url.pathandquery;
                        strurl   =   strurl.substring(0,   strurl.lastindexof( "/ ")+1);
                        if   (strurl.length   ==   7)
                        {
                                strurl   =   httpcontext.current.request.url.pathandquery;
                        }

                        this.imgview.src   =   strurl   +   uploadpath   +   value;
                }
                get  
                {  
                        return   (string)session[ "filename "];  
                }
        }

        //图片保存相对路径
        private   string   m_uplaodpath;
        public   string   uploadpath
        {
                set   {   m_uplaodpath   =   value;   }
                get   {   return   m_uplaodpath;   }
        }

        //用于判断图片是否上传成功
        private   string   m_strissuccess;
        public   string   issuccess
        {
                get   {   return   m_strissuccess;   }
                set   {   m_strissuccess   =   value;   }
        }

        //预览图片大小设置
        private   int   m_intviewpicheight   =   150;
        public   int   viewpicheight
        {
                get  
                {
                                return   m_intviewpicheight;
                }
                set   {   m_intviewpicheight   =   value;   }
        }
        private   int   m_intviewpicwidth=120;
        public   int   viewpicwidth
        {
                get  
                {
                                return   m_intviewpicwidth;
                }
                set   {   m_intviewpicwidth   =   value;   }
        }

        //上传图片最大不能超过
        private   int   m_intimgmaxsize   =   15;
        public   int   imgmaxsize
        {
                get   {   return   m_intimgmaxsize;   }
                set   {   m_intimgmaxsize   =   value;   }
        }
        #endregion

        #region     生成缩略图,添加水印

        ///   <summary>
        ///   生成缩略图,添加文字/图片水印
        ///   </summary>
        ///   <param   name= "txtoriginalpicturepath "> 原图片路径 </param>
        ///   <param   name= "txtsmallpicturepath "> 缩略图路径 </param>
        ///   <param   name= "intwidth "> 缩略图宽度 </param>
        ///   <param   name= "intheight "> 缩略图高度 </param>
        ///   <param   name= "strmode "> 缩略方式( 'h '--指定高,宽按比例, 'w '--指定宽,高按比例, 'hw '--按指定的宽高缩略) </param>
        ///   <param   name= "strsignetwordcontent "> 添加文字水印(文字内容) </param>
        ///   <param   name= "strsignetpicturepath "> 添加图片水印(图片路径) </param>
        public   void   makesmallpicture(string   txtoriginalpicturepath,   string   txtsmallpicturepath,   int   intwidth,   int   intheight,   string   strmode,   string   strsignetwordcontent,   string   strsignetpicturepath)
        {
                system.drawing.image   imgoriginalimage   =   system.drawing.image.fromfile(txtoriginalpicturepath);
                int   inttowidth   =   intwidth;
                int   inttoheight   =   intheight;

                int   x   =   0;
                int   y   =   0;
                int   intoriginalwidth   =   imgoriginalimage.width;
                int   intoriginalheight   =   imgoriginalimage.height;

                switch   (strmode)
                {
                        case   "hw "://指定高宽缩放(可能变形)                                
                                break;
                        case   "w "://指定宽,高按比例                                        
                                inttoheight   =   imgoriginalimage.height   *   intwidth   /   imgoriginalimage.width;
                                break;
                        case   "h "://指定高,宽按比例
                                inttowidth   =   imgoriginalimage.width   *   intheight   /   imgoriginalimage.height;
                                break;
                        case   "cut "://指定高宽裁减(不变形)                                
                                if   ((double)imgoriginalimage.width   /   (double)imgoriginalimage.height   >   (double)inttowidth   /   (double)inttoheight)
                                {
                                        intoriginalheight   =   imgoriginalimage.height;
                                        intoriginalwidth   =   imgoriginalimage.height   *   inttowidth   /   inttoheight;
                                        y   =   0;
                                        x   =   (imgoriginalimage.width   -   intoriginalwidth)   /   2;
                                }
                                else
                                {
                                        intoriginalwidth   =   imgoriginalimage.width;
                                        intoriginalheight   =   imgoriginalimage.width   *   intheight   /   inttowidth;
                                        x   =   0;
                                        y   =   (imgoriginalimage.height   -   intoriginalheight)   /   2;
                                }
                                break;
                        default:
                                break;
                }

                //新建一个bmp图片
                system.drawing.image   bitmap   =   new   system.drawing.bitmap(inttowidth,   inttoheight);

                //新建一个画板
                graphics   g   =   system.drawing.graphics.fromimage(bitmap);

                //设置高质量插值法
                g.interpolationmode   =   system.drawing.drawing2d.interpolationmode.high;

                //设置高质量,低速度呈现平滑程度
                g.smoothingmode   =   system.drawing.drawing2d.smoothingmode.highquality;

                //清空画布并以透明背景色填充
                g.clear(color.transparent);

                //在指定位置并且按指定大小绘制原图片的指定部分
                g.drawimage(imgoriginalimage,   new   rectangle(0,   0,   inttowidth,   inttoheight),   new   rectangle(x,   y,   intoriginalwidth,   intoriginalheight),   graphicsunit.pixel);

                //文字水印
                if   (!string.isnullorempty(strsignetwordcontent))
                {
                        system.drawing.font   f   =   new   font( "宋体 ",   14);
                        system.drawing.brush   b   =   new   solidbrush(color.white);
                        g.drawstring(strsignetwordcontent,   f,   b,   (convert.toint32(inttowidth   /   2)   -   20),   convert.toint32(intheight   /   2));
                }

                //图片水印  
                if   (!string.isnullorempty(strsignetpicturepath))
                {
                        system.drawing.image   copyimage   =   system.drawing.image.fromfile(strsignetpicturepath);
                        g.drawimage(copyimage,   new   rectangle(convert.toint32((inttowidth   -   copyimage.width)   /   2),   convert.toint32((inttoheight   -   copyimage.height)   /   2),   copyimage.width,   copyimage.height),   0,   0,   copyimage.width,   copyimage.height,   graphicsunit.pixel);
                        copyimage.dispose();
                }


                try
                {
                        //以jpg格式保存缩略图
                        bitmap.save(txtsmallpicturepath,   system.drawing.imaging.imageformat.jpeg);
                }
                catch   (system.exception   e)
                {
                        throw   e;
                }
                finally
                {
                        imgoriginalimage.dispose();
                        bitmap.dispose();
                        g.dispose();
                }
        }

        ///   <summary>
        ///   生成缩略图,添加文字/图片水印
        ///   </summary>
        ///   <param   name= "txtoriginalpicturepath "> 原图片路径 </param>
        ///   <param   name= "txtsmallpicturepath "> 缩略图路径 </param>
        ///   <param   name= "intwidth "> 缩略图宽度 </param>
        ///   <param   name= "intheight "> 缩略图高度 </param>
        ///   <param   name= "strmode "> 缩略方式( 'h '--指定高,宽按比例, 'w '--指定宽,高按比例, 'hw '--按指定的宽高缩略) </param>
        public   void   makesmallpicture(string   txtoriginalpicturepath,   string   txtsmallpicturepath,   int   intwidth,   int   intheight,   string   strmode)
        {
                makesmallpicture(txtoriginalpicturepath,   txtsmallpicturepath,   intwidth,   intheight,   strmode,   " ",   " ");
        }

        ///   <summary>
        ///   生成缩略图,添加文字/图片水印
        ///   </summary>
        ///   <param   name= "txtoriginalpicturepath "> 原图片路径 </param>
        ///   <param   name= "txtsmallpicturepath "> 缩略图路径 </param>
        ///   <param   name= "intwidth "> 缩略图宽度 </param>
        ///   <param   name= "intheight "> 缩略图高度 </param>
        public   void   makesmallpicture(string   txtoriginalpicturepath,   string   txtsmallpicturepath,   int   intwidth,   int   intheight)
        {
                makesmallpicture(txtoriginalpicturepath,   txtsmallpicturepath,   intwidth,   intheight,   "hw ",   " ",   " ");
        }

        #endregion
}
发表于:2007-03-06 09:20:454楼 得分:0
谢谢各位大哥帮助!
发表于:2007-03-13 16:07:385楼 得分:0
我的问题是:图片压缩的方式上船(图片压缩后,上传,然后在解压)


快速检索

最新资讯
热门点击