【发布时间】:2011-04-06 15:49:48
【问题描述】:
我正在尝试从该站点下载图像。当图像可用时,我使用的代码工作正常。如果图像不可用,则会产生问题。如何验证图像的可用性?
代码:
方法一:
WebRequest requestPic = WebRequest.Create(imageUrl);
WebResponse responsePic = requestPic.GetResponse();
Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error
webImage.Save("D:\\Images\\Book\\" + fileName + ".jpg");
方法二:
WebClient client = new WebClient();
Stream stream = client.OpenRead(imageUrl);
bitmap = new Bitmap(stream); // Error : Parameter is not valid.
stream.Flush();
stream.Close();
client.dispose();
if (bitmap != null)
{
bitmap.Save("D:\\Images\\" + fileName + ".jpg");
}
编辑:
Stream 有以下语句:
Length '((System.Net.ConnectStream)(str)).Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
Position '((System.Net.ConnectStream)(str)).Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
ReadTimeout 300000 int
WriteTimeout 300000 int
【问题讨论】:
-
用
try - catch包装违规语句,并向我们提供异常详细信息。 -
行 bitmap = new Bitmap(stream);显示错误:参数无效。