!!对指定的url网页进行截图(支持滚屏的网页)!!

时间:2021-10-11 15:22:06
写一个对指定网址截图的程序,网上找能找到不少。
虽然我找到了几个能成功运行的,但是我发现这些程序普遍不支持滚屏。
比如我截取百度首页成一张图片没有什么问题,因为百度的首页高度比较小,不滚屏就可以截取全图。
但是如果我对网易或者其它的网站截图就比较难,只能截取一屏幕的图片,即时我规定了高度也不行,一屏幕以下的全部是空白的。

请问asp.net(c#)对这样的功能如何实现( 能滚屏),
谢谢!

7 个解决方案

#1


up

#2


以前看到过一网站,直接在网页上拉框,保存一个区域作为自己的定制的页面,非常炫的功能.网址忘了,有知道的说一下.现在想来研究研究他们怎么实现的

#3


给你个Winform下的例子:
WINFORM把一个网页保存成图片

使用

Bitmap MyImage = Test.GetControlScrollImage(new Uri(@"http://www.sina.com.cn"), 1024); 
MyImage.Save(@"C:\1.BMP"); 
MyImage.Dispose();
使用到的类

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

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

namespace Print 

    public class Test 
    { 
        public static Bitmap GetHtmlImage(Uri UrlString,int Width) 
        { 
            WebBrowser MyControl = new WebBrowser(); 
            MyControl.Size = new Size(Width, 10); 
            MyControl.Url = UrlString; 
            while (MyControl.ReadyState != WebBrowserReadyState.Complete) 
            { 
                Application.DoEvents(); 
            } 
            
           
            MyControl.Height= MyControl.Document.Body.ScrollRectangle.Height+20; 
            MyControl.Url = UrlString; 
            WebControlImage.Snapshot snap = new WebControlImage.Snapshot(); 
            Bitmap MyImage= snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(0, 0, MyControl.Width, MyControl.Height)); 

            MyControl.Dispose(); 

            return MyImage; 
             
        }       
        /// <summary> 
        /// WebBrowser获取图形 
        /// </summary> 
        private class WebControlImage 
        { 
            internal static class NativeMethods 
            { 
                [StructLayout(LayoutKind.Sequential)] 
                public sealed class tagDVTARGETDEVICE 
                { 
                    [MarshalAs(UnmanagedType.U4)] 
                    public int tdSize; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdDriverNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdDeviceNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdPortNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdExtDevmodeOffset; 
                } 

                [StructLayout(LayoutKind.Sequential)] 
                public class COMRECT 
                { 
                    public int left; 
                    public int top; 
                    public int right; 
                    public int bottom; 
                    public COMRECT() 
                    { 
                    } 

                    public COMRECT(Rectangle r) 
                    { 
                        this.left = r.X; 
                        this.top = r.Y; 
                        this.right = r.Right; 
                        this.bottom = r.Bottom; 
                    } 

                    public COMRECT(int left, int top, int right, int bottom) 
                    { 
                        this.left = left; 
                        this.top = top; 
                        this.right = right; 
                        this.bottom = bottom; 
                    } 

                    public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height) 
                    { 
                        return new NativeMethods.COMRECT(x, y, x + width, y + height); 
                    } 

                    public override string ToString() 
                    { 
                        return string.Concat(new object[] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom }); 
                    } 

                } 

                [StructLayout(LayoutKind.Sequential)] 
                public sealed class tagLOGPALETTE 
                { 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short palVersion; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short palNumEntries; 
                } 
            } 

#4




            public class Snapshot 
            { 
                /// <summary> 
                /// 取快照 
                /// </summary> 
                /// <param name="pUnknown">Com 对象</param> 
                /// <param name="bmpRect">图象大小</param> 
                /// <returns></returns> 
                public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect) 
                { 
                    if (pUnknown == null) 
                        return null; 
                    //必须为com对象 
                    if (!Marshal.IsComObject(pUnknown)) 
                        return null; 
                    //IViewObject 接口 
                    UnsafeNativeMethods.IViewObject ViewObject = null; 
                    IntPtr pViewObject = IntPtr.Zero; 
                    //内存图 
                    Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height); 
                    Graphics hDrawDC = Graphics.FromImage(pPicture); 
                    //获取接口 
                    object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown), 
                        ref UnsafeNativeMethods.IID_IViewObject, out pViewObject); 
                    try 
                    { 
                        ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject; 
                        //调用Draw方法 
                        ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT, 
                            -1, 
                            IntPtr.Zero, 
                            null, 
                            IntPtr.Zero, 
                            hDrawDC.GetHdc(), 
                            new NativeMethods.COMRECT(bmpRect), 
                            null, 
                            IntPtr.Zero, 
                            0); 
                    } 
                    catch (Exception ex) 
                    { 
                        Console.WriteLine(ex.Message); 
                        throw ex; 
                    } 
                    //释放 
                    hDrawDC.Dispose(); 
                    return pPicture; 
                } 
            } 
            [SuppressUnmanagedCodeSecurity] 
            internal static class UnsafeNativeMethods 
            { 
                public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}"); 

                [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
                public interface IViewObject 
                { 
                    [PreserveSig] 
                    int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [In] NativeMethods.COMRECT lprcBounds, [In] NativeMethods.COMRECT lprcWBounds, IntPtr pfnContinue, [In] int dwContinue); 
                    [PreserveSig] 
                    int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet); 
                    [PreserveSig] 
                    int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze); 
                    [PreserveSig] 
                    int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze); 
                    void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink); 
                    void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(UnmanagedType.LPArray)] System.Runtime.InteropServices.ComTypes.IAdviseSink[] pAdvSink); 
                } 
            } 
        } 

    } 
}

