代码改变世界

获取网站缩略图代码

2011-09-07 14:16  TTlive  阅读(457)  评论(0编辑  收藏  举报

先看界面效果图:

 

 最终运行得出的图如下:

 

 

 

页面代码:

 

 

Form1.cs
/*
* 作者:HJL
* Blog:Http://www.cnblogs.com/xt_hjl
* 获取网站缩略图程序
* 2010年8月最后一天
* 欢迎修改和传播
* 最好能保留该信息^_^
* 也欢迎大家访问我的博客
* Http://www.cnblogs.com/xt_hjl
*
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Utility;
using System.Threading;
using System.Text.RegularExpressions;

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

private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text))
{
MessageBox.Show(
"请输入要截图的网址!");
return;
}
Regex r
= new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
if (r.IsMatch(this.textBox1.Text))
{
if (string.IsNullOrEmpty(this.textBox2.Text))
{
MessageBox.Show(
"请选择要保存的目录!");
return;
}
if (string.IsNullOrEmpty(this.textBox3.Text))
{
MessageBox.Show(
"请输入要保存的文件名!");
return;
}
try
{
GetWebImage gwi
= new GetWebImage(this.textBox1.Text, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, 320, 240);
Bitmap bi
= gwi.GetBitmap();
bi.Save(
this.textBox2.Text.Trim() + "\\" + this.textBox3.Text.Trim());
System.Diagnostics.Process.Start(
this.textBox2.Text.Trim()+"\\"+this.textBox3.Text.Trim());
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(
"请输入正确的网址!");
return;
}
}

private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog(
this);
this.textBox2.Text = folderBrowserDialog1.SelectedPath;
}
}
}

 

另外还在启动时做了特别处理。。。。看代码:

 

Program.cs
1 /*
2 * 作者:HJL
3 * Blog:Http://www.cnblogs.com/xt_hjl
4 * 获取网站缩略图程序
5 * 2010年8月最后一天
6 * 欢迎修改和传播
7 * 最好能保留该信息^_^
8 * 也欢迎大家访问我的博客
9 * Http://www.cnblogs.com/xt_hjl
10 *
11 */
12  using System;
13  using System.Collections.Generic;
14  using System.Windows.Forms;
15  using Utility;
16  using System.Drawing;
17  using System.Text.RegularExpressions;
18
19  namespace WindowsFormsApplication1
20 {
21 static class Program
22 {
23 /// <summary>
24 /// 应用程序的主入口点。
25 ///ex: a.exe www.cnblogs.com c:\\ a.bmp 320 340 on
26 /// </summary>
27   [STAThread]
28 static void Main(string[] args)
29 {
30 if (args.Length == 6)
31 {
32 Regex r = new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
33 if (r.IsMatch(args[0]))
34 {
35 try
36 {
37 GetWebImage gwi = new GetWebImage(args[0], 1024, 768, Convert.ToInt32(args[3]), Convert.ToInt32(args[4]));
38 Bitmap bi = gwi.GetBitmap();
39 bi.Save(args[1] + "\\" + args[2]);
40 if (args[5].Equals("on"))
41 {
42 System.Diagnostics.Process.Start(args[1] + "\\" + args[2]);
43 }
44
45 }
46 catch (Exception ex)
47 {
48 MessageBox.Show(ex.Message);
49 }
50 }
51 else
52 {
53 MessageBox.Show("参数不正确,如:http://www.cnblogs.com/xt_hjl c:\\ a.bmp 320 240 on");
54 }
55
56 return;
57 }
58 else if (args.Length > 0)
59 {
60 MessageBox.Show("参数不够,如:http://www.cnblogs.com/xt_hjl c:\\ a.bmp 320 240 on");
61 return;
62 }
63 Application.EnableVisualStyles();
64 Application.SetCompatibleTextRenderingDefault(false);
65 Application.Run(new Form1());
66 }
67 }
68 }
69  

 

 

引用到的代码
/*
* 修改者:吴鹏(AlphaWu)
* Blog:Http://AlphaWu.CoM
* 获取网站缩略图代码
* 2006 圣诞节
* C#代码根据该页面“
http://www.codeproject.com/useritems/website_screenshot.asp
* VB.Net代码修改而来
* 欢迎修改和传播
* 最好能保留该信息^_^
* 也欢迎大家访问我的博客
* Http://AlphaWu.Cnblogs.CoM
*
* Http://AlphaWu.CoM
*
*
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Utility
{
/// <summary>
/// GetImage thumb = new GetImage(url, 1024, 768, 320, 240);
//System.Drawing.Bitmap x = thumb.GetBitmap();
//x.Save(Response.OutputStream, ImageFormat.Jpeg);
//Response.ContentType = "image/jpeg";
/// </summary>
public class GetWebImage
{
int S_Height;
int S_Width;
int F_Height;
int F_Width;
string MyURL;

public int ScreenHeight
{
get
{
return S_Height;
}
set
{
S_Height
= value;
}
}

public int ScreenWidth
{
get
{
return S_Width;
}
set
{
S_Width
= value;
}
}

public int ImageHeight
{
get
{
return F_Height;
}
set
{
F_Height
= value;
}
}

public int ImageWidth
{
get
{
return F_Width;
}
set
{
F_Width
= value;
}
}

public string WebSite
{
get
{
return MyURL;
}
set
{
MyURL
= value;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="GetWebImage"/> class.
/// </summary>
/// <param name="WebSite">The web site.</param>
/// <param name="ScreenWidth">Width of the screen.</param>
/// <param name="ScreenHeight">Height of the screen.</param>
/// <param name="ImageWidth">Width of the image.</param>
/// <param name="ImageHeight">Height of the image.</param>
public GetWebImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenHeight = ScreenHeight;
this.ScreenWidth = ScreenWidth;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}

/// <summary>
/// Gets the bitmap.
/// </summary>
/// <returns></returns>
public Bitmap GetBitmap()
{
WebPageBitmap Shot
= new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);

Shot.GetIt();
Bitmap Pic
= Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}

/// <summary>
///
/// </summary>
public class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;

/// <summary>
/// Initializes a new instance of the <see cref="WebPageBitmap"/> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public WebPageBitmap(string url, int width, int height)
{
this.URL = url;
this.Width = width;
this.Height = height;
MyBrowser
= new WebBrowser();
MyBrowser.ScrollBarsEnabled
= false;
MyBrowser.Size
= new Size(this.Width, this.Height);
}

/// <summary>
/// Gets it.
/// </summary>
public void GetIt()
{
MyBrowser.Navigate(
this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}

/// <summary>
/// Draws the bitmap.
/// </summary>
/// <param name="theight">The theight.</param>
/// <param name="twidth">The twidth.</param>
/// <returns></returns>
public Bitmap DrawBitmap(int theight, int twidth)
{

Bitmap myBitmap
= new Bitmap(this.Width, this.Height);
Rectangle DrawRect
= new Rectangle(0, 0, this.Width, this.Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput
= myBitmap;
System.Drawing.Bitmap oThumbNail
= new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g
= Graphics.FromImage(oThumbNail);

g.CompositingQuality
= System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.SmoothingMode
= System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

Rectangle oRectangle
= new Rectangle(0, 0, twidth, theight);
g.DrawImage(imgOutput, oRectangle);

try
{
return oThumbNail;
}
catch
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput
= null;
MyBrowser.Dispose();
MyBrowser
= null;
}

}
}
}

 

 本次源码下载

欢迎转载,转载请注明来处 http://www.cnblogs.com/xt_hjl/archive/2010/08/30/1812934.html