欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-18 20:46:04
本文实例讲述了asp.net实现根据url生成网页缩略图的方法。分享给大家供大家参考,具体如下: 工作中需要用到根据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程序设计有所帮助。

上一篇:

下一篇: