如何使用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 联系我们: