本文实例讲述了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" 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程序设计有所帮助。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月30日
2024年11月30日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]