zoukankan      html  css  js  c++  java
  • C# 发送HttpWebRequest获得网络图片的大小、尺寸

    可以通过HttpWebRequest的方式发送一个网络图片的请求,再通过HttpWebResponse接收返回的数据,分析数据流得到图片的大小以及尺寸,代码如下:

    /// <summary>  
            /// 获取图片的大小和尺寸  
            /// </summary>  
            /// <param name="aPhotoUrl">图片url</param>  
            /// <param name="aSize">图片大小</param>  
            /// <param name="aLength">图片尺寸</param>  
            private void GetPhotoInfo(string aPhotoUrl, ref string aSize, ref string aLength)  
            {  
                try  
                {  
                    Uri mUri = new Uri(aPhotoUrl);  
                    HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(mUri);                  
                    mRequest.Method = "GET";  
                    mRequest.Timeout = 200;  
                    mRequest.ContentType = "text/html;charset=utf-8";                   
                    HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();  
                    Stream mStream = mResponse.GetResponseStream();  
                    aSize = (mResponse.ContentLength / 1024).ToString() + "KB";  
                    Image mImage = Image.FromStream(mStream);  
                    aLength = mImage.Width.ToString() + "x" + mImage.Height.ToString();  
                    mStream.Close();  
                }  
                catch (Exception e)  
                {  
                    //MessageBox.Show(aPhotoUrl + "获取失败");  
                }  
            }  
    
  • 相关阅读:
    分零食「JSOI 2012」
    礼物「AHOI / HNOI2017」
    力「ZJOI2014」
    修改权值「多校联考2019」
    哪吒闹海「多校联考2019」
    消失之物
    灵异事件
    BZOJ1297: [SCOI2009]迷路
    BZOJ3445: [Usaco2014 Feb] Roadblock
    Luogu3953:逛公园
  • 原文地址:https://www.cnblogs.com/yangdunqin/p/HttpWebRequest.html
Copyright © 2011-2022 走看看