private void ThreadShowImage(MyImage obj)
{
Thread t = new Thread(new ParameterizedThreadStart(ShowImage));
t.IsBackground = true;
try
{
t.Start(obj);
}
catch
{
return;
}
}
==============================================================
封装 图片对象类
public class MyImage
{
private PictureBox _pic; 图片空间
private string _url; 地址
private Bitmap _image; 图片
public MyImage(PictureBox p_pic,string p_url)
{
_pic = p_pic;
_url = p_url;
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public PictureBox Pic
{
get { return _pic; }
}
public Bitmap Image
{
get { return _image; }
set { _image = value; }
}
}
private void ShowImage(object obj)
{
MyImage image = obj as MyImage;
image.Pic.BackgroundImage = global::WinForm.Properties.Resources.loading; // 默认等待图片
try
{
System.Net.WebClient w = new System.Net.WebClient();
byte[] bytes = w.DownloadData(image.Url);
MemoryStream ms = new MemoryStream(bytes);
Image img = Image.FromStream(ms);
image.Pic.BackgroundImage = img;
}
catch
{
image.Pic.BackgroundImage = global::WinForm.Properties.Resources.smile_sad_48;
return;
}
}
在方法中调:
MyImage image= new MyImage(pic, "http://....."); pic为图片控件
ThreadShowImage( image) ;