Tuesday, May 28, 2013

Grab Youtube Images and Save it in Folder

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    public System.Drawing.Image DownloadImageFromUrl(string imageUrl)
    {
        System.Drawing.Image image = null;
        try
        {
            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
            webRequest.AllowWriteStreamBuffering = true;
            webRequest.Timeout = 30000;
            System.Net.WebResponse webResponse = webRequest.GetResponse();
            System.IO.Stream stream = webResponse.GetResponseStream();
            image = System.Drawing.Image.FromStream(stream);
            webResponse.Close();
        }
        catch (Exception ex)
        {
            return null;
        }
        return image;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        System.Drawing.Image image = DownloadImageFromUrl("http://img.youtube.com/vi/MnyYCyIP--c/1.jpg"); //txtUrl.Text.Trim()
         string rootPath = @"C:\DownloadedImageFromUrl";
       // string fileName = System.IO.Path.Combine(rootPath, "test.gif");
        string fileName = Server.MapPath("~/Images/" + "test.gif");//
        image.Save(fileName);
        //Note:- if gdi+ error occurs,it means, the path which u are saving does not exists or wrong path...
    }
}

0 comments:

Post a Comment