Posted 10 years ago
·
Author
This is a modified version of my YouTube thumbnail downloader here: viewtopic.php?f=109&t=9911 It's purpose is to display the video thumbnail on the form. I use this to quickly check lots of video thumbnails for different reasons.
As with the thumbnail downloader, most of the code is for sanitizing the input and is also thread safe and ready for multithreading.
It can accept 3 types of input:
-A basic YouTube url:
-A video thumbnail link:
- A plain video id
Usage:
Method:
This is the createPB method I use to display the resulting thumbnail. It's quick and dirty but does the job
This is the loadimg method that allows you to open the thumbnail in your browser by double clicking the picturebox
As with the thumbnail downloader, most of the code is for sanitizing the input and is also thread safe and ready for multithreading.
It can accept 3 types of input:
-A basic YouTube url:
https://www.youtube.com/watch?v=VIDEO ID
-A video thumbnail link:
https://i.ytimg.com/vi/VIDEO ID/0.jpg
- A plain video id
Usage:
Method:
private void getThumbnail(object oYTurl)
{
string sYTurl = oYTurl.ToString(), vidID = null;
//if the input isn't empty
if(!string.IsNullOrEmpty(sYTurl))
{
//if the input matches a basic YT link
if(Regex.IsMatch(sYTurl, "(http|https)://www\\.youtube\\.com/watch\\?v=.*"))
{
//remove everything but the video id
vidID = Regex.Replace(sYTurl, "(http|https)://www\\.youtube\\.com/watch\\?v=", "");
}
//if the input is a thumbnail url
else if (Regex.IsMatch(sYTurl, "(http|https)://i.*\\.ytimg.com/vi/.*/0.jpg"))
{
//scrub the url leaving behind the video id
vidID = Regex.Replace(sYTurl, "(http|https)://i.*\\.ytimg.com/vi/", "").Replace("/0.jpg","");
}
else
{
//if the input isn't a basic YT link and not a thumbnail link
//then check if it is roughly the same size as a video id
if (sYTurl.Length > 5 && sYTurl.Length < 12)
{
using (WebClient wc = new WebClient())
{
try
{
//pass the input to the video api
wc.DownloadString("https://gdata.youtube.com/feeds/api/videos/" + sYTurl);
//if no errors, the input is a valid video id
vidID = sYTurl;
}
catch (WebException we)
{
//The input returns a 404 and therefore is not a valid id
HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound || errorResponse.StatusCode == HttpStatusCode.RequestTimeout)
{
return;
}
}
}
}
}
//if the vidID is not empty
if(!string.IsNullOrEmpty(vidID))
{
createPB("https://i.ytimg.com/vi/" + vidID.ToString() + "/maxresdefault.jpg");
createPB("https://i.ytimg.com/vi/" + vidID.ToString() + "/0.jpg");
}
}
}
This is the createPB method I use to display the resulting thumbnail. It's quick and dirty but does the job
public void createPB(object url)
{
PictureBox pb = new PictureBox();
pb.Size = new Size(120, 90);
pb.Tag = url.ToString();
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.DoubleClick += loadimg;
try
{
pb.Load(url.ToString());
}
catch (WebException we)
{
HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound || errorResponse.StatusCode == HttpStatusCode.RequestTimeout)
{
return;
}
}
if (this.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate()
{
flowLayoutPanel1.Controls.Add(pb);
});
}
else
{
flowLayoutPanel1.Controls.Add(pb);
}
}
This is the loadimg method that allows you to open the thumbnail in your browser by double clicking the picturebox