代码之家  ›  专栏  ›  技术社区  ›  Andrew Harry

如何通过via拍摄网站截图。网络代码?

  •  13
  • Andrew Harry  · 技术社区  · 17 年前

    是否可以使用以下工具对任何给定的url进行屏幕截图:。网络代码?

    4 回复  |  直到 17 年前
        1
  •  5
  •   Svish    17 年前
        2
  •  4
  •   John Feminella    17 年前

    我建议使用第三方方法,而不是自己做。但是,如果你坚持,那么严厉的方法就是使用 System.Diagnostics.Process 启动浏览器,然后 GetDesktopImage 来截图。

    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;
    }
    
        3
  •  2
  •   Assaf Lavie    17 年前

    我用过 HTMLSnapshot 在过去,他对它很满意(它不是免费的,但相当便宜)。

    我衷心建议你不要浪费太多时间自己实现这一点,因为你最终一定会重做很多第三方解决方案已经处理过的最终案例。

        4
  •  2
  •   virtualadrian    13 年前

    我到处搜索以找到解决这个问题的方法,使用WebBrowser和其他建议的问题是,你需要大量访问这个盒子。

    http://www.websitesscreenshot.com/

    他们的搜索引擎优化很糟糕,但我相信我是在一个论坛甚至是一堆上找到他们的。。。我有一个许可证,你得到的只是一个你可以引用的DLL。将许可证传递给构造函数将删除其水印。

    在使用ILSpy快速浏览之后,我相信它所做的是解释HTML并转储图像,以满足您的观看乐趣。

    用法

    保存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();
    

    保存原始HTML字符串

    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();
    

    你可以在他们的使用页面上找到更多信息。可在此处找到:

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

    推荐文章