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:
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. - The
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:
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.
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!
Marshall Rosenstein
WickedByte Software
History
- 18th December, 2007: Initial version
- 9th July, 2011: Project has been updated to VS 2010 and .NET 4
Marshall's torrid relationship with programming started as a child using BASIC on a Commodore PET computer in the 70's. He continued programming through high school, but did not study Computer Science in college. At the time, compilers would fail without telling you why, so after much soul searching, he realized he didn't want to make a living by spending eight hours a day looking for a missing semi-colon.
By the time he was pursuing his Ph.D. in Communication and Marketing, Microsoft had released Visual Studio. The improvements in the IDE were enough to cause Marshall to have late night affairs with COM and ASP. Marshall spent the dotcom bubble years as a web developer. After the bubble burst, he worked independently as a Java developer for medical applications. When Microsoft released an early beta of the .NET Framework, he was convinced to switch his focus from the Java Platform to the new Framework. He spent some time at Philips Medical Systems writing the data-access layer for the Carevue Chart hospital system. He is currently Technical Director for ASE Technologies.
Marshall lives in Salem, Massachusetts but would rather be in Hawaii.