Find Topics

Saturday, January 28, 2012

How To Add Youtube Video and Thumbnail in Asp.net Dynamically

0 comments

Last time i wrote about how To Add youtube Video To Asp.net website by URL without embeded code now i will tech you how you can add thumbnails of that videos , suppose you added some videos of youtube and you want to show it in a grid view 
but its not a easy task to create youtube videos tumbnails from your site. so we can use youtube thumbnails which used in youtube website.

How to Use


Here is the YouTube URL format that you will need in order to view and download a thumbnail from a YouTube video:

http://img.youtube.com/vi/VIDEO_ID/#.jpg



where for the video example above, VIDEO_ID=_Oev9aSkCd0 and # (1,2, or 3) corresponds to one of the 3 thumbnails that YouTube automatically generates.
Like Thumbnail for this video is
http://img.youtube.com/vi/_Oev9aSkCd0/1.jpg


Now How to add it in Asp.net Grid view
First do same as the previous tutorial and bind gridview by selecting youtube url  from database which was stored by you at the time of adding video
then
Add this in you C# code.



 public string getSRC(object imgSRC)
    {
        DataRowView dRView = (DataRowView)imgSRC;

        string v = dRView["videolink"].ToString();
            v = v.Substring(v.LastIndexOf("v=") + 2);
            if (v.Contains("&"))
                v = v.Substring(0, v.LastIndexOf("&"));
            string imgurl = "http://img.youtube.com/vi/" + v + "/2.jpg";
            return imgurl;

    }

This will Return Url of thumbnail Image.
Now Bind this url in asp.net Grid view Item Template

<td>    
<a href='<%# "watchvideo.aspx?videoid=" + Eval("i_autoid")%>'> 
 <asp:Image ID="Image2" runat="server" Height="67px" 
ImageUrl="<%# getSRC(Container.DataItem) %>" Width="71px" /></a>
</td>

Working Example

Do you like this article it Take 5 Second to Share with your Friends

Subscribe Email alerts to receive more Tech Updates to your Inbox!

Not Getting? Feel free to ask

Please Leave Comments your Comments are always Helpful. I will try to reply to your queries as soon as time allows.

Regards,
Nishant Srivastava

No Facebook Account! No worries you can add Comments Here!

Leave A Reply