日期:2014-05-17 浏览次数:20540 次
Stream stream = null; StreamReader reader = null; MemoryStream ms = null; HttpWebResponse webResponse = null; try { WebRequest webRequest = HttpWebRequest.Create("http://image.sjq.cn/img/201002/99913/1213.jpg"); //打开图片地址 webResponse = (HttpWebResponse)webRequest.GetResponse(); stream = webResponse.GetResponseStream(); reader = new StreamReader(stream); string textContent = reader.ReadToEnd(); byte[] bytes = System.Text.Encoding.Default.GetBytes(textContent); ms = new MemoryStream(bytes); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); [color=#FF0000] ms参数无效[/color] //记录是否是图片个格式 bool imgState = false; if (img.RawFormat == ImageFormat.Gif) imgState = true; if (img.RawFormat == ImageFormat.Jpeg) imgState = true; if (img.RawFormat == ImageFormat.Png) imgState = true; if (imgState) { imgHeight = img.Height; imgWidth = img.Width; imgSize = int.Parse(img.Size.ToString()); md5 = getMd5Hash(textContent); if (ImagesManager.GetImgMarkIsExits(md5)) { message = "该图片已经存在!"; return false; } return true; } else { message = "该URL地址的目标不是图片"; return false; } } catch (Exception ex) { message = ex.Message; message = "图片上传操作异常"; return false; } finally { webResponse.Close(); stream.Dispose(); stream.Close(); reader.Dispose(); reader.Close(); ms.Dispose(); ms.Close(); }