我正在构建一个从互联网上检索图像的应用程序.即使它工作正常,但在应用程序中使用try-catch语句时速度很慢(在错误的给定URL上).
(1)这是验证URL和处理错误输入的最佳方法 - 还是应该使用正则表达式(或其他方法)?
(2)如果我没有在textBox中指定http://,为什么应用程序尝试在本地查找图像?
private void btnGetImage_Click(object sender, EventArgs e)
{
String url = tbxImageURL.Text;
byte[] imageData = new byte[1];
using (WebClient client = new WebClient())
{
try
{
imageData = client.DownloadData(url);
using (MemoryStream ms = new MemoryStream(imageData))
{
try
{
Image image = Image.FromStream(ms);
pbxUrlImage.Image = image;
}
catch (ArgumentException)
{
MessageBox.Show("Specified image URL had no match",
"Image Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
catch (ArgumentException)
{
MessageBox.Show("Image URL can not be an empty string",
"Empty Field", …Run Code Online (Sandbox Code Playgroud) 如何检查图像是否存在于C#/ ASP.NET 中的http://someurl/myimage.jpg似乎应该有一种方法来检查这个 - 但我找不到一个.
我找到了这个,但它并没有真正回答这个问题.