ASP.NET根据URL生成网页缩略图示例程序(C#语言)
工作中可能马上要用到根据URL生成网页缩略图功能,提前做好准备。 在网上找了份源码,但是有错误:当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a9”,解决后运行良好,记录在此备用! 起始页:Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Snap</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value="生成网页缩略图"/>
</div>
</form>
</body>
</html> 调用页:Snap.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div> </div>
</form>
</body>
</html> PS:红色字体部分是为解决错误增加的代码,强制程序在单线程环境下运行! 调用页:Snap.aspx.cs using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Imaging; namespace CaptureToImage
{
public partial class Snap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = string.Empty;
url = Request.QueryString[];
try
{
GetImage thumb = new GetImage(url, , , , );
System.Drawing.Bitmap x = thumb.GetBitmap();
x.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.ContentType = "image/jpeg";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
} 类文件:GetImage.cs using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Web.UI; namespace CaptureToImage
{
public class GetImage
{
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;
}
} public GetImage(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;
}
[STAThread]
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;
}
} public class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width; 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);
} public void GetIt()
{
NavigateUrl(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
} public delegate void DelUserHandler(string url); public void NavigateUrl(string url)
{
try
{
if (this.MyBrowser.InvokeRequired)
{
DelUserHandler handler = new DelUserHandler(NavigateUrl);
MyBrowser.Invoke(handler, url);
}
else
{
this.MyBrowser.Navigate(url);
}
}
catch (Exception ex)
{
throw new Exception("NavigateUrl()" + ex.Message);
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
Bitmap myBitmap = new Bitmap(this.Width, this.Height);
Rectangle DrawRect = new Rectangle(, , 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(, , twidth, theight);
g.DrawImage(imgOutput, oRectangle); try
{
return oThumbNail;
}
catch
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
} }
}
} PS:项目中需要添加引用System.Windows.Forms
ASP.NET根据URL生成网页缩略图示例程序(C#语言)的更多相关文章
- 一步一步搭建客服系统 (3) js 实现“截图粘贴”及“生成网页缩略图”
最近在做一个客服系统的demo,在聊天过程中,我们经常要发一些图片,而且需要用其它工具截图后,直接在聊天窗口里粘贴,就可以发送:另外用户输入一个网址后,把这个网址先转到可以直接点击的link,并马上显 ...
- 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)
import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...
- Asp.Net 上传图片并生成高清晰缩略图
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...
- Asp.Net 上传图片并生成高清晰缩略图(转)
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...
- Asp.Net MVC路由生成URL过程
这次谈一谈Asp.Net MVC中所学到的路由生成URL的相关技术,顺便提一提遇到的一些坑,真的是掉坑掉多了,也就习以为常了,大不了从坑里再爬出来.初学者,包括我,都以为,mvc的核心是模型视图控制器 ...
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase
原文地址:http://www.51csharp.com/MVC/882.html ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...
- ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用
本片文章翻译自ABP在CodeProject上的一个简单示例程序,网站上的程序是用ABP之前的版本创建的,模板创建界面及工程文档有所改变,本文基于最新的模板创建.通过这个简单的示例可以对ABP有个更深 ...
- Spring MVC-集成(Integration)-生成RSS源示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_rss_feed.htm 说明:示例基于Spring MVC 4.1.6. 以下示 ...
- .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序
在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...
随机推荐
- react-native —— 在Windows下搭建React Native Android开发环境
在Windows下搭建React Native Android开发环境 前段时间在开发者头条收藏了 @天地之灵_邓鋆 分享的<在Windows下搭建React Native Android开发环 ...
- 基于asp.net+MINIUI的项目----在线学习系统
1 数据库列的自动计算: 描述:一张选课表,其中有学习的开始时间和结束时间,一个列用来计算学习的总时间(小时) 解决:选择该列 属性:计算列规范:公式:(datediff(hour,[StartTim ...
- Mina、Netty、Twisted一起学(九):异步IO和回调函数
用过JavaScript或者jQuery的同学都知道,JavaScript特别是jQuery中存在大量的回调函数,例如Ajax.jQuery的动画等. $.get(url, function() { ...
- Unity3D 中的三个Update()方法
MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当Mo ...
- Network - SSH
SSH(Secure Shell) https://wiki.wireshark.org/SSH SSH 协议与OpenSSH详解 http://my.oschina.net/liting/blo ...
- 【转】 制作Android Demo GIF:程序演示效果GIF图录制
在平时写博客或者分享自己写的程序效果的时候经常需要做成GIF图,以下就是介绍几种常用的GIF录制方法: 一.录制工具 1.(生成动画的工具:Ulead GIF Animator),可以讲单独的图片生成 ...
- 使用git提交中删除idea
https://segmentfault.com/q/1010000000720031 http://www.tuicool.com/articles/a6Nf63F 先有项目,然后分享至github ...
- [转]virtualenv建立多个Python独立开发环境
不同的人喜欢用不同的方式建立各自的开发环境,但在几乎所有的编程社区,总有一个(或一个以上)开发环境让人更容易接受. 使用不同的开发环境虽然没有什么错误,但有些环境设置更容易进行便利的测试,并做一些重复 ...
- HtmlAgilityPack 处理通配的contains
//选择不包含class属性的节点 var result = node.SelectNodes(".//span[not(@class)]"); //选择不包含class和id属性 ...
- 策略模式(Stategy Pattern)
知识点 找出引用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 针对接口编程,而不是实现编程. 多用组合,少用继承 示例