65.9K
CodeProject 正在变化。 阅读更多。
Home

Tickr:创建您自己的 Flickr 幻灯片

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.75/5 (5投票s)

2007年9月6日

CPOL

2分钟阅读

viewsIcon

32342

downloadIcon

384

创建您自己的 Flickr 幻灯片。

Screenshot - tickr.jpg

引言

Flickr 是一个非常流行的 Web 2.0 社交图片分享网站。每张图片都有描述和一定数量的标签。搜索特定标签并查看显示的图片很有趣。Tickr for Mac OS X 是第一个基于给定关键词创建幻灯片的软件。
在 Tickr 之后不久,出现了 Slickr,一个 Windows 客户端,功能相同。还有一个 OpenGL 屏幕保护程序 和一个 AJAX 插件,适用于 Wordpress,也名为 Slickr。

现在我想向初级 Web 2.0 开发者或混合应用开发者展示使用 .NET 创建此类应用程序是多么容易。我们省略了 Tickr 和 Slickr 的桌面滑动功能,从而创建一个简单且可重用的代码片段。

Using the Code

我们在表单上创建一个 TextBox, Button, PictureBox 和一个 StatusStrip。然后我们初始化一个线程和一个委托作为表单的成员变量。委托用于在线程中安全地更新 picturebox

//members
private Thread workerThread = null;
private delegate void SetPictureHandler(string URL);

当点击按钮时,我们轻轻地启动线程。
如果它已经在运行,我们会中断它并启动一个新的。

private void searchButton_Click(object sender, EventArgs e)
{
    if (workerThread != null)
        {    
           workerThread.Interrupt();
           workerThread = null;
        }

        workerThread = new Thread(new ThreadStart(DoWork));
        workerThread.Start();
}

真正的工作是在线程的函数中完成的。我们使用 HttpWebRequest 获取 flickr 网页,其中包含文本框中给定的关键词。HttpWebResponse 为我们提供服务器响应。我们将响应存储在变量 html 中,并使用正则表达式来过滤掉我们需要的内容。

我们使用正则表达式模式 <img(.*)/> 过滤掉所有图像。现在的问题是,我们也会得到 buddyicon、主要的 flickr 徽标和其他无用的图像。我们可以通过检查图像的当前 URL 是否包含 farm(Flickr 存储图像的服务器)并且不包含 buddyicons(所有 buddy 图标都包含此字符串)来过滤掉可用的图像。

我们使用 GetImage 函数分别获取每张图像,该函数实际上再次使用 HttpWebRequestHttpWebResponse 从数据流创建图像。该图像使用 SetImage 函数进行线程安全设置。

我们还提取图像 URL 的 alt 标签,因为这为我们提供了当前图像的描述。然后每张图像显示 3 秒钟,然后提取下一张图像。

private void DoWork()
{
  try
  {    
     SetStatus("Fetching images...");
     HttpWebRequest hwreq = (HttpWebRequest)WebRequest.Create
		("http://www.flickr.com/search/?q=" + txtSearch.Text);
     HttpWebResponse hwres = (HttpWebResponse)hwreq.GetResponse();                

     StreamReader sr = new StreamReader(hwres.GetResponseStream());
     String html = sr.ReadToEnd();

     String pattern = @"<img(.*)/>";
     MatchCollection matches = Regex.Matches(html, pattern, 
		RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | 
		RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);

     foreach (Match match in matches)
     {
       String result = match.ToString();

       if (result.Contains("farm") && !result.Contains("buddyicons"))
       {
          String URLPattern = @"http://(.*)jpg";
          MatchCollection URLMatches = Regex.Matches(result, URLPattern, 
		RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | 
		RegexOptions.IgnorePatternWhitespace);                        

          foreach (Match URLMatch in URLMatches)
          {
            SetImage(URLMatch.ToString());

            String AltPattern = @"alt=(.*)/";
            MatchCollection AltMatches = Regex.Matches(result, AltPattern, 
		RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | 
		RegexOptions.IgnorePatternWhitespace);

            foreach (Match AltMatch in AltMatches)
            {
               SetStatus(AltMatch.ToString().Replace("alt=", "").Replace
						("\"", "").Replace("/", ""));
            }

            Thread.Sleep(3000);
          }                        
       }
     }

     hwreq = null;
     sr.Close();
     hwres.Close();
  }
  catch (Exception ex)
  {
  }
} 

GetImage 例程的代码

private Image GetImage(string sURL)
{
  Stream str = null;
  HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(sURL);
  HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
  str = wRes.GetResponseStream();

  return Image.FromStream(str);
} 

线程安全函数 SetImage 的代码如下

private void SetImage(string URL)
{
  if (this.pictureBox1.InvokeRequired)
  {
     SetPictureHandler d = new SetPictureHandler(SetImage);
     pictureBox1.Invoke(d, new object[] { URL });
  }
  else
  {
     pictureBox1.Image = GetImage(URL);
  }
} 

toolStripStatusLabel 可以直接设置

private void SetStatus(string text)
{
  this.toolStripStatusLabel1.Text = text;         
}

历史

  • 2007年6月9日:版本 1.0,初稿

关于 KristofLeroux

Kristof Leroux 是信息学硕士,专业方向为人工智能。
他拥有自己的软件开发公司 Laranea,该公司专门从事使用游戏 AI、神经网络、遗传算法和机器学习的软件开发。另一个重要的分支是移动开发。目前,他在 Web 2.0 领域非常活跃,并且正在研究人工智能的创造力。

© . All rights reserved.