ASP.NET实现根据URL生成网页缩略图的方法_实用技巧

本文实例讲述了ASP.NET实现根据URL生成网页缩略图的方法。分享给大家供大家参考,具体如下:

工作中需要用到根据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[0];
   try
   {
    GetImage thumb = new GetImage(url, 1024, 768, 800, 600);
    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(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;
   }
  }
 }
}

PS:项目中需要添加引用System.Windows.Forms

希望本文所述对大家asp.net程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索asp.net
, url
, 生成
网页缩略图
,以便于您获取更多的相关知识。

时间: 2024-08-02 18:31:04

ASP.NET实现根据URL生成网页缩略图的方法_实用技巧的相关文章

ASP.NET实现上传图片并生成缩略图的方法_实用技巧

本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法.分享给大家供大家参考,具体如下: protected void bt_upload_Click(object sender, EventArgs e) { //检查上传文件的格式是否有效 if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { Response.Write("上传图片格式无效!"); re

asp.net中Table生成Excel表格的方法_实用技巧

本文实例讲述了asp.net中Table生成Excel表格的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: <!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/19

asp.net利用后台实现直接生成html分页的方法_实用技巧

本文实例讲述了asp.net利用后台实现直接生成html分页的方法,是一个比较实用的功能.分享给大家供大家参考之用.具体方法如下: 1.建立存储过程: ALTER procedure [dbo].[p_news_query] @Page int as begin select top 5 new_id,new_title,new_url,new_content_text,create_time,user_name from (select *,ROW_NUMBER() over(order by

asp.net简单生成XML文件的方法_实用技巧

本文实例讲述了asp.net简单生成XML文件的方法.分享给大家供大家参考,具体如下: 方式一:直接使用DataSet SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=sa;Database=northwind;Persist Security Info=True"; conn.Open(); SqlDataAdapter da

.net等比缩放生成缩略图的方法_实用技巧

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 /// </summary> /// <param name="phyPath">原图片的路径</param> /// <param name="width">缩略图宽</param> /// <param name=&quo

ASP.NET创建动态缩略图的方法_实用技巧

本文实例讲述了ASP.NET创建动态缩略图的方法.分享给大家供大家参考.具体分析如下: 提示: 1. 导入 System.IO 2. 创建 类C lass "CreateThumbnails" or any class and place following function inside that class You need one function to response call back to main function Function ImageAbortDummyCal

ASP.NET MVC使用Ajax的辅助的解决方法_实用技巧

前言:前面我们已经简单的介绍过了MVC如何Jquery,因为我们如果使用Ajax的话必须要了解Jquery,这篇博客我们将大致了解一下ASP.NET MVC如何使用Ajax的辅助方法,此博客是我的读书笔记,如果那里写的不好,还请各位朋友提出来,我们共同学习.1.准备工作 (1)在MVC刚开始学习的时候,我们就需要介绍ASP.NET MVC框架中的HTML的辅助方法,但是这类文章现在已经很多了,而且个人感觉很简单,所以没有写笔记,我在这里就不介绍了. (2)ASP.NET MVC框架中的HTML辅

asp.net不同页面间数据传递的多种方法_实用技巧

1. Get(即使用QueryString显式传递)方式:在url后面跟参数.特点:简单.方便.缺点:字符串长度最长为255个字符:数据泄漏在url中.适用数据:简单.少量.关键的数据.适用范围:传递给自己.传递给另一个目标页面:常用于2个页面间传递数据.用法:例如:url后加?UserID=-,跳转到目标页面,目标页面在伺服端可用Request.QueryString["InputText"]获取其指定参数值. 2. Post方式:通用的方式.利用form提交.特点:最常用的方法.常

Asp.Net实现404页面与301重定向的方法_实用技巧

本文实例讲述了Asp.Net实现404页面与301重定向的方法.分享给大家供大家参考.具体实现方法如下: 从一种程度来讲301重定向与404页面没什么关系为什么我要拿到一起来讲来,因为都很简单实现,所在我就一起介绍一下了. 如何在 asp.net 中设置404页面的方法记录下来. 下边首先看看之前的设置方法,web.config文件中: 复制代码 代码如下: <configuration>     <system.web>         <customErrors mode