asp.net 上传图片生成缩略图效果

下面这个.net操作类是一款图片操作类,他的原理是很把图片上传到远程服务器,然后再根据用户设置生成小图或其它操作,有需要的朋友可以参考一下。

public class imghelp
    {
        //public static string gif = "image/gif";
        //public static string jpg = "image/pjpeg";
        //public static string png = "image/x-png";
        page pag = new page();
        string[] imgtype = { "image/gif", "image/pjpeg", "image/x-png" };
        public static string thisyear = datetime.now.year.tostring();
        public static string thismonth = datetime.now.month.tostring();
        public static string thisday = datetime.now.day.tostring();
        string strdate = thisyear + thismonth + thisday + datetime.now.hour.tostring() + datetime.now.second.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();

        /// <summary>
        /// 保存上传的文件返回true为保存成功massage为文件路径,返回false为保存失败massage错误信息
        /// </summary>
        /// <param name="producfile"></param>
        /// <param name="size">允许上传的最大值(以字节为单位)</param>
        /// <param name="addurl">存储路径</param>
        /// <param name="massage">存储信息</param>
        /// <returns></returns>
        public bool upimg(fileupload producfile, int size, string addurl, out string massage)
        {
            string mag;
            bool su = false;
            int i = throwimg(producfile, size, out mag);
            if (i == 1)
            {
                //新文件名
                string newfilename = strdate + producfile.filename.replace("~", "").replace("|", "").replace("&", "");
                string url = addurl;
                string pddurl = pag.server.mappath(url);
                try
                {
                    if (!directory.exists(pddurl))
                    {
                        directory.createdirectory(pddurl);
                    }                  
                    if (file.exists(pag.server.mappath(url + newfilename)) == false)
                    {
                        producfile.saveas(pag.server.mappath(url + newfilename));
                        massage = url + newfilename;
                        su = true;
                    }
                    else
                    {
                       massage = "上传文件名已经存在,请重新上传!";                          
                    }
                  
                }
                catch
                {
                    massage = "文件上传时程序出现异常,请重新上传!";                   
                }
            }
            else
            {
                massage = mag;
            }
            return su;

        }

        /// <summary>
        /// 上传图片关生成 缩略图
        /// </summary>
        /// <param name="producfile">控件</param>
        /// <param name="size">允许上传的最大值(以字节为单位)</param>
        /// <param name="addurl">存储路径</param>
        /// <param name="frontname">保存文件前缀名集合</param>
        /// <param name="imgwid">,宽度集合</param>
        /// <param name="imghid">高度集合</param>
        /// <param name="massage">返回上传信息</param>
        /// <returns></returns>
        public bool upimgandbreviary(fileupload producfile, int size, string addurl,string[] frontname,int[] imgwid,int[] imghid,out string massage)
        {
            string mag;
            bool key = false;
            int i = throwimg(producfile, size, out mag);
            if (i != 1)
            {
                massage = mag;
                key = false;

               
            }
            else if (frontname.length <= 0 || frontname.length != imgwid.length || frontname.length != imghid.length)
            {
                massage = "缩略图信息不相符";
                key = false;
            }
            else
            {
                //新文件名
                string newfilename = "primary_" + strdate + producfile.filename.replace("primary_", "");
                //存储路径
                string url = addurl + thisyear + "/" + thismonth + "/" + thisday;
                //转为服务器的绝对路径
                string pddurl = pag.server.mappath(url);

                try
                {
                    //判断路径目录是否存在
                    if (!directory.exists(pddurl))
                    {
                        //创建目录
                        directory.createdirectory(pddurl);
                        if (file.exists(pag.server.mappath(url + "/" + newfilename)) == false)
                        {
                            //保存文件
                            producfile.saveas(pag.server.mappath(url + "/" + newfilename));
                            //返回文件地址
                            massage = url + "/" + newfilename;

                            //生成缩略图
                            for (int j = 0; j < frontname.length; j++)
                            {
                                breviaryimg(massage, massage.replace("primary_", frontname[j]), imgwid[j], imghid[j]);
                            }
                            key = true;

                            //生成两张缩略图,保存名是在原图名前加了一个middle_,small_。
                            //newwidth1 = 200;
                            //newheight1 = 200;
                            //newwidth2 = 60;
                            //newheight2 = 60;
                            //breviaryimg(massage, massage.replace("primary_", "middle_"), newwidth1, newheight1);
                            //breviaryimg(massage, massage.replace("primary_", "small_"), newwidth2, newheight2);                      

                        }
                        else
                        {
                            massage = "上传文件重名,请重新上传!";
                            key = false;
                        }
                    }
                    else
                    {
                        if (file.exists(pag.server.mappath(url + "/" + newfilename)) == false)
                        {
                            //保存文件
                            producfile.saveas(pag.server.mappath(url + "/" + newfilename));
                            //返回保存地址
                            massage = url + "/" + newfilename;

                            //生成缩略图。
                            for (int j = 0; j < frontname.length; j++)
                            {
                                breviaryimg(massage, massage.replace("primary_", frontname[j]), imgwid[j], imghid[j]);
                            }
                            key = true;

                        }
                        else
                        {
                            massage = "上传文件名已经存在,请重新上传!";
                            key = false;
                        }
                    }
                }
                catch
                {
                    massage = "文件上传时出现程序错误,请重新上传!";
                    key = false;
                }

            }                             
         
            return key;

        }

        /// <summary>
        /// 检测上传文件
        /// </summary>
        /// <param name="producfile"></param>
        /// <param name="size">允许上传最大值</param>
        /// <param name="massage">检测信息</param>
        /// <returns></returns>   
        private int throwimg(fileupload producfile, int size, out string massage)
        {
            int i = 0;

            boolean imgok = false;
            if (producfile.hasfile)
            {
                //图片类型集
                string[] imgextension = { ".gif", ".jpg", ".jpeg", ".png" };
                //文件类型
                string filetype = producfile.postedfile.contenttype;
                //文件扩展名
                string fileextype = path.getextension(producfile.filename).tolower();
                //匹配类型
                for (int k = 0; k < imgextension.length; k++)
                {
                    if (fileextype == imgextension[k])
                    {
                        foreach (string type in imgtype)
                        {
                            if (filetype == type)
                            {
                                imgok = true;
                                break;
                            }
                        }
                    }
                }
                if (imgok == true)
                {
                    //获取文件大小
                    int filesize = producfile.postedfile.contentlength;
                    if (filesize < size)
                    {
                        i = 1;
                        massage = "";
                    }
                    else
                    {
                        massage = "上传文件过大!";
                        i = 0;
                    }
                }
                else
                {
                    massage = "上传的文件的格式不合法!";
                    i = 0;
                }
            }
            else
            {
                massage = "请选择正确的文件路径!";
                i = 0;
            }
            return i;

        }

        /// <summary>
        /// 生成缩缩略图
        /// </summary>
        /// <param name="imagepath">原图路径</param>
        /// <param name="savepath">缩略图存储路径</param>
        /// <param name="wid">缩略图宽</param>
        /// <param name="hid">缩略图高</param>
        private void breviaryimg(string imagepath, string savepath, int wid, int hid)
        {
            string originalimagepath = pag.server.mappath(imagepath);
            string thumbnailpath = pag.server.mappath(savepath);
            system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);
            //int towidth = wid;
            //int toheight = hid;

            int x = 0;
            int y = 0;
            int ow = originalimage.width;
            int oh = originalimage.height;
         
            //新建一个bmp图片
            system.drawing.image bitmap = new system.drawing.bitmap(wid, hid);

            //新建一个画板
            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(system.drawing.color.transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.drawimage(originalimage, new system.drawing.rectangle(0, 0, wid, hid), new system.drawing.rectangle(x, y, ow, oh), system.drawing.graphicsunit.pixel);

            try
            {

                //以原格式保存缩略图
                bitmap.save(thumbnailpath, originalimage.rawformat);
            }
            catch (system.exception e)
            {
                throw e;
            }
            finally
            {
                originalimage.dispose();
                bitmap.dispose();
                g.dispose();
            }
        }
    }

