try { System.Net.WebRequest request = System.Net.WebRequest.Create( "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif"); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); Bitmap bitmap2 = new Bitmap(responseStream); PictureBox1.Image = bitmap2; } catch(System.Net.WebException) { MessageBox.Show("There was an error opening the image file." + "Check the URL"); } }http://msdn.microsoft.com/zh-cn/library/z7ha67kw(v=VS.80).aspx
如果文件没有有效的图像格式,或者如果 GDI+ 不支持文件的像素格式,则此方法将引发 OutOfMemoryException 异常 检查字节长度 FileStream fs = new FileStream("d:\\a.gif", FileMode.Open,FileAccess.Read); long l = fs.Length; byte[] buffer = new byte[l]; int i = fs.Read(buffer, 0, (int)l); Bitmap bitmap = new Bitmap(new MemoryStream(buffer));
try
{
System.Net.WebRequest request =
System.Net.WebRequest.Create(
"http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream =
response.GetResponseStream();
Bitmap bitmap2 = new Bitmap(responseStream);
PictureBox1.Image = bitmap2; }
catch(System.Net.WebException)
{
MessageBox.Show("There was an error opening the image file."
+ "Check the URL");
}
}http://msdn.microsoft.com/zh-cn/library/z7ha67kw(v=VS.80).aspx
检查字节长度
FileStream fs = new FileStream("d:\\a.gif", FileMode.Open,FileAccess.Read);
long l = fs.Length;
byte[] buffer = new byte[l];
int i = fs.Read(buffer, 0, (int)l);
Bitmap bitmap = new Bitmap(new MemoryStream(buffer));
stream.Write(imageBytes, 0, imageBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
这样也不行的话,先确定imageBytes是否是正确的图片字节流