2012-05-15 58 views

回答

0

這是我如何做到這一點使用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); 
+0

感謝這個,但是我對asp.net很新,並且不知道如何實現這個。這就像一個功能?如何添加要抓取的網址或要保存的位置?對不起,我不知道自己在做什麼,但我正在努力學習很多東西。過去5年來我一直在使用php。 – AmazingChase

+0

這是一個供你在後面的aspx.cs代碼中使用的類。無論您需要創建縮略圖,請創建此類的實例,然後調用GenerateWebSiteThumbnailImage函數。它會傳回一個位圖給你,以你想要的任何格式保存。 –

+0

@AmazingChase抱歉,忘了ping。 –

相關問題