码迷,mamicode.com
首页 > 其他好文 > 详细

图片压缩

时间:2015-05-26 17:50:21      阅读:108      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!