[robot]自訂Web browser control的畫面截圖function(比原本DrawToBitmap穩定!不會有空白圖片!)

  • 1322
  • 0

摘要:[robot]自訂Web browser control的DrawToOOXX畫面截圖

原本的webBrowser1.DrawToBitmap真的不是很穩定,有時候在本機上執行OK

但是發佈到正式機的時候就變成空白圖片,以下是參考網路上的作法改寫的,穩定多了,目前沒碰過空白圖片

新增一個類別來截圖,此類別直接引用微軟的IViewObject截圖,很簡潔的作法,任何物件引用此interface,都可以立刻截圖


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyNameSpace
{
    public class NativeMethods
    {
        [ComImport]
        [Guid("0000010D-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IViewObject
        {
            void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        public static void DrawToImage(object obj, Image destination, Color backgroundColor)
        {
            using (var graphics = Graphics.FromImage(destination))
            {
                var deviceContextHandle = IntPtr.Zero;
                var rectangle = new RECT();

                rectangle.Right = destination.Width;
                rectangle.Bottom = destination.Height;
                graphics.Clear(backgroundColor);
                                
                try
                {
                    deviceContextHandle = graphics.GetHdc();
                    
                    var viewObject = obj as IViewObject;
                    viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
                }
                finally
                {
                    if (deviceContextHandle != IntPtr.Zero)
                    {
                        graphics.ReleaseHdc(deviceContextHandle);
                    }
                }
            }
        }
    }
}

使用方法:


//截圖           
Bitmap bitmap = new Bitmap(1500, 1200);
Rectangle bitmapRect = new Rectangle(0, 0, 1500, 1200);
// This is a method of the WebBrowser control, and the most important part
//webBrowser1.DrawToBitmap(bitmap, bitmapRect);
//改用自訂的drawtoimage
NativeMethods.DrawToImage(webBrowser1.ActiveXInstance, bitmap, Color.White);

// Generate a thumbnail of the screenshot (optional)
System.Drawing.Image origImage = bitmap;
System.Drawing.Image origThumbnail = new Bitmap(1500, 1200, 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, 1500, 1200);
oGraphic.DrawImage(origImage, oRectangle);
string localfile = Path.Combine(Config.LocalStoragePath, sheetno, string.Format("{0}.png", sheetno));
origThumbnail.Save(localfile, ImageFormat.Png);
origImage.Dispose();

 

參考資料:

webbrowser截图空白解决以及截图整个网页 - Freedom Ho

http://www.freedomho.com/manong/60/