我想從網站下載圖像。我使用的代碼在圖像可用時工作正常。如果圖片不可用,則會造成問題。如何驗證圖像的可用性?從.NET/C中的網站下載圖像#
代碼:
方法1:
WebRequest requestPic = WebRequest.Create(imageUrl);
WebResponse responsePic = requestPic.GetResponse();
Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error
webImage.Save("D:\\Images\\Book\\" + fileName + ".jpg");
方法2:
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");
}
編輯:
流具有下面的語句:
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
包裹有問題的語句'嘗試 - catch',併爲我們提供了異常的詳細信息。 – gimel 2010-09-01 07:25:31
line bitmap = new Bitmap(stream);顯示錯誤:參數無效。 – Geeth 2010-09-01 09:26:37