using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;

namespace ryControls
{
    /// <summary>
    /// 超级图片控件,支持加载工程内部图片、异步加载网络图片
    /// </summary>
    public partial class SuperPictureBox : PictureBox
    {
        /// <summary>
        /// 超级图片控件,支持加载工程内部图片、异步加载网络图片
        /// </summary>
        public SuperPictureBox()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 图片所在上级域名
        /// </summary>
        [Description("图片所在上级域名")]
        public string ImageDomain { get; set; }
        /// <summary>
        /// 图片资源所在上级文件夹
        /// </summary>
        [Description("图片资源所在上级文件夹")]
        public string ImageResFolder { get; set; }
        /// <summary>
        /// 根据不同前缀，加载不同格式图片。i:表示工程内置图片;s:表示T_ImageDomain域名下的图片,u:表示指定url的图片;f:表示本地图片
        /// </summary>
        /// <param name="sUrl"></param>
        public void LoadPicBySUrl(string sUrl)
        {
            string HeadStr = sUrl;
            if (HeadStr.Length > 2)
            {
                string keyStr = HeadStr.Substring(0, 2);
                if (keyStr == "i:")//内置
                {
                    this.Image = ButtonImages.ImageObject.GetResBitmap(Application.ExecutablePath, ImageResFolder + HeadStr.Substring(2));
                }
                else if (keyStr == "s:") //服务器上的地址
                {
                    LoadFromUrl(ImageDomain + HeadStr.Substring(2), true);
                }
                else if (keyStr == "u:") //URL地址
                {
                    LoadFromUrl(HeadStr.Substring(2), true);
                }
                else if (keyStr == "f:") //本地图片
                {
                    LoadFromFile(HeadStr.Substring(2));
                }
            }
        }
        private void LoadPic(string url)
        {
            Image image=null;
            try
            {
                WebRequest webreq = WebRequest.Create(url);
                WebResponse webres = webreq.GetResponse();
                Stream stream = webres.GetResponseStream();
                image = Image.FromStream(stream);
                stream.Close();
            }
            catch { }
            try
            {
                DateTime dt = DateTime.Now.AddSeconds(5);
                while(!this.IsHandleCreated)
                {
                    Thread.Sleep(10);
                    if (dt < DateTime.Now) { break; }
                }
                this.Invoke(new Action(() =>
                {
                    this.Image = image;
                }));
            }
            catch { }
        }
        /// <summary>
        /// 加载本地图片,加载完毕不会占用本地图片
        /// </summary>
        /// <param name="path"></param>
        public void LoadFromFile(string path)
        {
            this.Image = ryCommon.RyFiles.LoadPicFromFile(path);
        }
        /// <summary>
        /// 加载指定url图片
        /// </summary>
        /// <param name="url">url位置</param>
        /// <param name="isAsyn">是否异步</param>
        public void LoadFromUrl(string url, bool isAsyn)
        {
            if (isAsyn)
            {
                void starter() { LoadPic(url); }
                new Thread(starter).Start();
            }
            else
            {
                LoadPic(url);
            }
        }
    }
}
