How to Create a Youtube Search in C#
By FoxLearn 7/19/2024 2:42:21 AM 7.92K
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#
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.
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