Windows Forms: Youtube Search in C#

This post shows you How to Create a Youtube Search using Youtube Search API in C# .NET Windows Forms Application.

YoutubeSearch.dll is a library for .NET, written in c# code that helps you get search query results from YouTube

To use youtube search api you can right click on your project, then select Manage NuGet Packages -> Search youtubesearch -> Install

Drag the TextBox, Button and DataGridView controls from the visual studio toolbox to your winform, then design a simple UI that allows you to enter the video name, then display search query results to the DataGridView as shown below.

youtube search in c#

Creating the Video class allows you to map data return from the search query.

public class Video
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string Url { get; set; }
    public Image Thumbnail { get; set; }
}

Add the click event handler to the Search button that allows you to search video from the youtube site.

private void btnSearch_Click(object sender, EventArgs e)
{
    VideoSearch items = new VideoSearch();
    List<Video> list = new List<Video>();
    foreach (var item in items.SearchQuery(txtSearch.Text, 1))
    {
        Video video = new Video();
        video.Title = item.Title;
        video.Author = item.Author;
        video.Url = item.Url;
        byte[] imageBytes = new WebClient().DownloadData(item.Thumbnail);
        //Get thubnail video
        using (MemoryStream ms = new MemoryStream(imageBytes))
        {
            //Read image from stream
            video.Thumbnail = Image.FromStream(ms);
        }
        list.Add(video);
    }
    //Add videos to datagridview
    videoBindingSource.DataSource = list;
}

Through the youtube search api example c# code, we will get data return from the query search results. Next, download the thumbnail image corresponding the video, then add the video object to the video list. Finllay, set DataSource property of bindingsource to the video list as shown above.

VIDEO TUTORIAL