asp.net載入新浪方式的圖片輪播

暖楓無敵發表於2013-05-29

               <div class="pic_news">
                        <div id="focusViwer" align="center">
                            <embed id="focusflash"  width="320" height="225" flashvars='<%=FULL %>'
                                wmode="opaque" menu="false" allowscriptaccess="sameDomain" quality="High" bgcolor="#F5F5F5"
                                name="focusflash" src="images/flash1.swf" type="application/x-shockwave-flash">
                        </div>
                        <script type="text/javascript" charset="gbk">
			<!--
                            var focus_width = 318
                            var focus_height = 218
                            var text_height = 25
                            var swf_height = focus_height + text_height
                            var pics = '<%=PICURL %>';
                            var links = '<%=LINKS %>';
                            links = links.replace(/.$/, "");
                            var texts = '<%=TEXT %>';
                            var FocusFlash = new sinaFlash("images/flash1", "focusflash", focus_width, swf_height, "7", "#F5F5F5", false, "High");
                            FocusFlash.addParam("allowScriptAccess", "sameDomain");
                            FocusFlash.addParam("menu", "false");
                            FocusFlash.addParam("wmode", "opaque");
                            FocusFlash.addVariable("pics", pics);
                            FocusFlash.addVariable("links", links);
                            FocusFlash.addVariable("texts", texts);
                            FocusFlash.addVariable("borderwidth", focus_width);
                            FocusFlash.addVariable("borderheight", focus_height);
                            FocusFlash.addVariable("textheight", text_height);
                            FocusFlash.write("focusViwer");
			//-->
                        </script>
                    </div>

OracleDM dm = new OracleDM();
    public string LINKS = ""; //超連結
    public string PICURL = ""; //圖片路徑
    public string TEXT = ""; //文字


    public string FULL = ""; //全路徑
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RpTzgg();//通知公告
            RpXwlb();//新聞列表
            RpQxsl();//區縣水利
            RpZtbd();//專題報導
            RpFxkh();//防汛抗旱
            RpSzjc();    //水政監察
            ShowIMG();//生成圖片報導


        }
    }

   //首頁圖片新聞
    public void ShowIMG()
    {
        string sqlstr = " select ID, TITLE , L_Content from (select * from t_leastclass where type='圖片報導' ) where rownum<=5";
        DataTable dtP = dm.getsql(sqlstr).Tables[0];
        string strTitle = "";//拼接標題
        string picUrl = ""; //圖片標題
        string link = ""; //超連結
        for (int i = 0; i < dtP.Rows.Count; i++)
        {
            strTitle += dtP.Rows[i]["TITLE"].ToString().Length > 20 ? dtP.Rows[i]["TITLE"].ToString().Substring(0, 20) + "..." : dtP.Rows[i]["Title"].ToString() + "|";
            picUrl += getImageUrl(dtP.Rows[i]["L_CONTENT"].ToString()) + "|";
            link += "lashgc.aspx?id=" + dtP.Rows[i]["ID"].ToString() + "|";
        }
        strTitle = strTitle.TrimEnd('|');
        picUrl = picUrl.TrimEnd('|');
        link = link.TrimEnd('|');


        LINKS = link;
        PICURL = picUrl;
        TEXT = strTitle;
        FULL = "pics=" + PICURL + "&links=" + LINKS + "&texts=" + TEXT + "&borderwidth=320&borderheight=198&textheight=26";
    }

 /// <summary>
    /// 從字串中獲取需要的圖片地址
    /// </summary>
    /// <param name="source">字串</param>
    /// <returns>需要的圖片地址</returns>
    public string getImageUrl(string source)
    {
        ArrayList list = new ArrayList();
        string result = "";
        string pattern = "<img[^<>]*?\\ssrc=['\"]?(.*?)['\"].*?>";
        Regex reg = new Regex(pattern);
        MatchCollection mc = reg.Matches(source);
        foreach (Match m in mc)
        {
            list.Add(m.Groups[1].Value);
        }
        if (list.Count > 0)
        {
            string s = list[0].ToString().TrimStart('/');
            result = s.Substring(s.IndexOf('/') + 1);
        }
        else
        {
            result = "images/noPic.jpg";
        }
        return result;
    }


    #endregion


重點是:利用正規表示式取字串中的圖片路徑


<img[^<>]*?\\ssrc=['\"]?(.*?)['\"].*?>

相關文章