网页截图 webbrowser 内存溢出

xswwsx1234567 2010-06-14 06:14:53
我在用webbrowser 进行网页截图,每截一次,iis进程所占的内存就会增加,直至最后内存溢出 报“引发类型为 system.outofmemoryexception 的异常” 网上查了许多的资料,还是没解决,大家有什么好的方法吗。
...全文
171 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xswwsx1234567 2010-06-14
  • 打赏
  • 举报
回复
自己顶
xswwsx1234567 2010-06-14
  • 打赏
  • 举报
回复
WebPreviewBase 这个类的部分代码

 internal class WebPreviewBase : IDisposable
{
// Fields
private bool _fullpage = false;
private int _thumbH = 0x300;
private int _thumbW = 0x400;
private Uri _uri= new Uri("about:blank");
private WebBrowser _wb = new WebBrowser();
private int h;
private int w;

// 构造函数
public WebPreviewBase(Uri uri, int thumbW, int thumbH, bool fullpage)
{
this._wb.ScriptErrorsSuppressed = false;
this._wb.ScrollBarsEnabled = false;
this._wb.Size = new Size(0x400, 0x300);
this._wb.NewWindow += new CancelEventHandler(this.CancelEventHandler);
this._wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.DocCompletedEventHandler);
this._thumbW = thumbW;
this._thumbH = thumbH;
this._uri = uri;
this._fullpage = fullpage;
}

public Bitmap GetWebPreview()
{
Bitmap Bmp3;
try
{
object o = this._wb.ActiveXInstance;
this._wb.GoBack();
this.InitComobject();
Snapshot snap = new Snapshot();
Bitmap thumBitmap = (Bitmap) ImageLibrary.ResizeImageToAFixedSize(snap.TakeSnapshot(this._wb.ActiveXInstance, new Rectangle(0, 0, this.w, this.h)), this._thumbW, this._thumbH, ImageLibrary.ScaleMode.W);
Bmp3 = thumBitmap;
}
catch (Exception ex)
{
throw ex;
}
return Bmp3;
}

public void Dispose()
{
this._wb.Dispose();
}
xswwsx1234567 2010-06-14
  • 打赏
  • 举报
回复
aspx.cs里
bitmap bmp
bmp = Okpower.Utility.WebPreview.GetWebPreview(new Uri(url), 0x493e0, 800, 600, true);

Okpower.Utility.WebPreview 这个类的代码如下

namespace Okpower.Utility
{
public class WebPreview
{
// Fields
private Bitmap _bitmap;
private Exception _ex;
private bool _fullPage;
private int _height;
private int _timeout;
private Uri _uri;
private int _width;

// Methods
// 实现基类的构造函数
private WebPreview(Uri uri)
: this(uri, 0x7530, 200, 150, true)
{
}

//构造函数
private WebPreview(Uri uri, int timeout, int width, int height, bool fullPage)
{
this._uri = null;
this._ex = null;
this._bitmap = null;
this._timeout = 0x7530;
this._width = 200;
this._height = 150;
this._fullPage = true;
this._uri = uri;
this._timeout = timeout;
this._width = width;
this._height = height;
this._fullPage = fullPage;
}

// 启动单线程单元 获得位图
internal Bitmap GetWebPreview()
{
Thread t = new Thread(new ParameterizedThreadStart(WebPreview.StaRun));
t.SetApartmentState(ApartmentState.STA);
t.Start(this);
if (!t.Join(this._timeout))
{
t.Abort();
throw new TimeoutException();
}
if (this._ex != null)
{
throw this._ex;
}
if (this._bitmap == null)
{
throw new ExecutionEngineException();
}
return this._bitmap;
}

//
public static Bitmap GetWebPreview(Uri uri)
{
WebPreview wp = new WebPreview(uri);
return wp.GetWebPreview();
}

public static Bitmap GetWebPreview(Uri uri, int timeout, int width, int height, bool fullPage)
{
WebPreview wp = new WebPreview(uri, timeout, width, height, fullPage);
return wp.GetWebPreview();
}

private static void StaRun(object _wp)
{
WebPreview wp = (WebPreview)_wp;
try
{
wp._bitmap = new WebPreviewBase(wp._uri, wp._width, wp._height, wp._fullPage).GetWebPreview();
}
catch (Exception ex)
{
wp._ex = ex;
}
}
}

还有好几个类,太长了 ,整个是一个类库,我在aspx 里调用截图的时候,只用到类库这样的的一个方法去获得bitmap对象
bmp = Okpower.Utility.WebPreview.GetWebPreview(new Uri(url), 0x493e0, 800, 600, true);
mngzilin 2010-06-14
  • 打赏
  • 举报
回复
上代码。

62,041

社区成员

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

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

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

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