Click here to Skip to main content
13,596,556 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 25 Sep 2011

Google Image Search Client in C# 5.0 and WPF

Rate this:
Please Sign up or sign in to vote.
A simple 50 lines of C# 5.0 code which showcases the await and async features in C# 5.0.




This article mainly focuses on demonstrating how the async and await keywords combined with lambda expressions in C# 5.0 are going to minimize the lines of code and how efficient they are.

Before going further, let me list the prerequisites.

  1. The given code works only with .NET 4.5.

  2. This solution file was built in Visual Studio 2011 Developer View version.

  3. I used Google Image API for getting image URLs of the search keyword.
  4. Check for proxy and internet settings since the code uses internet to get the data.

Hence get all the things in place before trying out the code :)


For reference, remember that the await keyword is used to mark asynchronous calls so that you don’t need to create callback functions anymore and can write code in the same way as if it were synchronous. The compiler will do all of the heavy lifting for you. The async keyword must be presented in the signature of the method that makes the asynchronous calls. Briefly, you can use the await keyword only if the async keyword is in the method signature. The same is true for lambda expressions.

In the below example, I am going to show all the above said possibilities.

Using the Code

The UI code in XAML is quite simple and very much self explanatory. So let us go for the core logic.

The core logic of the program rests with the search button click where in the following tasks are performed:

Task 1: Initialize GimageSearchClient and begin search.

Task 2: Pass four parameters to beginsearch such that the first among them is the search keyword to get images, the second one is the number of results to display, the third and very important one is the lambda expression which returns the results of GimageSearchClient and assigns the same to wrapPanel through a dispatcher. The final param is the AsyncState which is any way null.

 private void btnSubmit_Click(object sender, RoutedEventArgs e)
    GimageSearchClient client = new GimageSearchClient("");
    IList<IImageResult> results ;
     IAsyncResult result= client.BeginSearch(
        textSearch.Text.Trim(),  //param1
        int.Parse(textResult.Text), //param2
        ((arResult) => //param3
            results = client.EndSearch(arResult);
                (Action<IList<IImageResult>>)(async (data) =>
                      for (int i = 0; i < results.Count; i++)
                            Image img = new Image{
                            Source = await DownloadImage(results[i].TbImage.Url),
                             Stretch = Stretch.UniformToFill,
                             StretchDirection = StretchDirection.DownOnly,
        null //param4


The key function here is to download the image which is done by await DownloadImage in the above code which is again a single line function:

private async Task<BitmapImage> DownloadImage(string url)
    return byteArrayToImage(await new WebClient().DownloadDataTaskAsync(url));

public BitmapImage byteArrayToImage(byte[] byteArrayIn)
    BitmapImage myBitmapImage;
    using (MemoryStream stream = new MemoryStream(byteArrayIn))
        myBitmapImage = new BitmapImage();
        myBitmapImage.StreamSource = stream;
        myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    return myBitmapImage;

Here the download image uses the Async CTP function DownloadDataTaskAsync which downloads the image in terms of bytes. Hence a byteArrayToImage utility function is written to convert the same in BitmapImage format.

The interesting point here is the await keyword, which is used both for the DownloadDataTaskAsync function and the lambda expression used.

Points of Interest

Hence to be simple, with C# 5.0 coming out, the code for which once we used to write pages and pages is going to be in word count, with the same functionality.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Krishnachytanya Ayyagari
Software Developer (Junior) Thomson Reuters
India India
Currently, I am Software Engineer in ThomsonReuters. I am a man who believes in a fact that -"Technology is best available now-a-days but what matter is "How you use it?" and "What you solved using it?" ....." My site, my work and everything about me demonstrates this basic idea. I do what I love and I love what I do.....
U can find all about me @ Smile | :)

You may also be interested in...

Comments and Discussions

QuestionHow to increase the width of images? Pin
prasadbhagat9-Jun-16 0:04
memberprasadbhagat9-Jun-16 0:04 
GeneralThe underlying Google WebService API for this is now unavailable (as of 2/12/15) Pin
PeteMagrath2-Dec-15 16:48
memberPeteMagrath2-Dec-15 16:48 
GeneralRe: The underlying Google WebService API for this is now unavailable (as of 2/12/15) Pin
Lenny Chen31-Dec-15 20:42
professionalLenny Chen31-Dec-15 20:42 
Questionnumber of results Pin
Member 861249112-Aug-14 23:45
memberMember 861249112-Aug-14 23:45 
Bala Vignesh12-Oct-13 19:02
memberBala Vignesh12-Oct-13 19:02 
GeneralMy vote of 5 Pin
dinhvandong11-Jul-12 15:50
memberdinhvandong11-Jul-12 15:50 
GeneralMy vote of 5 Pin
manudurand26-Sep-11 3:41
membermanudurand26-Sep-11 3:41 
GeneralRe: My vote of 5 Pin
Krishnachytanya Ayyagari26-Sep-11 3:47
memberKrishnachytanya Ayyagari26-Sep-11 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180621.3 | Last Updated 25 Sep 2011
Article Copyright 2011 by Krishnachytanya Ayyagari
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid