VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C#实现将网页保存成图片的网页拍照功能

本文实例主要实现了网页照相机程序的功能。C#实现将网页保存成图片格式,简单实现网页拍照,主要是基于ActiveX 组件的网页快照类,AcitveX 必须实现 IViewObject 接口。因此读者完全可扩展此类将其用于你的C#软件项目中。在此特别感谢作者:随飞提供的代码。

主要功能代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Drawing;
using System.Windows.Forms;
namespace SnapLibrary
{
  /// <summary>
  /// ActiveX 组件快照类,用于网页拍照,将网页保存成图片
  /// AcitveX 必须实现 IViewObject 接口
  /// 作者:随飞
  /// </summary>
  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 接口
      SnapLibrary.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(SnapLibrary.UnsafeNativeMethods.IViewObject)) as SnapLibrary.UnsafeNativeMethods.IViewObject;
        //调用Draw方法
        ViewObject.Draw((int)DVASPECT.DVASPECT_CONTENT,
          -1,
          IntPtr.Zero,
          null,
          IntPtr.Zero,
          hDrawDC.GetHdc(),
          new NativeMethods.COMRECT(bmpRect),
          null,
          IntPtr.Zero,
          0);
        Marshal.Release(pViewObject);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
        throw ex;
      }
      //释放
      hDrawDC.Dispose();
      return pPicture;
    }
  }
}


相关教程