代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
抓取网页内容常用类
作者:
dezai
/ 发布于
2014/1/26
/
287
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace Yihu.Frame { public class WebSiteThumbnail { Bitmap m_Bitmap; string m_Url; int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight; public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) { m_Url = Url; m_BrowserHeight = BrowserHeight; m_BrowserWidth = BrowserWidth; m_ThumbnailWidth = ThumbnailWidth; m_ThumbnailHeight = ThumbnailHeight; } /// <summary> /// 载取网页 /// </summary> /// <param name="Url">网页地址</param> /// <param name="BrowserWidth">页面宽度</param> /// <param name="BrowserHeight">页面高度</param> /// <param name="ThumbnailWidth">图像宽度</param> /// <param name="ThumbnailHeight">图像高度</param> /// <returns></returns> public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) { WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight); return thumbnailGenerator.GenerateWebSiteThumbnailImage(); } /// <summary> /// 启用新线程执行 /// </summary> /// <returns></returns> public Bitmap GenerateWebSiteThumbnailImage() { Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage)); m_thread.SetApartmentState(ApartmentState.STA); m_thread.Start(); m_thread.Join(); return m_Bitmap; } /// <summary> /// 读取整个网页的类容 /// </summary> protected void _GenerateWebSiteThumbnailImage() { WebBrowser m_WebBrowser = new WebBrowser(); m_WebBrowser.ScrollBarsEnabled = false; m_WebBrowser.Navigate(m_Url); m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); m_WebBrowser.Dispose(); } /// <summary> /// 转换网页类容为图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser m_WebBrowser = (WebBrowser)sender; m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight); m_WebBrowser.ScrollBarsEnabled = false; m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height); m_WebBrowser.BringToFront(); m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds); m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero); } /// <summary> /// 生成缩略图 /// </summary> /// <param name="left">左边偏移量</param> /// <param name="top">上边偏移量</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <param name="originalImagePath">图片路径</param> /// <param name="fliepatch">缩略图保存路劲</param> protected static void dropimg(int left, int top, int width, int height, string originalImagePath,string fliepatch) { System.Drawing.Image originalImage = null; originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = left; int y = top; int ow = originalImage.Width; int oh = originalImage.Height; //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, towidth, toheight), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(fliepatch, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); if (System.IO.File.Exists(originalImagePath)) { System.IO.File.Delete(originalImagePath); } } } /// <summary> /// 保存图片 /// </summary> /// <param name="url">网页的url地址</param> /// <param name="left">距左边的宽度</param> /// <param name="top">距上边的高度</param> /// <param name="width">图片宽度</param> /// <param name="height">图片高度</param> /// <param name="filepatch">图片保存路径</param> public static void saveimg( string url,int left,int top,int width,int height,string filepatch) { //获取网页载图 Bitmap m_Bitmap = Yihu.Frame.WebSiteThumbnail.GetWebSiteThumbnail(url, 818, 721, 818, 721); if (!System.IO.Directory.Exists(Yihu.Common.Utils.GetMapPath("/UploadImg/mycard/"))) { System.IO.Directory.CreateDirectory(Yihu.Common.Utils.GetMapPath("/UploadImg/mycard/")); } string imgpatch = Yihu.Common.Utils.GetMapPath("/UploadImg/mycard/") + Yihu.Frame.BasePage.userId + "_model.jpg"; m_Bitmap.Save(imgpatch, System.Drawing.Imaging.ImageFormat.Jpeg);//JPG、GIF、PNG等均可 dropimg(left, top, width, height, imgpatch, filepatch); } } }
试试其它关键字
抓取网页内容
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3