Image Processing: Part 1

Paul Kinlan

I get so many things running through my head that I want to do. I was looking at the Yahoo search API and I found that you can search for
images. So I decided to do some tests to see what I can do with the results and how easy it would be in C# to load the images (it is
really easy, I was well chuffed when it all clicked into place)



string query = http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?;
query += “appid=[SOMEIDSHOULDGOHERE]&”;
query += “query=” + txtQuery.Text +”&”;
query += “results=5&”;
query += “start=1&”;
query += “format=any”;
imageList = newImageList();
WebRequestwr =WebRequest.Create(query);
using(WebResponse wResp = wr.GetResponse())
{

ImageSearchResponse.ResultSet imgResp = newImageSearchResponse.ResultSet();
using(Stream responseStream = wResp.GetResponseStream())
{
XmlSerializer serializer = newXmlSerializer(typeof(ImageSearchResponse.ResultSet));
imgResp = (ImageSearchResponse.ResultSet)serializer.Deserialize(responseStream);
}
//Add the images into
int imageIdx = 0;
foreach(ImageSearchResponse.ResultTypert in imgResp.Result)
{
WebRequestwrImage =WebRequest.Create(rt.Url);
using(WebResponse wRespImage = wrImage.GetResponse())
{
Stream sr = wRespImage.GetResponseStream();
System.Drawing.ImageinsertImage = Bitmap.FromStream(sr);
imageList.Images.Add(insertImage);
}
}
lstImages.Items.Add(rt.Title, imageIdx++);
}
lstImages.LargeImageList = imageList;

Basically the above code construct a REST Query to send to Yahoo, inside the REST Query are the parameters for the search and its results.
The WebRequest is created and the WebResonse is recieved. The Xml feed is then serialized into a custom datatype that is created from
Yahoo’s schema via the xsd.exe tool.


The custom data type is then iterated across, because each item in the ImageSearchResponse.Result is an image that matches our searcg
criteria.

A new query is then created to download this image from the site that Yahoo is pointing to. The response stream is returned and an image
created directly off this via Bitmap.FromStream(); The Bitmap is then loaded into an ImageList.

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium