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;

public partial class admin_Image_upload : Fm.Bussiness.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    DataTable GetXml()
    {
        //实例化XML文件操作类
       Fm.Bussiness. Xml_work xml = new Fm.Bussiness.Xml_work();
        //获取XML配置文件路径
        string path = Server.MapPath("../config/art_config.xml");
        //调用读取类
        DataTable dt = xml.Xml_Reader(path, "art_set");
        return dt;

    }
    protected void upload_Click(object sender, EventArgs e)
    {
        DataTable dt = GetXml();
        //获取设置的有效图片格式
        string[] type = dt.Rows[0]["img_type"].ToString().Split('|');
        //获取扩展名
        string kzm = imgupload.FileName.Substring(imgupload.FileName.LastIndexOf("."), imgupload.FileName.Length - imgupload.FileName.LastIndexOf("."));
        
        //循环判断上传的图片格式是否有效
        bool bo = false;
        for (int i = 0; i < type.Length; i++)
        {
            if (kzm =="."+type[i].ToString())
            {
                bo = true;
            }
        }

        //判断上传图片是不是符合设置
        if (imgupload.PostedFile.ContentLength / 1024 > int.Parse(dt.Rows[0]["up_size"].ToString()))
        {
            Response.Write("<script>alert('请选择小于 " + dt.Rows[0]["up_size"].ToString() + "KB 的图片');</script>");
        }
        else if (bo == false)
        {
            Response.Write("<script>alert('请选择有效的图片格式');</script>");
        }
        else
        {
            string temp = Server.MapPath("../");
            string path = "upload/";
            //上传图片前创建新文件夹
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            if (month.Length == 1)
            {
                month = "0" + month;
            }
            string day = DateTime.Now.Day.ToString();
            if (day.Length == 1)
            {
                day = "0" + day;
            }
            path = path + "images/" + year + "/" + month + "/" + day;
            ATL.IO.IOStream stream = new ATL.IO.IOStream();
            if (!System.IO.File.Exists(path))
            {
                stream.CreateFolder(temp+path);
            }
            //上传所选择的图片
            string ph = path + "/" + year + month + day + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second + imgupload.FileName;
            imgupload.SaveAs(temp+ph);
            //图片加水印
            if (dt.Rows[0]["tr_fs"].ToString() == "true")
            {
                //实例化图片操作类
                ATL.Picture.Images image = new ATL.Picture.Images();
                if (dt.Rows[0]["wz_tp"].ToString() == "文字")
                {
                    System.Drawing.Color color = System.Drawing.Color.FromName(dt.Rows[0]["color"].ToString());
                    image.LetterWatermark(temp + ph, int.Parse(dt.Rows[0]["fontsize"].ToString()), dt.Rows[0]["sy_wenzi"].ToString(), color, dt.Rows[0]["weizhi"].ToString());
                }
                else
                {
                    string waterpath = Server.MapPath(".." + dt.Rows[0]["img_path"].ToString());
                    image.ImageWatermark(temp + ph, waterpath, dt.Rows[0]["weizhi"].ToString());
                }
            }
            //预览
            imglook.Src = "/" + ph;
            imgpath.Value = "/" + ph;
        }
    }
}
