首页 > 其他 > 详细

图片压缩

时间:2015-05-26 17:47:39      阅读:166      评论:0      收藏:0      [点我收藏+]

public string UploadPhotoUrl(HttpPostedFileBase uploadFile)
        {
            MessageResult result = new MessageResult();
            string domain = this.TheVisitorInfo.DomainUniqueName;
            string userLoginName = this.TheVisitorInfo.LoginName;
            Guid g = this.TheVisitorInfo.Guid;
            result.Message = MyStringHelper.default_domain_logo_url;
            
            if (string.IsNullOrEmpty(domain))
            {
                return @"<script>alert(‘域唯一名不能为空!‘)</script>";
            }
            if (string.IsNullOrEmpty(userLoginName))
            {
                return @"<script>alert(‘用户名不能为空!‘)</script>";
            }
            if (uploadFile != null)
            {

                //存入文件
                if (uploadFile.ContentLength > 0)
                {
                    using (BinaryReader r = new BinaryReader(uploadFile.InputStream))
                    {
                        try
                        {
                            if (!MyFileTypeChecker.CheckExtension(r, new string[] { "png", "jpg", "gif", "bmp" }))
                            {
                                r.Close();
                                uploadFile.InputStream.Close();
                                return @"<script>alert(‘您选择上传的文件被系统认定为非法文件!请重新选择文件!‘)</script>";
                            }
                            string lastPre = "jpg";
                            string newFileName = ChangeName(uploadFile.FileName, g.ToString(), lastPre);

                            //保存文件
                            string filepath= Server.MapPath("~/content/default/images/") + "PhotoTest" + newFileName;
                            uploadFile.SaveAs(filepath);

                            //保存数据库
                            result.Message = "/content/default/images/" + "Photo" + newFileName;
                            siteProfileBo.UpdatePhotoUrl(domain, result.Message, userLoginName);

                            //截取缩略图
                            var img = Image.FromFile(filepath);
                            Image thumbnail = img.GetThumbnailImage(180, 120, null, IntPtr.Zero);
                            thumbnail.Save(Server.MapPath(result.Message));
                            thumbnail.Dispose();
                            img.Dispose();
                            if (System.IO.File.Exists(filepath))
                            {
                                //如果存在则删除
                                System.IO.File.Delete(filepath);
                            }
                        }
                        catch (Exception e)
                        {

                        }
                        finally
                        {
                            r.Close();
                            uploadFile.InputStream.Close();
                        }
                    }
                }
            }
            return "<script>window.parent.getUrlCallBack(\‘" + result.Message + "\‘)</script>";
        }

图片压缩

原文:http://www.cnblogs.com/zhtbk/p/4530741.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!