时间: 2024-09-20 08:54:46

asp.net 上传图片生成缩略图效果的相关文章

php上传图片生成缩略图(GD库)_php技巧

首先来一段简单的php上传图片生成缩略图的详细代码,分享给大家供大家参考,具体内容如下 <?php function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type

Asp无组件生成缩略图

网上有不少生成缩略图的ASP组件.若你的虚拟空间不支持注册新组件,可能会感觉自己的网站失色不少.心晴不才,结合网上资源写了个无组件生成缩略图程序,仅供参考. 还是先看看基础部分吧.首先,我们知道在页面中显示图片是如下代码: <img src="http://edu.cnzz.cn/NewsInfo/pic.gif" border="0" width="300" height="260"> src是图片路径,bord

如何实现asp无组件生成缩略图

  网上有不少生成缩略图的ASP组件.若你的虚拟空间不支持注册新组件,可能会感觉自己的网站失色不少.心晴不才,结合网上资源写了个无组件生成缩略图程序,仅供参考. 还是先看看基础部分吧.首先,我们知道在页面中显示图片是如下代码:     <img src="pic.gif" border="0" width="300" height="260"> src是图片路径,border控制图片边缘宽度,width是图片的长度

Asp无组件生成缩略图的代码_应用技巧

  还是先看看基础部分吧.首先,我们知道在页面中显示图片是如下代码: <img src="pic.gif" border="0" width="300" height="260"> src是图片路径,border控制图片边缘宽度,width是图片的长度,height是图片的高度.缩略图的生成其实就是在原始尺寸上缩放.但一般为了尽量少失真,我们都会按比例缩放.于是,获取图片的长宽尺寸也就成了生成缩略图的重点. 下面便

