一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-06-25 04:17:20 编辑:袖梨 来源:一聚教程网

下面这个.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();

        ///


        /// 保存上传的文件返回true为保存成功massage为文件路径,返回false为保存失败massage错误信息
        ///

        ///
        /// 允许上传的最大值(以字节为单位)
        /// 存储路径
        /// 存储信息
        ///
        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;

        }


        ///


        /// 上传图片关生成 缩略图
        ///

        /// 控件
        /// 允许上传的最大值(以字节为单位)
        /// 存储路径
        /// 保存文件前缀名集合
        /// ,宽度集合
        /// 高度集合
        /// 返回上传信息
        ///
        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;

        }


        ///


        /// 检测上传文件
        ///

        ///
        /// 允许上传最大值
        /// 检测信息
        ///    
        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;

        }


        ///


        /// 生成缩缩略图
        ///

        /// 原图路径
        /// 缩略图存储路径
        /// 缩略图宽
        /// 缩略图高
        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();
            }
        }
    }

热门栏目