Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

An API for Yahoo Image Search

4.86/5 (10 votes)
10 Jul 2011CPOL2 min read 1   1.8K  
Querying images from Yahoo! programmatically.
Screenshot - ImageGrabber

Introduction

In An API for Google Image Search, Ilan Assayag demonstrated how to programmatically query the Google image service. He also mentioned that the Yahoo! SDK enables image searches as well. In this article, I'll describe how to use .NET to access the Yahoo image service.

Background

Yahoo! Search Web Services enables developers to programmatically perform searches using the Yahoo search engine. The SDK currently includes support for Perl, Python and PHP, Java, JavaScript, and Flash. However, there is also a .NET developer center that includes "How to" articles and downloads. I've condensed this information into a .NET API that uses a familiar request - response pattern.

Using the Code

The solution file contains three projects:

  1. YahooImageSetup is a setup project that creates an installer for the sample "Yahoo Image Search" Windows forms application. Simply compile and run the setup application if you are only interested in searching for images.
  2. The ImageGrabber project contains the code for the sample Windows forms application.
  3. YahooAPI is a library project that contains the classes used to access the Yahoo image service.

Here's an example of basic use of the library:

C#
using Com.WickedByte.YahooAPI;

YahooImageRequest request = new YahooImageRequest();
request.ApplicationId = "asdf1234";
request.Query = "Star Trek";

YahooImageResponse response = request.GetResponse();

The ApplicationId is a required value that you get when you sign up with Yahoo! as a developer. It establishes the identity of the application that is calling the Yahoo! search service. The Query is the specific search string that will be processed. GetResponse() serializes the query as an HTTP GET, sends it to Yahoo!, and then deserializes the results.

There are a number of properties that can be set on the YahooImageRequest to modify the search results. I imagine that presence of the IsAdultOk property will make this application somewhat popular.

YahooImageResponse also contains a number of useful properties, the most important of which is the Images property. If you iterate over this property, each YahooImage element can be used to retrieve a System.Drawing.Image thumbnail or full-sized image. Once you retrieve an Image, it remains cached within the YahooImage object, so that calling GetImage() or GetThumbnailImage() repeatedly doesn't cause a Web download more than once.

C#
foreach( YahooImage result in response.Images )
{
   System.Drawing.Image thumb = result.GetThumbnailImage();
   System.Drawing.Image image = result.GetImage();
   //Do something with the downloaded images...
}

Points of Interest

I haven't covered most of the properties in the .NET wrapper API. You might want to explore the source code to see what's available to you. The sample GUI application demonstrates how to use many of the available properties. The GUI is also multi-threaded, so that you can double-click on a thumbnail result to retrieve the full-sized version while additional search results are still returning.

If you use this code to develop your own application, please apply for an application ID at the Yahoo! Search Web Services developer center and replace the value in the app.config file with your own. Have fun!

Marshall Rosenstein
WickedByte Software

History

  • 18th December, 2007: Initial version
  • 9th July, 2011: Project has been updated to VS 2010 and .NET 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)