Asp无组件生成缩略图的代码

还是先看看基础部分吧.首先,我们知道在页面中显示图片是如下代码: <img src="pic.gif" border="0" width="300" height="260"> src是图片路径,border控制图片边缘宽度,width是图片的长度,height是图片的高度.缩略图的生成其实就是在原始尺寸上缩放.但一般为了尽量少失真,我们都会按比例缩放.于是,获取图片的长宽尺寸也就成了生成缩略图的重点. 下面便是编

Asp无组件生成缩略图(1)

缩略图|无组件 首先,我们知道在页面中显示图片是如下代码: <img src="http://www.163design.net/a/f/pic.gif" border="0" width="300" height="260"> src是图片路径,border控制图片边缘宽度,width是图片的长度,height是图片的高度.缩略图的生成其实就是在原始尺寸上缩放.但一般为了尽量少失真,我们都会按比例缩放.于是,获取

.net mvc中上传图片生成缩略图

        #region CreateThumbnail         /// <summary>         /// 生成缩略图         /// </summary>         /// <param name="uploadObject">上传的HttpPostedFile或图片物理路径</param>         /// <param name="uploaddir">上传

Asp无组件生成缩略图 (3)

缩略图|无组件 3.定义缩略图尺寸 这部分代码就是仁者见仁,智者见智了.首先,我们需要规定缩略图显示尺寸范围,譬如:300X260,代码可以这样写:<%Dim PXWidth,PXHeightDim Pp '//ProportionIf PWidth=0 Or PWidth="" ThenPXWidth=0PXHeight=0ElsePp=FormatNumber(PWidth/PHeight,2) '//长宽比End IfIf PWidth>=PHeight ThenIf

asp.net中生成缩略图并添加版权实例代码_实用技巧

复制代码 代码如下: //定义image类的对象Drawing.Image image,newimage;//图片路径protected string imagePath;//图片类型protected string imageType;//图片名称protected string imageName;//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true:否则返回 false