2011-03-17 94 views
4

我有一個網頁源代碼的字符串; 如何將其保存在字節[]中作爲圖像?如何將網頁保存爲圖片

+0

你的意思'的byte []'?並且是圖片的網址,還是您需要呈現的網站? – Aidiakapi 2011-03-17 10:36:28

+0

是字節,我有一個網頁的HTML洞。作爲Pagesource保存在sprint中。我需要從它製作一個圖像並保存爲ms sql數據庫中的字節[]。 – 2011-03-17 10:38:00

+0

http://stackoverflow.com/questions/2434156/webbrowser-drawtobitmap-or-other-methods – 2011-03-17 10:40:23

回答

4

這裏:http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx

是如何使用WebBrowser.DrawToBitmap方法的例子。

生成位圖後,可以使用任何您想要的編碼進行壓縮。
這是從MSDN如何壓縮到PNG(無損和小)的例子:
How to: Encode and Decode a PNG Image

祝你好運:)

編輯:
爲了得到字節數組,你可能希望將內存流用作輸出流。

這裏有一個如何類似的東西會工作的工作例如:

public static void Main(string[] args) 
{ 
    byte[] test = new byte[] { 2, 5, 6, 1, 9 }; 
    MemoryStream ms = new MemoryStream(); 
    ms.Write(test, 0, 5); 

    byte[] image = new byte[ms.Length]; 
    Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length); 
    for (int i = 0; i < ms.Length; i++) 
     Console.WriteLine(image[i]); 
    Console.ReadKey(); 
} 

這裏有一個如何它會在你的情況的示例:

public static void Main(string[] args) 
{ 
    MemoryStream ms = new MemoryStream(); 
    // You have a PNGBitmapEncoder, and you call this: 
    encoder.Save(ms); 

    byte[] image = new byte[ms.Length]; 
    Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length); 
    for (int i = 0; i < ms.Length; i++) 
     Console.WriteLine(image[i]); 
    Console.ReadKey(); 
} 
2

你可以看看following articlethis one也說明了截取網頁截圖並將其保存爲圖片的一種方式。 WPFChromium組件還允許您在不依賴Internet Explorer的情況下實現此目的。

+2

有趣的項目參考。 – 2011-03-17 10:49:10