Click here to Skip to main content
15,883,606 members
Articles / Desktop Programming / WPF

Google Image Search Client in C# 5.0 and WPF

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
25 Sep 20112 min read 67.6K   9.5K   18   9
A simple 50 lines of C# 5.0 code which showcases the await and async features in C# 5.0.

Capture1.PNG

Capture2.PNG

Introduction

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 :)

Background

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.

C#
 private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    wrapPanel.Children.Clear();
    GimageSearchClient client = new GimageSearchClient("http://www.google.com");
    IList<IImageResult> results ;
   
     IAsyncResult result= client.BeginSearch(
        textSearch.Text.Trim(),  //param1
        int.Parse(textResult.Text), //param2
        ((arResult) => //param3
        {
            results = client.EndSearch(arResult);
            Dispatcher.Invoke(DispatcherPriority.Send,
                (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,
                                };
                            wrapPanel.Children.Add(img);
                        }
            }),null);
        }),
        null //param4
        );
}

Observation

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:

C#
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.BeginInit();
        myBitmapImage.StreamSource = stream;
        myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        myBitmapImage.EndInit();
    }
    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.

License

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


Written By
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 @ http://www.AKrishnachytanya.com Smile | :)

Comments and Discussions

 
QuestionIs this still working? Pin
Chandraprakash G (சந்திரப்பிரகாஷ்)19-Jul-20 14:51
Chandraprakash G (சந்திரப்பிரகாஷ்)19-Jul-20 14:51 
QuestionHow to increase the width of images? Pin
prasadbhagat9-Jun-16 0:04
prasadbhagat9-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
PeteMagrath2-Dec-15 16:48 
GeneralRe: The underlying Google WebService API for this is now unavailable (as of 2/12/15) Pin
Pandalien31-Dec-15 20:42
professionalPandalien31-Dec-15 20:42 
Questionnumber of results Pin
Member 861249112-Aug-14 23:45
Member 861249112-Aug-14 23:45 
QuestionSIR, I GET THE FOLLOWING EXCEPTION... AM A BEGINNER TO PROGRAMMING... CAN U HELP ME MR.Krishnachytanya Ayyagari Pin
Bala Vignesh12-Oct-13 19:02
Bala Vignesh12-Oct-13 19:02 
GeneralMy vote of 5 Pin
dinhvandong11-Jul-12 15:50
dinhvandong11-Jul-12 15:50 
GeneralMy vote of 5 Pin
manudurand26-Sep-11 3:41
manudurand26-Sep-11 3:41 
Nice article. Would have been perfect with a bit of MVVM! FYI, it's working too in VS 2010 SP1 (.NET 4.0) with Async CTP Refresh installed.
GeneralRe: My vote of 5 Pin
Krishnachytanya Ayyagari26-Sep-11 3:47
Krishnachytanya 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.