0
我有一個頁面,動態生成一個小的HTML頁面,其中包含1個小表w /文本。我希望能夠拍攝該頁面的圖片(png優先)並將其保存到我的服務器。如何創建網頁圖像並使用.net將圖像保存到我的服務器?
我以前使用第三方解決方案(ABCdrawHTML2),但我已經改變了服務器,這個沒有它。有沒有第三方解決方案做到這一點?
我有一個頁面,動態生成一個小的HTML頁面,其中包含1個小表w /文本。我希望能夠拍攝該頁面的圖片(png優先)並將其保存到我的服務器。如何創建網頁圖像並使用.net將圖像保存到我的服務器?
我以前使用第三方解決方案(ABCdrawHTML2),但我已經改變了服務器,這個沒有它。有沒有第三方解決方案做到這一點?
這是我如何做到這一點使用WebBrowser控件Windows.Forms的:
public class WebSiteThumbnailImage
{
string m_Url;
int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
Bitmap m_Bitmap = null;
public WebSiteThumbnailImage(string url, int browserWidth, int browserHeight, int thumbnailWidth, int thumbnailHeight)
{
m_Url = url;
m_BrowserWidth = browserWidth;
m_BrowserHeight = browserHeight;
m_ThumbnailWidth = thumbnailWidth;
m_ThumbnailHeight = thumbnailHeight;
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate(m_Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
}
}
要在您的代碼隱藏使用此功能,在適當的地方,這樣做:
WebSiteThumbnailImage thumbnail = new WebSiteThumbnailImage(url, 1000, 1000, 200, 200);
Bitmap image = thumbnail.GenerateWebSiteThumbnailImage();
image.Save(filePath);
感謝這個,但是我對asp.net很新,並且不知道如何實現這個。這就像一個功能?如何添加要抓取的網址或要保存的位置?對不起,我不知道自己在做什麼,但我正在努力學習很多東西。過去5年來我一直在使用php。 – AmazingChase
這是一個供你在後面的aspx.cs代碼中使用的類。無論您需要創建縮略圖,請創建此類的實例,然後調用GenerateWebSiteThumbnailImage函數。它會傳回一個位圖給你,以你想要的任何格式保存。 –
@AmazingChase抱歉,忘了ping。 –