怎么在ASP.NET中根据URL生成网页缩略图

发布时间:2021-02-18 17:04:26 作者:Leah
来源:亿速云 阅读:161

本篇文章为大家展示了怎么在ASP.NET中根据URL生成网页缩略图,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

起始页: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;
   }
  }
 }
}

上述内容就是怎么在ASP.NET中根据URL生成网页缩略图,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. asp.net如何实现生成缩略图及加水印
  2. 怎么在ASP.NET中对URL进行映射

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:如何在asp.net中根据日期计算天数

下一篇:如何在Java中将Date日期类型的字段转换成json字符串

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》