2009-03-17 39 views
13

Có thể chụp ảnh màn hình của bất kỳ url cụ thể nào sử dụng mã .Net không?Làm cách nào để bạn chụp Ảnh màn hình của một trang web qua mã .Net?

Cách dễ nhất để làm điều đó là gì?

+0

Với công cụ hiển thị nào?Sau khi tất cả, các công cụ dựng hình khác nhau sẽ tạo ra các kết quả khác nhau –

+0

webkit có vẻ là tiêu chuẩn nhất tuân thủ –

+0

wow. didnt nghĩ rằng nó đã được dễ dàng như vậy :) – Karim

Trả lời

3

Tôi khuyên bạn nên sử dụng phương pháp tiếp cận của bên thứ ba cho việc này, chứ không phải tự mình thực hiện. Tuy nhiên, nếu bạn nhấn mạnh, cách xử lý nặng nề là sử dụng System.Diagnostics.Process để khởi chạy trình duyệt, sau đó GetDesktopImage để có ảnh chụp màn hình.

tôi chắc chắn rằng có một cách dễ dàng hơn, nhưng điều đó trông như thế này:

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 

public class PlatformInvokeGDI32 
{ 
    public const int SRCCOPY = 13369376; 

    [DllImport("gdi32.dll",EntryPoint="DeleteDC")] 
    public static extern IntPtr DeleteDC(IntPtr hDc); 

    [DllImport("gdi32.dll",EntryPoint="DeleteObject")] 
    public static extern IntPtr DeleteObject(IntPtr hDc); 

    [DllImport("gdi32.dll",EntryPoint="BitBlt")] 
    public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp); 

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")] 
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,int nWidth, int nHeight); 

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")] 
    public static extern IntPtr CreateCompatibleDC(IntPtr hdc); 

    [DllImport ("gdi32.dll",EntryPoint="SelectObject")] 
    public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp); 
} 

public class PlatformInvokeUSER32 
{ 
    public const int SM_CXSCREEN = 0; 
    public const int SM_CYSCREEN = 1; 

    [DllImport("user32.dll", EntryPoint="GetDesktopWindow")] 
    public static extern IntPtr GetDesktopWindow(); 

    [DllImport("user32.dll",EntryPoint="GetDC")] 
    public static extern IntPtr GetDC(IntPtr ptr); 

    [DllImport("user32.dll",EntryPoint="GetSystemMetrics")] 
    public static extern int GetSystemMetrics(int abc); 

    [DllImport("user32.dll",EntryPoint="GetWindowDC")] 
    public static extern IntPtr GetWindowDC(Int32 ptr); 

    [DllImport("user32.dll",EntryPoint="ReleaseDC")] 
    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc); 
} 

public class CaptureScreen 
{ 
    public static Bitmap GetDesktopImage() 
    { 
     //In size variable we shall keep the size of the screen. 
     SIZE size; 

     //Variable to keep the handle to bitmap. 
     IntPtr hBitmap; 

     //Here we get the handle to the desktop device context. 
     IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow()); 

     //Here we make a compatible device context in memory for screen device context. 
     IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC); 

     //We pass SM_CXSCREEN constant to GetSystemMetrics to get the 
     //X coordinates of the screen. 
     size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN); 

     //We pass SM_CYSCREEN constant to GetSystemMetrics to get the Y coordinates of the screen. 
     size.cy = PlatformInvokeUSER32.GetSystemMetrics (PlatformInvokeUSER32.SM_CYSCREEN); 

     //We create a compatible bitmap of the screen size and using the screen device context. 
     hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy); 

     //As hBitmap is IntPtr, we cannot check it against null. 
     //For this purpose, IntPtr.Zero is used. 
     if(hBitmap != IntPtr.Zero) 
     { 
      //Here we select the compatible bitmap in the memeory device 
      //context and keep the refrence to the old bitmap. 
      IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap); 

      //We copy the Bitmap to the memory device context. 
      PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY); 

      //We select the old bitmap back to the memory device context. 
      PlatformInvokeGDI32.SelectObject(hMemDC, hOld); 

      //We delete the memory device context. 
      PlatformInvokeGDI32.DeleteDC(hMemDC); 

      //We release the screen device context. 
      PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC); 

      //Image is created by Image bitmap handle and stored in local variable. 
      Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); 

      //Release the memory to avoid memory leaks. 
      PlatformInvokeGDI32.DeleteObject(hBitmap); 

      //This statement runs the garbage collector manually. 
      GC.Collect(); 

      //Return the bitmap 
      return bmp; 
     } 

     //If hBitmap is null, retun null. 
     return null; 
    } 
} 

