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.
Using the Code
The solution file contains three projects:
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.
ImageGrabber project contains the code for the sample Windows forms application.
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:
YahooImageRequest request = new YahooImageRequest();
request.ApplicationId = "asdf1234";
request.Query = "Star Trek";
YahooImageResponse response = request.GetResponse();
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
GetThumbnailImage() repeatedly doesn't cause a Web download more than once.
foreach( YahooImage result in response.Images )
System.Drawing.Image thumb = result.GetThumbnailImage();
System.Drawing.Image image = result.GetImage();
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!
- 18th December, 2007: Initial version
- 9th July, 2011: Project has been updated to VS 2010 and .NET 4