버전 2.0

sharpdevelop/googleImageSearch 2015. 9. 7. 23:09

버전2 소스파일 :googleImageSearch2_src.zip


버전2 실행파일 :googleImageSearch2_Debug.zip



---------------------------------------------------------


변경사항 : HtmlAgilityPack 사용하는 방식으로 변경해 봄.


https://htmlagilitypack.codeplex.com/



1. HtmlAgilityPack 다운받아서 HtmlAgilityPack.dll을  레퍼런스에 추가하는 방법











2. HtmlAgilityPack.dll  라이브러리 이용하는 걸로 수정된 코드


/*
 * Created by SharpDevelop.
 * User: PC1
 * Date: 2015-09-07
 * Time: 오후 10:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;


using HtmlAgilityPack;


namespace googleImageSearch2
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        
        void MainFormLoad(object sender, EventArgs e)
        {
            //1. url 대입
            //txtUrl.Text ="https://www.google.co.kr/search?q=%EA%B9%80%EC%82%AC%EB%9E%91&biw=1280&bih=895&source=lnms&tbm=isch&sa=X&ved=0CAYQ_AUoAWoVChMIvPDvrYa5xwIVxiiUCh3_SA24";//김사랑
            textBox1.Text="설현"//기본값

        }
        
        void Button1Click(object sender, EventArgs e)
        {
            textBox2.Text="https://www.google.co.kr/search?q="
                          + textBox1.Text
                       + "&biw=1280&bih=895&source=lnms&tbm=isch&sa=X&ved=0CAYQ_AUoAWoVChMIrJze3Ne5xwIVJOemCh2BgAdd";
                
            //2. 구글에 이미지 검색요청 해서 응답 받아오기
            
            //http://findfun.tistory.com/44
            WebRequest request = WebRequest.Create(textBox2.Text);
            request.Credentials = CredentialCache.DefaultCredentials;
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            Encoding encode;
            if (response.CharacterSet.ToLower() == "utf-8") { encode = Encoding.UTF8; }
            else { encode = Encoding.Default; }
            
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, encode);
 
            string responseFromServer = reader.ReadToEnd();            


            dataStream.Close();
            reader.Close();
            textBox3.Text=responseFromServer;
            
            
            //-------------------------
            
            //XmlDocument doc = new XmlDocument();
            //doc.LoadXml(responseFromServer);
            //XmlNodeList results = doc.SelectNodes("//img/@src");
            
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(textBox3.Text);
            
            textBox4.Text="";

            imageList1.Images.Clear();
            imageList1.ImageSize = new Size(120120);

            listView1.Clear();
            listView1.View = View.LargeIcon;


            List<string> images = new List<string>();
            
            string imgUrlPath = "";
            int count=0;
            
            foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(".//img[@src]")){
                 //string attributeValue = node.GetAttributeValue("prop", "");
                 if ((node.GetAttributeValue("src","").ToString()).Contains("https://encrypted-")){  
                     
                     imgUrlPath = node.GetAttributeValue("src","").ToString();
                    images.Add(imgUrlPath);

                    textBox4.Text +=  imgUrlPath + "\r\n";
                    
                    //------------------------------
                    //출처 : http://www.devpia.com/Maeul/Contents/Detail.aspx?BoardID=17&MaeulNo=8&no=111893&ref=111788
                    // 이미지 url을 imaglist1에 넣기
                    byte[] buffer = null;
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    System.Net.WebClient wc = new System.Net.WebClient();

                    buffer = wc.DownloadData(imgUrlPath);
                    
                    ms.Write(buffer, 0, buffer.Length);
                    imageList1.Images.Add(new Bitmap(ms));
                     //--------------------------

                     int idx = imageList1.Images.Keys.Count-1;
                    imageList1.Images.SetKeyName(idx, "");
                    listView1.Items.Add(count.ToString(),idx);
                        
                    listView1.LargeImageList = imageList1;
                    count++;

                 }
                
            }
         //MessageBox.Show(imageList1.Images.Count.ToString());
            
        }
    }
}


3. 실행결과 캡쳐 (UI는 저번버전과 차이점은 없음.)













'sharpdevelop > googleImageSearch' 카테고리의 다른 글

버전 1.0  (0) 2015.09.06
: