显示网络图片
想在打开程序的时候,在图片框中显示网站服务器中的图片(www.****.com/*.jpg)哪位大哥给写个代码
程序代码:string imgUrl = "http://***/456.jpg";//目标图片
WebRequest webReq = WebRequest.Create(imgUrl);
WebResponse webRes = webReq.GetResponse();
long fileLength = webRes.ContentLength;
try
{
Stream srm = webRes.GetResponseStream();
StreamReader srmReader = new StreamReader(srm);
byte[] bufferbyte = new byte[fileLength];
int allByte = (int)bufferbyte.Length;
int startByte = 0;
while (fileLength > 0)
{
Application.DoEvents();
int downByte = srm.Read(bufferbyte, startByte, allByte);
if (downByte == 0) { break; };
startByte += downByte;
allByte -= downByte;
}
string tempPath = Application.StartupPath + "//download.jpg";//临时文件名
//保存文件
FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bufferbyte, 0, bufferbyte.Length);
srm.Close();
srmReader.Close();
fs.Close();
//显示下载图片
this.pictureBox1.Image = Image.FromFile(tempPath);
}
catch (WebException ex)
{
MessageBox.Show("下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