//This structure shall be used to keep the size of the screen. 
public struct SIZE 
{ 
    public int cx; 
    public int cy; 
} 
2

Có một số thành phần của bên thứ 3 mà thực hiện điều này. Rõ ràng nó không phải là tầm thường, vì những thứ như Flash và các công cụ không nắm bắt dễ dàng như vậy, ví dụ.

Tôi đã sử dụng HTMLSnapshot trong quá khứ và hài lòng với nó (nó không miễn phí, nhưng khá rẻ).

Tôi chân thành đề nghị bạn không lãng phí quá nhiều thời gian thực hiện việc này vì bạn bị ràng buộc phải kết thúc quá trình làm lại các giải pháp của bên thứ ba đã xử lý.

+0

Tôi tự hỏi nếu có ai đó tư vấn cho Larry Page và Sergey Brin giống nhau: P –

2

Tôi đã tìm kiếm rất nhiều để tìm ra giải pháp cho vấn đề này và vấn đề với việc sử dụng Trình duyệt Web và các đề xuất khác là bạn cần nhiều quyền truy cập vào hộp.

Cho đến nay, đây là công cụ của bên thứ ba tốt nhất mà tôi đã có thể tìm thấy:

http://www.websitesscreenshot.com/

SEO của họ là khủng khiếp, nhưng tôi tin rằng tôi đã tìm thấy chúng trên một diễn đàn hoặc thậm chí ngăn xếp ... Tôi làm sở hữu một giấy phép và những gì bạn nhận được chỉ là một DLL duy nhất bạn có thể tham khảo. Việc chuyển một giấy phép cho hàm tạo sẽ loại bỏ hình mờ của chúng.

Sau khi xem nhanh ILSpy, tôi tin rằng những gì nó làm là nó diễn giải HTML và đổ một hình ảnh cho niềm vui của bạn.

Cách sử dụng

Tiết kiệm URL

WebsitesScreenshot.WebsitesScreenshot _Obj; 
_Obj = new WebsitesScreenshot.WebsitesScreenshot(); 
WebsitesScreenshot.WebsitesScreenshot.Result _Result; 
_Result = _Obj.CaptureWebpage("http://www.google.com"); 
if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Captured) 
{ 
    _Obj.ImageFormat = WebsitesScreenshot. 
     WebsitesScreenshot.ImageFormats.PNG; 
    _Obj.SaveImage ("c:\\google.png"); 
}    
_Obj.Dispose(); 

Saving liệu HTML Chuỗi

WebsitesScreenshot.WebsitesScreenshot _Obj; 
_Obj=new WebsitesScreenshot.WebsitesScreenshot(); 
WebsitesScreenshot.WebsitesScreenshot.Result _Result; 
_Result = _Obj.CaptureHTML(
"<html><body><h1> Hello world </h1></body></html>"); 
if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Captured) 
{ 
    _Obj.ImageFormat = WebsitesScreenshot. 
     WebsitesScreenshot.ImageFormats.GIF; 
    _Obj.SaveImage("c:\\test.gif"); 
} 
_Obj.Dispose(); 

Bạn có thể tìm thêm trên trang sử của họ. Đã tìm thấy tại đây:

http://www.websitesscreenshot.com/Usage.html

Hy vọng điều này sẽ hữu ích!

Chúc mừng !!

Các vấn đề liên quan