从 WebBrowser 控件保存图像
运行以下代码,但生成的位图向下移动约半英寸并在底部截止。我检查了图像的宽度和高度,它正在创建正确尺寸的图像,只是图像内容向下移动并被截断。我很困惑...有什么想法吗?
using mshtml;
using System.Drawing;
using System.Runtime.InteropServices;
[ComImport, InterfaceType((short)1), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")]
private interface IHTMLElementRenderFixed
{
void DrawToDC(IntPtr hdc);
void SetDocumentPrinter(string bstrPrinterName, IntPtr hdc);
}
public Bitmap GetImage(string id)
{
HtmlElement e = webBrowser1.Document.GetElementById(id);
IHTMLImgElement img = (IHTMLImgElement)e.DomElement;
IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img;
Bitmap bmp = new Bitmap(img.width, img.height);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
render.DrawToDC(hdc);
g.ReleaseHdc(hdc);
return bmp;
}
The following code runs, but the Bitmap generated is shifted down about half an inch and cutoff at the bottom. I checked the image width and height and it is creating an image of the correct size, just the image contents are shifted down and cutoff. I'm stumped... any ideas?
using mshtml;
using System.Drawing;
using System.Runtime.InteropServices;
[ComImport, InterfaceType((short)1), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")]
private interface IHTMLElementRenderFixed
{
void DrawToDC(IntPtr hdc);
void SetDocumentPrinter(string bstrPrinterName, IntPtr hdc);
}
public Bitmap GetImage(string id)
{
HtmlElement e = webBrowser1.Document.GetElementById(id);
IHTMLImgElement img = (IHTMLImgElement)e.DomElement;
IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img;
Bitmap bmp = new Bitmap(img.width, img.height);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
render.DrawToDC(hdc);
g.ReleaseHdc(hdc);
return bmp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所做的就是渲染图像,就像浏览器以所有样式渲染图像一样。不知道这是否是你真正想要的?如果您只想下载图像,那么使用网络请求来解决它会更容易,如其他答案中所述。
如果您确实想要渲染,第一步就是更改
为“
现在您将获得完整渲染的网络浏览器图像”。
如果您想要一个完美的解决方案,即使是大图像,您也必须滚动并逐块获取图像。
What you are doing is rendering the image as it is rendered by the browser with all the styles. I don't know if this is what your realy want? If you only want to download the image then it is easier to solve it with a web request as described in hte other answers.
If you realy want to render than the first step is to change
to
Now you get the complete rendered web browser image.
If you want a perfect solution even for big images you also have to scroll around and get the image tile by tile.
首先,我相信您获得的 img 元素的大小与实际图像大小不同。
其次,为什么不直接使用 System.Net.WebRequest 并从 URL 下载实际图像。您已经有了 URL 并且已经有了 IMG 元素信息,事实上,如果您不显示网络浏览器,请尝试使用 System.Net.WebRequest,这样您就可以验证您获得的内容类型是实际图像还是占位符。
http://msdn.microsoft.com/en-us/library /system.net.webrequest.aspx
to start with I believe the img element you getting has a different size other than what is the actual image size.
Secondly, why don't you use direct
System.Net.WebRequest
and download the actual image from the URL. you already have the URL and Already have the IMG element information, infact if you not show the webbrowser try using System.Net.WebRequest, this way you can verify what content type you getting is it an actual image or place holder.http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx
如果您有地址,可以使用以下方式保存:
当客户端是
System.Net.WebClient
If you have the address, you can save it using:
When client is a
System.Net.WebClient