※一刀の专栏

☆风云有意,一刀无情☆---------朴实和谐的生存环境,与世无争的处世心态,以自然之道,养自然之身。...

上传图片并且生成可以控制大小图片清晰度的方法

上传图片并且生成可以控制大小图片清晰度的方法
private void Upload_Click(object sender, System.EventArgs e)
        {
            if(myFile.PostedFile !=null)
            {
                // 检查文件扩展名字
                HttpFileCollection files  = HttpContext.Current.Request.Files;
                HttpPostedFile postedFile = files[0];
                string fileName,fileExtension,file_id,file_path;
                //取出精确到毫秒的时间做文件的名称
                int year = System.DateTime.Now.Year;
                int month = System.DateTime.Now.Month;
                int day = System.DateTime.Now.Day;
                int hour = System.DateTime.Now.Hour;
                int minute = System.DateTime.Now.Minute;
                int second = System.DateTime.Now.Second;
                int millisecond = System.DateTime.Now.Millisecond;
                string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString();

                //获得文件类型
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                fileExtension = System.IO.Path.GetExtension(fileName);

                //重新命名文件,防止重复
                file_id = "topnews_"+my_file_id+fileExtension;
                file_path = "images/article_images/"+file_id;

                //文件上传到服务器的根目录
                postedFile.SaveAs(Request.PhysicalApplicationPath+@"images/article_images/"+ file_id);

                //处理图片大小
                int width,height,level;
                width=120;
                height=90;
                level=100;//从1-100
                GetThumbnailImage(width,height,level,file_id);
            }
        }

        //生成缩略图函数
        public void GetThumbnailImage(int width,int height,int level,string file_id)
        {
            string newfile= Request.PhysicalApplicationPath+"images/article_images/"+"top_"+ file_id;
            System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Request.PhysicalApplicationPath+"images/article_images/"+ file_id);
            System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            Bitmap output=new Bitmap(thumbnailImage);

            //处理JPG质量的函数
            ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici=null;
            foreach(ImageCodecInfo codec in codecs){if(codec.MimeType=="image/jpeg")ici=codec;}
            EncoderParameters ep=new EncoderParameters();
            ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
            output.Save(newfile,ici,ep);

            //释放所有使用对象
            ep.Dispose();
            output.Dispose();
            oldimage.Dispose();
            thumbnailImage.Dispose();

            //删除源图片
            string file_path = "images/article_images/"+"top_"+file_id;
            usageIO.DeletePath(Request.PhysicalApplicationPath+"images/article_images/"+ file_id);
            Response.Write("<script >parent.Form1.A_Simg.value ='"+file_path+"';location.replace('Upload_Img.aspx')</script>");
        }

        bool ThumbnailCallback()
        {
            return false;
        }

 

阅读更多
个人分类: 编程开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

上传图片并且生成可以控制大小图片清晰度的方法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