工作中可能马上要用到根据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#语言)的更多相关文章

  1. 一步一步搭建客服系统 (3) js 实现“截图粘贴”及“生成网页缩略图”

    最近在做一个客服系统的demo,在聊天过程中,我们经常要发一些图片,而且需要用其它工具截图后,直接在聊天窗口里粘贴,就可以发送:另外用户输入一个网址后,把这个网址先转到可以直接点击的link,并马上显 ...

  2. 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)

    import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...

  3. Asp.Net 上传图片并生成高清晰缩略图

    在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...

  4. Asp.Net 上传图片并生成高清晰缩略图(转)

    在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...

  5. Asp.Net MVC路由生成URL过程

    这次谈一谈Asp.Net MVC中所学到的路由生成URL的相关技术,顺便提一提遇到的一些坑,真的是掉坑掉多了,也就习以为常了,大不了从坑里再爬出来.初学者,包括我,都以为,mvc的核心是模型视图控制器 ...

  6. ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase

    原文地址:http://www.51csharp.com/MVC/882.html   ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...

  7. ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用

    本片文章翻译自ABP在CodeProject上的一个简单示例程序,网站上的程序是用ABP之前的版本创建的,模板创建界面及工程文档有所改变,本文基于最新的模板创建.通过这个简单的示例可以对ABP有个更深 ...

  8. Spring MVC-集成(Integration)-生成RSS源示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_rss_feed.htm 说明:示例基于Spring MVC 4.1.6. 以下示 ...

  9. .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序

    在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...

随机推荐

  1. sql 循环处理表数据中当前行和上一行中某值相+/-

    曾经,sql中循环处理当前行数据和上一行数据浪费了我不少时间,学会后才发现如此容易,其实学问就是如此,难者不会,会者不难. 以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的 ...

  2. 在SSIS中的不同组件间使用局部临时表

    Connetion的属性RetainSameConnection是个boolean值,指定是否保持相同的链接,默认值是false,表示每个component都会单独的使用connection,在com ...

  3. Reporting Service 配置SMTP和设置订阅出现的异常

    SSRS能够按照schedule,以mail的形式发送report,这是通过设置subscription report来实现的. 1,发送mail需要在SSRS中配置SMTP Server,如果没有R ...

  4. springMVC robots.txt 处理

    正常情况这样就好使 <mvc:resources mapping="/robots.txt" location="/lib/robots.txt"/> ...

  5. Debugging WebLogic Server Applications Using Eclipse and the WebLogic-Plugin

    http://www.oracle.com/technetwork/cn/tutorials/eclipse-plugin-093411.html

  6. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. Git的奇技淫巧🙈

    Git的奇技淫巧

  8. html/css基础篇——DOM中关于脱离文档流的几种情况分析

    所谓的文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列.并最终窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素.脱离文档流即是元素打乱了这个排列,或是从排版中拿走. ...

  9. SQL Server代理(9/12):理解作业和安全

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这个系列的前一篇文章里,你学习了如何在S ...

  10. Hyperledger中数据存取的实现

    简介 本文介绍了在Hyperledger中数据存取的实现. API接口 Hyperledger提供基于key/value的数据存储,其中key是字符串,value则是二进制字节数组,Hyperledg ...