//BasicInfo user = Commons.Helper.Login.GetCurrentUser();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset     = "utf-8";

            System.Drawing.Bitmap   bitmap   = null; //按截图区域生成Bitmap
            System.Drawing.Image    thumbImg = null; //被截图
            System.Drawing.Graphics gps      = null; //存绘图对象
            System.Drawing.Image    finalImg = null; //最终图片

            try
            {
                string pointX = context.Request.Params["pointX"]; //X坐标
                string pointY = context.Request.Params["pointY"]; //Y坐标
                string imgUrl = context.Request.Params["imgUrl"]; //被截图图片地址
                string rlSize = context.Request.Params["maxVal"]; //截图矩形的大小
                string iType  = context.Request.Params["iType"];  //0 习练者; 1 导师
                string Uid    = context.Request.Params["Uid"];    //用户Uid

                int finalWidth  = 300;
                int finalHeight = 300;

                if (!string.IsNullOrEmpty(pointX) && !string.IsNullOrEmpty(pointY) && !string.IsNullOrEmpty(imgUrl))
                {
                    string ext = System.IO.Path.GetExtension(imgUrl).ToLower();   //上传文件的后缀(小写)

                    bitmap = new System.Drawing.Bitmap(Convert.ToInt32(rlSize), Convert.ToInt32(rlSize));

                    thumbImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(imgUrl));

                    System.Drawing.Rectangle rl = new System.Drawing.Rectangle(Convert.ToInt32(pointX), Convert.ToInt32(pointY), Convert.ToInt32(rlSize), Convert.ToInt32(rlSize)); //得到截图矩形

                    gps = System.Drawing.Graphics.FromImage(bitmap);                                                                                                                //读到绘图对象

                    gps.DrawImage(thumbImg, 0, 0, rl, System.Drawing.GraphicsUnit.Pixel);

                    finalImg = PubClass.GetThumbNailImage(bitmap, finalWidth, finalHeight);

                    string finalPath = "/Files/avatar/original/" + Uid + "/" + DateTime.Now.ToFileTime() + ext;

                    finalImg.Save(HttpContext.Current.Server.MapPath(finalPath));
                    if (!string.IsNullOrEmpty(iType))
                    {
                        if (iType == "0")
                        {
                            ViewYogaUserDetail listModel = new ViewYogaUserDetail();
                            using (YogaUserDetailServiceClient clientModels = new YogaUserDetailServiceClient())
                            {
                                listModel = clientModels.GetYogaUserDetailById(Convert.ToInt32(Uid));

                                if (!string.IsNullOrEmpty(listModel.DisplayImg))
                                {
                                    string[] ids = listModel.DisplayImg.Split(';');
                                    if (ids.Count() > 1)
                                    {
                                        if (!string.IsNullOrEmpty(ids[1]))
                                        {
                                            PubClass.FileDel(HttpContext.Current.Server.MapPath("~" + ids[1]));
                                        }
                                        listModel.DisplayImg = ids[0] + ";" + finalPath;
                                    }
                                    else
                                    {
                                        listModel.DisplayImg = finalPath + ";";
                                    }
                                }
                                else
                                {
                                    listModel.DisplayImg = finalPath + ";";
                                }
                                clientModels.Update(listModel);
                            }
                        }
                        else if (iType == "1")
                        {
                            ViewYogisModels Model = new ViewYogisModels();
                            using (YogisModelsServiceClient client = new YogisModelsServiceClient())
                            {
                                Model = client.GetYogisModelsById(Convert.ToInt32(Uid));

                                if (!string.IsNullOrEmpty(Model.DisplayImg))
                                {
                                    string[] ids = Model.DisplayImg.Split(';');
                                    if (ids.Count() > 1)
                                    {
                                        if (!string.IsNullOrEmpty(ids[1]))
                                        {
                                            PubClass.FileDel(HttpContext.Current.Server.MapPath("~" + ids[1]));
                                        }
                                        Model.DisplayImg = ids[0] + ";" + finalPath;
                                    }
                                    else
                                    {
                                        Model.DisplayImg = finalPath + ";";
                                    }
                                }
                                else
                                {
                                    Model.DisplayImg = finalPath + ";";
                                }
                                client.Update(Model);
                            }
                        }
                    }
                    bitmap.Dispose();
                    thumbImg.Dispose();
                    gps.Dispose();
                    finalImg.Dispose();
                    GC.Collect();

                    //PubClass.FileDel(HttpContext.Current.Server.MapPath(imgUrl));

                    context.Response.Write(finalPath);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        //BasicInfo user = Commons.Helper.Login.GetCurrentUser();
        //[WebMethod(EnableSession = true)]
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset     = "utf-8";

            System.IO.Stream     stream      = null;
            System.Drawing.Image originalImg = null;   //原图
            System.Drawing.Image thumbImg    = null;   //缩放图


            try
            {
                int minWidth  = 100;                                      //最小宽度
                int minHeight = 100;                                      //最小高度
                int maxWidth  = 500;                                      //最大宽度
                int maxHeight = 500;                                      //最大高度

                string resultTip = string.Empty;                          //返回信息

                HttpPostedFile file  = context.Request.Files["Filedata"]; //上传文件
                string         uid   = @context.Request.Params["Uid"];
                string         iType = @context.Request.Params["UserType"];
                //string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]);  //得到上传路径
                string uploadPath    = HttpContext.Current.Server.MapPath("~/Files/avatar/original");
                string uploadPathUid = HttpContext.Current.Server.MapPath("~/Files/avatar/original/" + uid);
                string lastImgUrl    = @context.Request.Params["LastImgUrl"];

                if (!string.IsNullOrEmpty(lastImgUrl))
                {
                    // PubClass.FileDel(HttpContext.Current.Server.MapPath(lastImgUrl));
                }

                if (file != null)
                {
                    if (!System.IO.Directory.Exists(uploadPath))
                    {
                        System.IO.Directory.CreateDirectory(uploadPath);
                    }
                    if (!System.IO.Directory.Exists(uploadPathUid))
                    {
                        System.IO.Directory.CreateDirectory(uploadPathUid);
                    }
                    string ext = System.IO.Path.GetExtension(file.FileName).ToLower();   //上传文件的后缀(小写)

                    if (ext == ".jpg" || ext == ".png")
                    {
                        string flag = "ThumbNail" + DateTime.Now.ToFileTime() + ext;

                        string uploadFilePath = uploadPathUid + "\\" + flag;   //缩放图文件路径

                        stream = file.InputStream;

                        originalImg = System.Drawing.Image.FromStream(stream);

                        if (originalImg.Width > minWidth && originalImg.Height > minHeight)
                        {
                            thumbImg = PubClass.GetThumbNailImage(originalImg, maxWidth, maxHeight);  //按宽、高缩放

                            if (thumbImg.Width > minWidth && thumbImg.Height > minWidth)
                            {
                                thumbImg.Save(uploadFilePath);

                                resultTip = "/Files/avatar/original/" + uid + "/" + flag + "$" + thumbImg.Width + "$" + thumbImg.Height;

                                if (!string.IsNullOrEmpty(iType))
                                {
                                    if (iType == "0")
                                    {
                                        ViewYogaUserDetail listModel = new ViewYogaUserDetail();
                                        using (YogaUserDetailServiceClient clientModels = new YogaUserDetailServiceClient())
                                        {
                                            listModel = clientModels.GetYogaUserDetailById(Convert.ToInt32(uid));
                                            if (!string.IsNullOrEmpty(listModel.DisplayImg))
                                            {
                                                string[] ids = listModel.DisplayImg.Split(';');
                                                if (ids.Count() > 1)
                                                {
                                                    if (!string.IsNullOrEmpty(ids[0]))
                                                    {
                                                        PubClass.FileDel(HttpContext.Current.Server.MapPath("~" + ids[0].Split('$')[0]));
                                                    }
                                                    listModel.DisplayImg = resultTip + ";" + ids[1];
                                                }
                                                else
                                                {
                                                    listModel.DisplayImg = resultTip + ";";
                                                }
                                            }
                                            else
                                            {
                                                listModel.DisplayImg = resultTip + ";";
                                            }
                                            clientModels.Update(listModel);
                                        }
                                    }
                                    else if (iType == "1")
                                    {
                                        ViewYogisModels Model = new ViewYogisModels();
                                        using (YogisModelsServiceClient client = new YogisModelsServiceClient())
                                        {
                                            Model = client.GetYogisModelsById(Convert.ToInt32(uid));
                                            if (!string.IsNullOrEmpty(Model.DisplayImg))
                                            {
                                                string[] ids = Model.DisplayImg.Split(';');
                                                if (ids.Count() > 1)
                                                {
                                                    if (!string.IsNullOrEmpty(ids[0]))
                                                    {
                                                        PubClass.FileDel(HttpContext.Current.Server.MapPath("~" + ids[0].Split('$')[0]));
                                                    }
                                                    Model.DisplayImg = resultTip + ";" + ids[1];
                                                }
                                                else
                                                {
                                                    Model.DisplayImg = resultTip + ";";
                                                }
                                            }
                                            else
                                            {
                                                Model.DisplayImg = resultTip + ";";
                                            }

                                            client.Update(Model);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                resultTip = "图片比例不符合要求";
                            }
                        }
                        else
                        {
                            resultTip = "图片尺寸必须大于" + minWidth + "*" + minHeight;
                        }
                    }
                }
                else
                {
                    resultTip = "上传文件为空";
                }

                context.Response.Write(resultTip);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (originalImg != null)
                {
                    originalImg.Dispose();
                }

                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }

                if (thumbImg != null)
                {
                    thumbImg.Dispose();
                }

                GC.Collect();
            }
        }