using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Threading;
using WebImageLib;
using WISGoogle;
namespace WebImageSearch
{
///
/// Form1 の概要の説明です。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Panel panel1;
private ThumbViewerControl.ThumbViewer thumbViewer1;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonStart;
private System.Windows.Forms.TextBox textBoxKeyword;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
///
/// 使用されているリソースに後処理を実行します。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///
private void InitializeComponent()
{
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.panel1 = new System.Windows.Forms.Panel();
this.thumbViewer1 = new ThumbViewerControl.ThumbViewer();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.buttonStop = new System.Windows.Forms.Button();
this.buttonStart = new System.Windows.Forms.Button();
this.textBoxKeyword = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 344);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(492, 22);
this.statusBar1.TabIndex = 0;
this.statusBar1.Text = "サムネイルは左ダブルクリックで大きな画像、右ダブルクリックで画像を含むページを開きます。";
//
// panel1
//
this.panel1.Controls.Add(this.trackBar1);
this.panel1.Controls.Add(this.buttonStop);
this.panel1.Controls.Add(this.buttonStart);
this.panel1.Controls.Add(this.textBoxKeyword);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(492, 80);
this.panel1.TabIndex = 1;
//
// thumbViewer1
//
this.thumbViewer1.BackColor = System.Drawing.SystemColors.Window;
this.thumbViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.thumbViewer1.Location = new System.Drawing.Point(0, 80);
this.thumbViewer1.Name = "thumbViewer1";
this.thumbViewer1.Size = new System.Drawing.Size(492, 264);
this.thumbViewer1.TabIndex = 2;
this.thumbViewer1.ThumbnailSize = 64;
this.thumbViewer1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.thumbViewer1_MouseDown);
//
// trackBar1
//
this.trackBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.trackBar1.Location = new System.Drawing.Point(0, 32);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(492, 45);
this.trackBar1.TabIndex = 3;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// buttonStop
//
this.buttonStop.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonStop.Location = new System.Drawing.Point(376, 8);
this.buttonStop.Name = "buttonStop";
this.buttonStop.TabIndex = 2;
this.buttonStop.Text = "停止";
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// buttonStart
//
this.buttonStart.Location = new System.Drawing.Point(296, 8);
this.buttonStart.Name = "buttonStart";
this.buttonStart.TabIndex = 1;
this.buttonStart.Text = "検索開始";
this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
//
// textBoxKeyword
//
this.textBoxKeyword.Location = new System.Drawing.Point(8, 8);
this.textBoxKeyword.Name = "textBoxKeyword";
this.textBoxKeyword.Size = new System.Drawing.Size(280, 19);
this.textBoxKeyword.TabIndex = 0;
this.textBoxKeyword.Text = "検索キーワードを入力してください";
//
// Form1
//
this.AcceptButton = this.buttonStart;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.CancelButton = this.buttonStop;
this.ClientSize = new System.Drawing.Size(492, 366);
this.Controls.Add(this.thumbViewer1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.statusBar1);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// アプリケーションのメイン エントリ ポイントです。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
bool working = false; // 検索実行中を示すフラグ
int numFound = 0; // 検索結果件数
string keyword = ""; // 入力された検索キーワード
string appName = "Web画像検索";
IWebImage google = new GoogleImage();
ArrayList webImages = ArrayList.Synchronized(new ArrayList());
private void Form1_Load(object sender, System.EventArgs e)
{
// トラックバーのプロパティ設定
trackBar1.Minimum = 15;
trackBar1.Maximum = thumbViewer1.ClientSize.Width;
trackBar1.TickFrequency = 10;
// サムネイルの初期サイズをとりあえず100に設定
trackBar1.Value = 100;
thumbViewer1.ThumbnailSize = 100;
readyToStart();
this.Text = appName;
}
private void buttonStart_Click(object sender, System.EventArgs e)
{
keyword = textBoxKeyword.Text;
// キーワードが未入力なら何もしない
if (keyword == "")
return;
// 状態の初期化
this.Text = appName;
thumbViewer1.Clear();
webImages.Clear();
google.Reset();
thumbViewer1.Focus();
statusBar1.Text = "";
working = true;
buttonStart.Enabled = false;
numFound = 0;
// 別スレッドで画像を検索・追加
Thread worker = new Thread(new ThreadStart(adding));
worker.Name = "AddImage";
worker.IsBackground = true;
worker.Start();
}
void adding()
{
do
{
foreach (WebImage wi in google.GetImages(keyword))
{
if (working == false)
break;
using (Image img = downloadImage(wi.SmallImageURL))
{
if (img != null)
{
webImages.Add(wi);
addToThumbViewer(img);
}
}
}
} while (google.MoveNext() && working);
working = false; // 停止ボタンを押さずに抜けたときのため
readyToStart();
}
delegate void addToThumbViewerDelegate(Image img);
void addToThumbViewer(Image img)
{
if (this.InvokeRequired)
{
this.Invoke(
new addToThumbViewerDelegate(addToThumbViewer),
new object[] {img});
}
else
{
this.Text = String.Format(
"{0} - 検索結果: {1} 件 ({2} ページ)",
appName, ++numFound, google.CurrentPage);
thumbViewer1.Add(img);
}
}
delegate void readyToStartDelegate();
void readyToStart()
{
if (this.InvokeRequired)
{
this.Invoke(new readyToStartDelegate(readyToStart));
}
else
{
panel1.Enabled = true;
buttonStart.Enabled = true;
this.ActiveControl = textBoxKeyword;
textBoxKeyword.Focus();
textBoxKeyword.SelectAll();
}
}
private void buttonStop_Click(object sender, System.EventArgs e)
{
if (working == false)
{
readyToStart();
return;
}
panel1.Enabled = false;
working = false;
}
private void trackBar1_Scroll(object sender, System.EventArgs e)
{
thumbViewer1.ThumbnailSize = trackBar1.Value;
}
private void Form1_Resize(object sender, System.EventArgs e)
{
int width = thumbViewer1.ClientSize.Width;
trackBar1.Maximum = width;
}
private void thumbViewer1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
WebImage wi = (WebImage)webImages[thumbViewer1.SelectedIndex];
statusBar1.Text = wi.DetailPageURL;
if (e.Clicks == 2) // ダブルクリック
{
System.Diagnostics.Process.Start(
e.Button == MouseButtons.Left
? wi.LargeImageURL : wi.DetailPageURL);
}
}
public static Image downloadImage(string url)
{
Image img = null;
WebClient wc = null;
Stream st = null;
try
{
wc = new WebClient();
st = wc.OpenRead(url);
img = Image.FromStream(st);
}
catch {}
finally
{
if (st != null)
st.Close();
if (wc != null)
wc.Dispose();
}
return img;
}
}
}