2010-02-23 44 views
15

Làm cách nào để lấy một sceenshot của một trang web được lập trình cho URL làm đầu vào?Chụp ảnh màn hình trang web theo chương trình

Và đây là những gì tôi có cho đến bây giờ:

// The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap) 
Bitmap bitmap = new Bitmap(1024, 768); 
Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768); 
// This is a method of the WebBrowser control, and the most important part 
webBrowser1.DrawToBitmap(bitmap, bitmapRect); 

// Generate a thumbnail of the screenshot (optional) 
System.Drawing.Image origImage = bitmap; 
System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat); 

Graphics oGraphic = Graphics.FromImage(origThumbnail); 
oGraphic.CompositingQuality = CompositingQuality.HighQuality; 
oGraphic.SmoothingMode = SmoothingMode.HighQuality; 
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 
Rectangle oRectangle = new Rectangle(0, 0, 120, 90); 
oGraphic.DrawImage(origImage, oRectangle); 

// Save the file in PNG format 
origThumbnail.Save(@"d:\Screenshot.png", ImageFormat.Png); 
origImage.Dispose(); 

Nhưng điều này không hoạt động. Nó chỉ cho tôi một bức tranh trắng trống. Tôi đang thiếu gì ở đây?

Có cách nào khác để tôi có thể nhận được ảnh chụp màn hình của một trang web theo chương trình không?

+0

Câu hỏi này vừa được hỏi hôm qua, mặc dù chủ yếu hướng đến Perl. Có lẽ một số câu trả lời sẽ giúp bạn, mặc dù rõ ràng sẽ đưa bạn một hướng khác. Đây là [link] (http://stackoverflow.com/questions/2312852/how-can-i-take-screenshots-with-perl). – lundmark

Trả lời

7

tôi đã tìm kiếm và đã tìm kiếm và đã tìm kiếm và tìm thấy nó Webpage thumbnailer (một bài báo The Code Project).

+2

Điều này sử dụng điều khiển trình duyệt web của Microsofts cho phép bạn thường xuyên chụp ảnh màn hình trắng trống – jjxtra

2

Bạn có thể thử gọi hàm gốc PrintWindow.

+1

Bạn có thể giải thích thêm một chút về điều này không? Xin lưu ý rằng tôi chỉ có URL của trang web làm đầu vào. – Manish

3

Vẽ điều khiển trình duyệt thành bitmap có phần không đáng tin cậy. Tôi nghĩ sẽ tốt hơn nếu chỉ quét màn hình cửa sổ của bạn.

using (Bitmap bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height, PixelFormat.Format24bppRgb)) 
using (Graphics graphics = Graphics.FromImage(bitmap)) 
{ 
    graphics.CopyFromScreen(
     PointToScreen(webBrowser1.Location), 
     new Point(0, 0), 
     bitmapSize); 
     bitmap.Save(filename); 
} 
+3

Cách tiếp cận này sẽ không hoạt động trong Ứng dụng Console, phải không? –

0

Bạn cũng có thể thử P/Gọi BitBlt() từ gdi32.dll. Hãy thử mã này:

Graphics mygraphics = webBrowser1.CreateGraphics(); 
Size s = new Size(1024, 768); 
Bitmap memoryImage = new Bitmap(s.Width, s.Height, mygraphics); 
Graphics memoryGraphics = Graphics.FromImage(memoryImage); 
IntPtr dc1 = mygraphics.GetHdc(); 
IntPtr dc2 = memoryGraphics.GetHdc(); 
// P/Invoke call here 
BitBlt(dc2, 0, 0, webBrowser1.ClientRectangle.Width, webBrowser1.ClientRectangle.Height, dc1, 0, 0, 13369376); 
mygraphics.ReleaseHdc(dc1); 
memoryGraphics.ReleaseHdc(dc2); 
memoryImage.Save(filename); 

P/Invoke sẽ là:

[DllImport("gdi32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
internal static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop); 
Các vấn đề liên quan