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.

To integrate YouTube search into a C# WinForms application, you can use the YouTube API. YoutubeSearch.dll is a library for .NET, written in c# code that helps you get search query results from YouTube.

How to integrate youtube search in C#

Here's a step-by-step guide to use youtube search api

Right click on your project, then select Manage NuGet Packages -> Search youtubesearch -> Install

Drag and drop 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;
}

When you run your application and type a search query into the TextBox and click the button, it will fetch YouTube search results and display them in the GridView.

To show the thumbnail image, you need to download the thumbnail image corresponding the video, then add the video object to the video list. Finally, set DataSource property of bindingsource to the video list as shown above.

VIDEO TUTORIAL