关闭

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

1237人阅读 评论(0) 收藏 举报
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;
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1048054次
    • 积分:13537
    • 等级:
    • 排名:第943名
    • 原创:210篇
    • 转载:312篇
    • 译文:0篇
    • 评论:278条
    最新评论