使用WebBrowser GDI+中发生一般性错误 对COM 组件的调用返回了错误HRESULT E_FAIL

Xuon 2010-10-12 06:09:19
发布的网站上,页面错误提示:“GDI+中发生一般性错误。”, 谁遇到过?
在本地测试没问题,发布到服务器上就有这个问题了。参考过http://www.cnblogs.com/wudingfeng/archive/2008/07/24/1250564.html,逐一排查未解决。

这个是用WebBrowser保存网页图片,应用在ASP.NET网站项目中出现的问题。

在自己的测试服务器上抛出的异常信息是“对COM 组件的调用返回了错误 HRESULT E_FAIL”。
参考过:http://www.cnblogs.com/chenjiang1014/archive/2008/12/25/1362597.html及CSDN相关帖子,未解决。但是这种异常在测试服务器上出现的是这种情况:在重启IIS或者服务器之后,不会再出现任何异常,开始可以截图,之后就得不到网页的截图,还没有任何异常。


贴代码:
来源http://blog.csdn.net/mngzilin/archive/2010/04/22/5517879.aspx

//类GetSnap:
//======================================

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.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;

/// <summary>
/// GetSnap 的摘要说明
/// </summary>

public class GetSnap
{
private string MyURL;

public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}

public GetSnap(string WebSite/*, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight*/)
{
this.WebSite = WebSite;
}

public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite/*, this.ScreenWidth, this.ScreenHeight*/);
Shot.GetIt();
//Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
Bitmap Pic = Shot.DrawBitmap();
return Pic;
}
}

class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;

public WebPageBitmap(string url/*, int width, int height*/)
{
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
}

public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}

this.Height = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollHeight"));
this.Width = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollwidth"));
MyBrowser.Size = new Size(this.Width, this.Height);
}

public Bitmap DrawBitmap(/*int theight, int twidth*/)
{
int theight=this.Height;
int twidth = this.Width;
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return (Bitmap)oThumbNail;
}
catch (Exception ex)
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
//===========================================

//在web中截图:

//新建test.aspx页面,引入命名空间using System.Drawing.Imaging;using System.Threading;

//添加button1事件:

protected void Button1_Click(object sender, EventArgs e)
{

Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
NewTh.Start();

}

//添加回调函数:

///// 捕获屏幕
public void CaptureImage()
{
//你的任务代码
try
{

string url=http://www.baidu.com;

GetSnap thumb = new GetSnap(url);
System.Drawing.Bitmap x = thumb.GetBitmap();//获取截图
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");

x.Save(Server.MapPath("SnapPic/" + FileName + ".jpg"));//保存截图到SnapPic目录下

}
catch (Exception ex)
{
}
}

...全文
2203 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xuon 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 n_ithero 的回复:]
对COM 组件的调用返回了错误 HRESULT E_FAIL这个错误还没找到解决办法。
[/Quote]
重启IIS问题解决。
Xuon 2010-10-16
  • 打赏
  • 举报
回复
对COM 组件的调用返回了错误 HRESULT E_FAIL这个错误还没找到解决办法。
Xuon 2010-10-16
  • 打赏
  • 举报
回复
GDI+中发生一般性错误。”,我这边在本地测试没这个问题,而发布到服务器上有这个错误(负责发布的是另外一个人),是因为发布的时候用的是虚拟目录,与本地测试的时候情况不一样。(也就是路劲问题
这个异常的原因,以下链接基本囊括了:http://www.cnblogs.com/wudingfeng/archive/2008/07/24/1250564.html
q107770540 2010-10-12
  • 打赏
  • 举报
回复
关 注
叶子 2010-10-12
  • 打赏
  • 举报
回复
在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常。
于是开始“摆渡”,并寻找到了解决办法:赋予 NETWORK SERVICE 帐户以写权限。

在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三种原因:
1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
3. 保存的文件已存在并因某种原因被锁定。
解决方法:
重启IIS,解除锁定。并在代码中使用 using 语句,确保释放 Image 对象所使用的所有资源。

全部信息:http://www.cnblogs.com/lgzslf/archive/2009/03/16/1412878.html
wuyq11 2010-10-12
  • 打赏
  • 举报
回复
webbrower截图
http://topic.csdn.net/u/20090702/11/845f5ac5-48f2-463a-8271-47c5eeb33cab.html
相应的帐户没有写权限。
指定的物理路径不存在。
在代码中使用 using 语句,释放 Image 对象所使用的所有资源。
文件名长度
img.Save(path, ImageFormat.Jpeg);
img.Dispose();
kke_007 2010-10-12
  • 打赏
  • 举报
回复
帮顶/........

62,046

社区成员

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

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

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

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