2 min read

Image Processing: Part 1

Paul Kinlan

Paul Kinlan

Lead of Chrome DevRel

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 forimages. 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 isreally 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 fromYahoo'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 searcgcriteria.

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 imagecreated directly off this via Bitmap.FromStream(); The Bitmap is then loaded into an ImageList.

Stay in the loop.

I'm trialing a newsletter. Join for monthly insights into web dev, Chrome, and the open web.

alternate_email

Get in touch

Open to chat about Chrome or Web development.

Book a consultation