将站内某页面转换成图片遇到的问题。

newworld826 2010-10-27 04:29:54
问题是这样的,我用表格和Label来显示从数据库中查询出来的数据,然后在该页面上添加一个按钮,点击该按钮后将该页内容转换成图片(JPG、GIF、PNG,当然最好是PDF只是目前没找到方法)。下面是我在网上查到的别人写的一个转换方法。使用后,能将网上的转换,但是把代码中的 网址(http://www.google.com.hk) 换成我站内的网页的 虚拟路径(例如:"~/Default.aspx")时,就报错“未将对象引用设置到对象的实例。”。我是最近才开始接触.NET,许多地方都还不清楚,还请各位高手多多指点,是这个方法不能实现我想要的功能,还是不能将网址改成站内的动态网页?如果有更好的办法还请详细讲解。多谢了!
代码如下:
.cs基类文件里的:

using System;
using System.Data;
using System.Configuration;
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;
using System.Threading;
using System.Windows.Forms;
namespace TestWebApp
{
public class WebSiteThumbnail
{
Bitmap m_Bitmap;
string m_Url;
int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
m_Url = Url;
m_BrowserHeight = BrowserHeight;
m_BrowserWidth = BrowserWidth;
m_ThumbnailWidth = ThumbnailWidth;
m_ThumbnailHeight = ThumbnailHeight;
}
public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate(m_Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
}
}
}



.aspx.cs里的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TestWebApp;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("http://www.google.com.hk", 600, 600, 600, 600);
MemoryStream ms = new MemoryStream();
m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可
byte[] buff = ms.ToArray();
Response.BinaryWrite(buff);
}
}




请多多讲解指点,感激不尽!
...全文
296 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
newworld826 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jlds123 的回复:]
在主机上装个虚拟打印机,然后调用虚拟打印机,可以转换成PDF,网上有例子
[/Quote]
我在网上查了一下,上面说是将word文档等转换成pdf格式,请问,.aspx类型的动态网页也可以转吗?
symbol_bc 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 newworld826 的回复:]
引用 4 楼 symbol_bc 的回复:
引用 3 楼 newworld826 的回复:
引用 1 楼 tangserver 的回复:
前面要加虚拟路径那些吧

是这样?"~/Default.aspx"


路径应该是http://localhost/yourwebsite/default.aspx这种


多谢你的提醒,路径应该是这样的,但现在的新问题是:我要转换的页面是动态……
[/Quote]

什么意思,什么叫动态页面
jlds123 2010-10-27
  • 打赏
  • 举报
回复
在主机上装个虚拟打印机,然后调用虚拟打印机,可以转换成PDF,网上有例子
newworld826 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 symbol_bc 的回复:]
引用 3 楼 newworld826 的回复:
引用 1 楼 tangserver 的回复:
前面要加虚拟路径那些吧

是这样?"~/Default.aspx"


路径应该是http://localhost/yourwebsite/default.aspx这种
[/Quote]
多谢你的提醒,路径应该是这样的,但现在的新问题是:我要转换的页面是动态.aspx页面,所以还是不成功,报错。。。
newworld826 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 symbol_bc 的回复:]
楼主我测试了你的代码,完全没有问题,你是在哪里报的“未将对象引用设置到对象的实例”
[/Quote]
代码是没问题,请问你网址那部分是怎么写的?我需要的是将自己站内的表单转换成图片。
symbol_bc 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 newworld826 的回复:]
引用 1 楼 tangserver 的回复:
前面要加虚拟路径那些吧

是这样?"~/Default.aspx"
[/Quote]

路径应该是http://localhost/yourwebsite/default.aspx这种
newworld826 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangserver 的回复:]
前面要加虚拟路径那些吧
[/Quote]
是这样?"~/Default.aspx"
symbol_bc 2010-10-27
  • 打赏
  • 举报
回复
楼主我测试了你的代码,完全没有问题,你是在哪里报的“未将对象引用设置到对象的实例”
tangserver 2010-10-27
  • 打赏
  • 举报
回复
前面要加虚拟路径那些吧

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