• 让PictureBox支持URL显示图片


      [ToolboxItem(true)]
        public class PictureBoxURL : PictureBox
        {
            private string _url = "";
            public string ImageUrl
            {
                get
                {
                    return _url;
                }
                set
                {
                    _url = value;
                    if (String.IsNullOrEmpty(_url)) return;
                    GetImageByWebRequest(this, _url);
                }
            }
    
            public Image ImageLoading { get; set; }
    
            /// <summary>
            /// 推荐使用ByWebRequest
            /// </summary>
            /// <param name="edit"></param>
            /// <param name="url"></param>
            private void GetImageByWebClient(PictureBoxURL edit, string url)
            {
                WebClientImage tt = new WebClientImage(edit, url);
                Thread thread = new Thread(new ThreadStart(tt.Do));
                thread.Start();
    
            }
            private void GetImageByWebRequest(PictureBoxURL edit, string url)
            {
                WebRequestImage tt = new WebRequestImage(edit, url);
                Thread thread = new Thread(new ThreadStart(tt.Do));
                thread.Start();
            }
    
            public class WebClientImage
            {
                private PictureBoxURL edit;
                private string url;
    
                public WebClientImage(PictureBoxURL a, string b)
                {
                    edit = a;
                    url = b;
                }
    
                public void Do()
                {
                    try
                    {
                        edit.BeginInvoke(new Action(() =>
                        {
                            edit.Image = edit.ImageLoading;
                        }));
    
                        using (var client = new System.Net.WebClient())
                        {
                            using (var strream = client.OpenRead(url))
                            {
    
                                Image Image = new Bitmap(strream);
                                edit.BeginInvoke(new Action(() =>
                                {
                                    edit.Image = Image;
                                }));
                            }
                        }
                    }
                    catch
                    {
                        edit.BeginInvoke(new Action(() =>
                        {
                            edit.Image = null;
                        }));
                    }
                }
            }
            private class WebRequestImage
            {
                private PictureBoxURL edit;
                private string url;
    
                public WebRequestImage(PictureBoxURL a, string b)
                {
                    edit = a;
                    url = b;
                }
                public void Do()
                {
                    try
                    {
                        if (edit.IsHandleCreated)
                        {
                            edit.BeginInvoke(new Action(() =>
                            {
                                edit.Image = edit.ImageLoading;
                            }));
                        }
                        else
                            edit.Image = edit.ImageLoading;
    
    
                        var request = (HttpWebRequest)WebRequest.Create(url);
    
                        using (var response = (HttpWebResponse)request.GetResponse())
                        {
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                using (var stream = response.GetResponseStream())
                                {
                                    Image Image = new Bitmap(stream);// Bitmap.FromStream(stream);
                                    edit.BeginInvoke(new Action(() =>
                                    {
                                        edit.Image = Image;
                                    }));
    
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            edit.BeginInvoke(new Action(() =>
                            {
                                edit.Image = null;
                            }));
                        }
                        catch { }
                    }
                }
            }
        }
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    字符串与字典常用命令
    Python学习之路:字符串常用操作
    Python学习之路:购物车实例
    面试题2017
    c#语法学习
    结构化设计模式-桥接模式
    结构型设计模式-适配器模式
    .Net Cache
    设计模式的六大原则
    uml类图关系
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5972234.html
Copyright © 2020-2023  润新知