DIV截图,DIV上面展示的是什么样的就截什么样的

不管你把浏览器窗口移到什么地方,不管是全屏还是小窗口,都可以截,不用创建浏览器对象,直接用windowsAPI,百试不爽。

不过其中用到了指针,在ASP.NET网站中运行指针,则必须在Config中加入下面代码:

<system.codedom>

    <compilers>

      <!--允许运行不安全代码-->

      <compiler language = "c#;cs;csharp" warningLevel="4" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" compilerOptions="/unsafe+" />

    </compilers>

  </system.codedom>

 

 

 

下面是详细代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;


public partial class gs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) 
    {
        this.Title = "公式编辑";
    }
    
    [DllImport("user32.dll")]
    unsafe public static extern int ClientToScreen(int handle, Point* p);  //将客户区坐标转换成屏幕坐标
    [DllImport("user32.dll")]
    public static extern int WindowFromPoint(int x, int y);      //获取指定点的窗口的句柄
    [DllImport("user32.dll")]
    unsafe public static extern int GetCursorPos(Point* p);     //获取当前的鼠标位置


    int handle = 0;
    int bx;
    int by;
    int width;
    int height;
    Bitmap bmp;
    Point p;
    Point cp;
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text != "")
        {
            string filename=CreateName();
            string filePath = Server.MapPath("~/UpLoad/gs/"+filename);

            string[] ss = TextBox2.Text.Split(',');
            bx = int.Parse(ss[0]);
            by = (int)(double.Parse(ss[1]));
            width = int.Parse(ss[2].Substring(0, ss[2].Length - 2));
            height=int.Parse(ss[3].Substring(0,ss[3].Length-2));
            p = new Point(bx,by);
            NewPoint();
            
            DisPlayImg();
            bmp.Save(filePath);
            bmp.Dispose();

            string js = "returnValue='" + filename + "';window.close();";
            ExcJs(js);
        }
    }

    unsafe private void NewPoint()   //获取公式相对于屏幕的坐标
    {
        cp = new Point(0, 0);
        fixed (Point* pp = &cp)
        {
            if (GetCursorPos(pp) != 0)
            {
                handle = WindowFromPoint(cp.X, cp.Y);
                if (handle != 0)
                {
                    fixed(Point* pt=&p)
                    if (ClientToScreen(handle,pt) == 0)
                        ShowMessage("坐标转换失败!");
                }
                else
                    ShowMessage("没有找到窗口!");
            }
        }
    }

    public void CatchImg()
    {
        bmp = new System.Drawing.Bitmap(width-1, height-1);
        Graphics g = Graphics.FromImage(bmp);
        g.CopyFromScreen(new Point(p.X+1, p.Y+1), new Point(0,0), new Size(width-1, height-1));
    }

    public void DisPlayImg()  //截图
    {
        Thread t = new Thread(new ThreadStart(CatchImg));
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        t.Join();
    }

    void ShowMessage(string MSG)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('" + MSG + "')</script>", false);
    }

    void ExcJs(string Js)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>"+Js+"</script>", false);
    }

    public string CreateName()
    {
        Guid g = Guid.NewGuid();
        return g.ToString()+".jpg";
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值