如何使用C#捕获特定网址的图片?

[英]How to use C# to capture a image of a specific url?


How to use C# to capture a image of a specific url?

如何使用C#捕获特定网址的图片?

I want to use C# to automatically capture a image of a webpage based on a specific url.

我想使用C#自动捕获基于特定网址的网页图像。

For example, I have a page contains a txtUrl.Text = "http://www.some.com/index.aspx" , then I click a button, how can I capture a image of that Url?

例如,我有一个包含txtUrl.Text =“http://www.some.com/index.aspx”的页面,然后我点击一个按钮,我该如何捕获该Url的图像?

3 个解决方案

#1


I assume you want to do this from ASP.NET (as opposed to from a WinForms application).

我假设你想从ASP.NET(而不是从WinForms应用程序)这样做。

In your web project, add a reference to System.Windows.Forms (yes, this is a bad thing to do). In your code-behind, you can then create an object of type System.Windows.Forms.WebBrowser:

在您的Web项目中,添加对System.Windows.Forms的引用(是的,这是一件坏事)。在您的代码隐藏中,您可以创建System.Windows.Forms.WebBrowser类型的对象:

WebBrowser browser = new WebBrowser();
// this will load up a URL into the web browser:
browser.Navigate(@"http://www.stackoverflow.com"); 

Next, just use the BitBlt API function (sorry, I don't have a link handy) to copy the WebBrowser control's graphical display to a Bitmap (which you can then display or save or whatever). With this function, the WebBrowser's Handle property is one of the parameters to pass.

接下来,只需使用BitBlt API函数(抱歉,我没有方便的链接)将WebBrowser控件的图形显示复制到Bitmap(然后可以显示或保存或其他)。使用此函数,WebBrowser的Handle属性是要传递的参数之一。

Update: here's a link to some code that does exactly what you need: http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/

更新:这里是一些代码的链接,可以完全满足您的需求:http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/

#2


If you mean a visual of the webpage, one approach is to integrate IE to your application and programmatically taking a screenshot. This (for the integrated web browser) and this (for taking screenshots with C#) may be of use. This is of course IE dependent.

如果您的意思是网页的视觉效果,一种方法是将IE集成到您的应用程序并以编程方式截取屏幕截图。这(对于集成的Web浏览器)和此(用于使用C#截取屏幕截图)可能是有用的。这当然是IE依赖的。

Another option is using the shotserver and shotfactory projects used for browsershots.org. They can be found here, though I'm not sure if there's a .NET API for it.

另一种选择是使用用于browsershots.org的shotserver和shotfactory项目。它们可以在这里找到,但我不确定它是否有.NET API。

#3


I don't think that is really possible only using C#. That is because C#, or the .NET framework for that matter, don't offer any kind of HTML markup rendering capabilities. The closest you can get - in my opinion - would be to use a WebBrowser control and then try to somehow capture it's graphical output (which would be the rendered page).

我不认为只有使用C#才有可能。这是因为C#或.NET框架不提供任何类型的HTML标记呈现功能。在我看来,你能得到的最接近的是使用WebBrowser控件,然后尝试以某种方式捕获它的图形输出(这将是渲染页面)。

The other way to do it would be to look for a .NET component that might do what you want.. Although I don't know of any that do.

另一种方法是寻找可能做你想要的.NET组件。虽然我不知道有什么用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/10/bfcd62604b11e6ec72ff2b10542ec735.html



 
  © 2014-2022 ITdaan.com 联系我们: