让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 { }
}
}
}
}
让PictureBox支持URL显示图片的更多相关文章
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...
- winform下picturebox控件显示图片问题
viewData_pictureBox.SizeMode=PictureBoxSizeMode.StretchImage;图片会自动按照比例缩放来完全显示在你的PictureBox中.
- 一行实现QQ群组头像,微信群组,圆角等效果. 并支持url直接加载图片
说点题外话. Coding中我们总是经历着这么几个过程. 学会使用: 不管是API也好, 开源库也好. 总是在最开始的学会去用. 了解实现原理: 可能会因为一些不兼容, 代码的异常状态的处理不够完美等 ...
- EmguCV控件Emgu.CV.UI.ImageBox及C# picturebox显示图片连续刷新出现闪烁问题
在上一篇里,EmguCV(OpenCV)实现高效显示汉字及叠加 实现了视频叠加及显示,但存在问题,就是 Emgu.CV.UI.ImageBox及C# picturebox显示图片时都会出现闪烁,尤其 ...
- Web前端学习(4):显示图片、url与文件路径
本章主旨 介绍<img>标签及其基本属性:介绍URL和文件路径 在上一章中,我简单地介绍了HTML的一些基本标签及基本属性,例如,我们用<p>标签来标记文本段落,用<h1 ...
- 重写TreeView,多层级节点下批量显示图片,图片支持缩略图和文件名列表切换,支持调用者动态匹配选中,支持外界拖入图片并添加到对应节点下
1.先看下整体效果 2.前端代码 <UserControl x:Class="iPIS.UI.Base.Tree.ImageTreeControl" xmlns=" ...
- 如何在启用SharePoint浏览器功能的InfoPath 表单中添加托管代码以动态地加载并显示图片
InfoPath 的浏览器表单不支持加载并显示图片,当然在模板中可以插入图片,但是如果想显示数据库的一幅图片,或是动态加载一张图片就无能为力了. 基实这个问题可以通过在浏览器表单中使用: " ...
- 我写的一个 Qt 显示图片的控件
Qt 中没有专门显示图片的控件.通常我们会使用QLabel来显示图片.可是QLabel 显示图片的能力还是有点弱.比方不支持图像的缩放一类的功能.使用起来不是非常方便. 因此我就自己写了个简单的类. ...
- ie8浏览器 图片本身问题导致 无法显示图片--- 诡异现象的排查分享
引子: 前段时间 做新版2.0 首页 的时候, 总感觉 新版首页 线上 精彩回顾下的 2张图片颜色怪怪的,当时以为是图片压缩太厉害导致的,由于实在太忙就没太在意!以下 是来自线上 截图: 红色方 ...
随机推荐
- vim - Removing duplicate lines
http://vim.wikia.com/wiki/Uniq_-_Removing_duplicate_lines :sort u
- java字典序全排列
import java.util.Arrays; /** *字典序全排列 *字符串的全排列 *比如单词"too" 它的全排列是"oot","oto&q ...
- 跟着思维导图学习javascript
1.javascript 变量 2.javascript 运算符 3.javascript 数组 4.javascript 流程语句 5.javascript字符串函数 6.javascript 函数 ...
- VSO-Branch和Merge
分支策略 一般采用一个MAIN分支,一个或多个DEVELOPMENT分支的分支结构,如下图(来自:https://msdn.microsoft.com/en-us/library/ee782536.a ...
- python os 命令,及判断文件夹是否存在
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- C语言堆和栈
堆和栈的区别 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.2.堆区(heap ...
- NoSql 中Mongodb数据库的使用
1.NoSql数据库简介 2.MongoDB数据库的简介 3.MongoDB下Windows下的安装
- iOS第三方库管理工具
作者:彷徨iOS 原文地址1:http://iostree.sinaapp.com/?p=78 原文地址2:http://blog.csdn.net/wzzvictory/article/detail ...
- [Ubuntu] change mouse scrolling between standard and natural
Standard: sudo vi .Xmodmap insert the content as below pointer = Natural: sudo vi .Xmodmap insert th ...
- WebSQL 查询工具
最近在写 WebSQL ,每次都在浏览器控制台执行 SQL 太费劲了,并且脑子不好使,总是忘记上次初始化的数据库是什么,所以写了一个特别简单的 WebSQL 可视化工具,说工具有点大了,就是为了方便, ...