특정 사이트에 있는 이미지 혹은 동영상 등의 링크를 가져옵니다.

(프레임 내에 있는 img 태그 혹은 embed 태그는 가져오지 않습니다.)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace webbrowserResource
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.imageList1.ImageSize = new Size(64, 64);
        }

        public Bitmap getImageFromURL(String sURL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(sURL);
            myRequest.Method = "GET";
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();

            return bmp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + textBox1.Text);
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream resStream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(resStream, Encoding.Default);
                string result = readStream.ReadToEnd();
                int idx = 0;

                listView1.Items.Clear();
                imageList1.Images.Clear();
                resStream.Close();
                readStream.Close();

                string[] imgURL = Regex.Split(result, "<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>");
                for (int i = 1; i < imgURL.Length; i+=2)
                {
                    string iURL = string.Empty;
                    ListViewItem item = new ListViewItem();

                    if (imgURL[i].StartsWith("http://")) iURL = imgURL[i];
                    else iURL = "http://" + textBox1.Text + (textBox1.Text.Substring(textBox1.Text.Length - 1) != "/" ? "/" : "") + imgURL[i];
                    if (iURL.IndexOf(' ') > -1) iURL = iURL.Substring(0, iURL.IndexOf(' '));
                    try
                    {
                        this.imageList1.Images.Add(getImageFromURL(iURL));
                        item.ImageIndex = idx; idx++;
                        item.Text = iURL;
                        this.listView1.Items.Add(item);
                    }
                    catch (Exception) { }
                }

                string[] embedURL = Regex.Split(result, "<embed[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>");
                for (int i = 1; i < embedURL.Length; i += 2) listView2.Items.Add(embedURL[i]);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                Process proc = new Process();
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.FileName = listView1.SelectedItems[0].Text;
                proc.Start();
            }
        }
    }
}