如何通过一个Url获得图片的大小

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    WebResponse response = request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    byte[] buffer = new byte[1024];

                    if (!response.ContentType.ToLower().StartsWith("text/"))
                    {
                        try
                        {
                            Stream outStream = new MemoryStream();
                            Stream inStream = response.GetResponseStream();

                            int bufferLength;
                            do
                            {
                                bufferLength = inStream.Read(buffer, 0, buffer.Length);
                                if (bufferLength > 0)
                                    outStream.Write(buffer, 0, bufferLength);
                            }
                            while (bufferLength > 0);

                            outStream.Flush();
                            outStream.Seek(0, SeekOrigin.Begin);
                            inStream.Close();

                            Image image = Image.FromStream(outStream);
                            outStream.Close();
                            MessageBox.Show("image   :=" + image.Width + "X" + image.Height);
                            image.Dispose();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("非图像格式\r\n" + ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("非图像格式");
                    }

posted @ 2014-04-10 17:57  小菜鸟飞飞  阅读(591)  评论(2编辑  收藏  举报