#5


顶下1

#6


up

#7


这篇文章有介绍,实现思路:http://www.hijava.org/architecture/the-implements-of-page-screenshots

#1


up

#2


以前看到过一网站,直接在网页上拉框,保存一个区域作为自己的定制的页面,非常炫的功能.网址忘了,有知道的说一下.现在想来研究研究他们怎么实现的

#3


给你个Winform下的例子:
WINFORM把一个网页保存成图片

使用

Bitmap MyImage = Test.GetControlScrollImage(new Uri(@"http://www.sina.com.cn"), 1024); 
MyImage.Save(@"C:\1.BMP"); 
MyImage.Dispose();
使用到的类

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

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

namespace Print 

    public class Test 
    { 
        public static Bitmap GetHtmlImage(Uri UrlString,int Width) 
        { 
            WebBrowser MyControl = new WebBrowser(); 
            MyControl.Size = new Size(Width, 10); 
            MyControl.Url = UrlString; 
            while (MyControl.ReadyState != WebBrowserReadyState.Complete) 
            { 
                Application.DoEvents(); 
            } 
            
           
            MyControl.Height= MyControl.Document.Body.ScrollRectangle.Height+20; 
            MyControl.Url = UrlString; 
            WebControlImage.Snapshot snap = new WebControlImage.Snapshot(); 
            Bitmap MyImage= snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(0, 0, MyControl.Width, MyControl.Height)); 

            MyControl.Dispose(); 

            return MyImage; 
             
        }       
        /// <summary> 
        /// WebBrowser获取图形 
        /// </summary> 
        private class WebControlImage 
        { 
            internal static class NativeMethods 
            { 
                [StructLayout(LayoutKind.Sequential)] 
                public sealed class tagDVTARGETDEVICE 
                { 
                    [MarshalAs(UnmanagedType.U4)] 
                    public int tdSize; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdDriverNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdDeviceNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdPortNameOffset; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short tdExtDevmodeOffset; 
                } 

                [StructLayout(LayoutKind.Sequential)] 
                public class COMRECT 
                { 
                    public int left; 
                    public int top; 
                    public int right; 
                    public int bottom; 
                    public COMRECT() 
                    { 
                    } 

                    public COMRECT(Rectangle r) 
                    { 
                        this.left = r.X; 
                        this.top = r.Y; 
                        this.right = r.Right; 
                        this.bottom = r.Bottom; 
                    } 

                    public COMRECT(int left, int top, int right, int bottom) 
                    { 
                        this.left = left; 
                        this.top = top; 
                        this.right = right; 
                        this.bottom = bottom; 
                    } 

                    public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height) 
                    { 
                        return new NativeMethods.COMRECT(x, y, x + width, y + height); 
                    } 

                    public override string ToString() 
                    { 
                        return string.Concat(new object[] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom }); 
                    } 

                } 

                [StructLayout(LayoutKind.Sequential)] 
                public sealed class tagLOGPALETTE 
                { 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short palVersion; 
                    [MarshalAs(UnmanagedType.U2)] 
                    public short palNumEntries; 
                } 
            } 

#4




            public class Snapshot 
            { 
                /// <summary> 
                /// 取快照 
                /// </summary> 
                /// <param name="pUnknown">Com 对象</param> 
                /// <param name="bmpRect">图象大小</param> 
                /// <returns></returns> 
                public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect) 
                { 
                    if (pUnknown == null) 
                        return null; 
                    //必须为com对象 
                    if (!Marshal.IsComObject(pUnknown)) 
                        return null; 
                    //IViewObject 接口 
                    UnsafeNativeMethods.IViewObject ViewObject = null; 
                    IntPtr pViewObject = IntPtr.Zero; 
                    //内存图 
                    Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height); 
                    Graphics hDrawDC = Graphics.FromImage(pPicture); 
                    //获取接口 
                    object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown), 
                        ref UnsafeNativeMethods.IID_IViewObject, out pViewObject); 
                    try 
                    { 
                        ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject; 
                        //调用Draw方法 
                        ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT, 
                            -1, 
                            IntPtr.Zero, 
                            null, 
                            IntPtr.Zero, 
                            hDrawDC.GetHdc(), 
                            new NativeMethods.COMRECT(bmpRect), 
                            null, 
                            IntPtr.Zero, 
                            0); 
                    } 
                    catch (Exception ex) 
                    { 
                        Console.WriteLine(ex.Message); 
                        throw ex; 
                    } 
                    //释放 
                    hDrawDC.Dispose(); 
                    return pPicture; 
                } 
            } 
            [SuppressUnmanagedCodeSecurity] 
            internal static class UnsafeNativeMethods 
            { 
                public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}"); 

                [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
                public interface IViewObject 
                { 
                    [PreserveSig] 
                    int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [In] NativeMethods.COMRECT lprcBounds, [In] NativeMethods.COMRECT lprcWBounds, IntPtr pfnContinue, [In] int dwContinue); 
                    [PreserveSig] 
                    int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet); 
                    [PreserveSig] 
                    int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze); 
                    [PreserveSig] 
                    int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze); 
                    void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink); 
                    void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(UnmanagedType.LPArray)] System.Runtime.InteropServices.ComTypes.IAdviseSink[] pAdvSink); 
                } 
            } 
        } 

    } 
}

#5


顶下1

#6


up

#7


这篇文章有介绍,实现思路:http://www.hijava.org/architecture/the-implements-of-page-screenshots