using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Collections.Generic;
using JuSNS.Common;
using JuSNS.Config;
using System.IO;
using System.Net;
using JuSNS.Common.PicUpload;
using System.Drawing.Imaging;

namespace JuSNS.Web.home.space
{
    public partial class UserHeadPhoto1 : JuSNS.UI.Page.UserPage
    {
        string filename = string.Empty;
        int size = 4;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.Expires = 0;
            this.yulan1.InnerHtml = "<img src=\"" + GetHeadImage(this.UserID, 2) + "\" style=\"width:100px;\" />";
            if (string.IsNullOrEmpty(this.GetHeadPath(this.UserID)))
            {
                Response.Redirect("userheadphoto.aspx");
            }
            filename = this.GetHeadPath(this.UserID);
            try
            {
                filename = Session["headpic"].ToString();
            }
            catch { }
            if (Page.IsPostBack)
            {
                action();
            }
            else
            {
                this.controlImg.Src = Public.GetSmallHeadPic(filename, size);
            }
        }

        protected void action()
        {
            string xx = Request.QueryString["x"];
            string yy = Request.QueryString["y"];
            string ww = Request.QueryString["w"];
            int x = 0;
            int y = 0;
            int w = 150;
            int h = 150;
            if (!string.IsNullOrEmpty(xx))
            {
                x = int.Parse(xx);

            }
            if (!string.IsNullOrEmpty(yy))
            {
                y = int.Parse(yy);
            }
            if (!string.IsNullOrEmpty(ww))
            {
                w = int.Parse(ww);
                h = w;
            }
            makeRectImage(filename, x, y, w, h); //生成剪切头像
            Response.Redirect("UserHeadPhoto.aspx?sucmsg=" + Server.UrlEncode("成功上传头像。可能要几分钟后才能生效。"));
        }
        /// <summary>
        /// 生成剪切头像图片
        /// </summary>
        /// <param name="im"></param>
        /// <param name="filename"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        protected void makeRectImage(string filename, int x, int y, int w, int h)
        {
            Dictionary<string, PicConfigInfo> pci;
            pci = PicConfig.Portrait;
            string filepath = Public.GetSmallHeadPic(filename, size);
            string smallPath = Public.GetSmallHeadPic(filename, 1);
            if (string.IsNullOrEmpty(PicConfig.ProtraitServer) || PicConfig.ProtraitServer != "~")
            {
                //剪切for远程
            }
            else
            {
                //本地剪切
                Bitmap bmp = new Bitmap(Server.MapPath(filepath));
                MemoryStream ms = new MemoryStream();
                ImageCodecInfo myImageCodecInfo;
                System.Drawing.Imaging.Encoder myEncoder;
                EncoderParameter myEncoderParameter;
                EncoderParameters myEncoderParameters;
                myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
                myEncoder = System.Drawing.Imaging.Encoder.Quality;
                myEncoderParameters = new EncoderParameters(1);
                myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                myEncoderParameters.Param[0] = myEncoderParameter;
                bmp.Save(ms, myImageCodecInfo, myEncoderParameters);
                bmp.Dispose();
                byte[] data1;
                PicUpload.makeRectImage(ms.ToArray(), out data1, x, y, w, h);
                System.Drawing.Image image = Bitmap.FromStream(new MemoryStream(data1));
                Thumbnail thb = new Thumbnail(image);
                thb.NewPath = Server.MapPath(smallPath);
                thb.Width = pci["1"].X;
                thb.Height = pci["1"].Y;
                thb.Make();
                image.Dispose();
                ms.Dispose();
            }
        }
    }
}
