首页 > 开发 > 综合 > 正文

活用C#截取计算机屏幕

2024-07-21 02:26:31
字体:
来源:转载
供稿:网友
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 今天无聊翻代码,翻出来一个以前写的c#截屏的函数...拿出来和大家共享一下.
    这段代码是参照网上一段截屏的c++代码改写的.只不过把api都声明了一下而已.
    声明的各api也附后.以供参照.如有问题欢迎指出.
    作者:appledotnet

     

     ///
    /// 截取部分屏幕
    ///
    /// 左上角
    /// 右下角
    /// 是否全屏幕
    /// 返回值bitmap
    public static bitmap getpartscreen(point p1,point p2,bool full)
    {
        intptr hscrdc,hmemdc;
        intptr hbitmap,holdbitmap;
        int nx,ny,nx2,ny2;
        nx=ny=nx2=ny2=0;
        int nwidth, nheight;
        int xscrn, yscrn;
        hscrdc = createdc("display", null, null, 0);//创建dc句柄
        hmemdc = createcompatibledc(hscrdc);//创建一个内存dc
        xscrn = getdevicecaps(hscrdc, getdevicecapsindex.horzres);//获取屏幕宽度
        yscrn = getdevicecaps(hscrdc, getdevicecapsindex.vertres);//获取屏幕高度
        if(full)//如果是截取整个屏幕
        {
            nx = 0;
            ny = 0;
            nx2 = xscrn;
            ny2 = yscrn;
        }
        else
        {
            nx = p1.x;
            ny = p1.y;
            nx2 =p2.x;
            ny2 =p2.y;
            //检查数值合法性
            if(nx<0)nx = 0;
            if(ny<0)ny = 0;
            if(nx2>xscrn)nx2 = xscrn;
            if(ny2>yscrn)ny2 = yscrn;
        }
        nwidth = nx2 - nx;//截取范围的宽度
        nheight = ny2 - ny;//截取范围的高度
        hbitmap = createcompatiblebitmap(hscrdc, nwidth, nheight);//从内存dc复制到hbitmap句柄
        holdbitmap = selectobject(hmemdc, hbitmap);
        bitblt(hmemdc, 0, 0, nwidth, nheight,hscrdc, nx, ny,(uint32)0xcc0020);
        hbitmap = selectobject(hmemdc, holdbitmap);
        deletedc(hscrdc);//删除用过的对象
        deletedc(hmemdc);//删除用过的对象
        return bitmap.fromhbitmap(hbitmap);//用bitmap.fromhbitmap从hbitmap返回bitmap
    }
     


    所用到的api声明:


     [dllimport("gdi32.dll")]
    public static extern intptr createdc(
     string lpszdriver,        // driver name
     string lpszdevice,        // device name
     string lpszoutput,        // not used; should be null
     int64 lpinitdata  // optional printer data
     );
     
    [dllimport("gdi32.dll")]
    public static extern intptr createcompatibledc(
     intptr hdc // handle to dc
     );
     
    [dllimport("gdi32.dll")]
    public static extern int getdevicecaps(
     intptr hdc,     // handle to dc
     getdevicecapsindex nindex   // index of capability
    );

    [dllimport("gdi32.dll")]
    public static extern intptr createcompatiblebitmap(
     intptr hdc,        // handle to dc
     int nwidth,     // width of bitmap, in pixels
     int nheight     // height of bitmap, in pixels
     );

    [dllimport("gdi32.dll")]
    public static extern intptr selectobject(
     intptr hdc,          // handle to dc
     intptr hgdiobj   // handle to object
     );

    [dllimport("gdi32.dll")]
    public static extern int bitblt(
     intptr hdcdest, // handle to destination dc
     int nxdest,  // x-coord of destination upper-left corner
     int nydest,  // y-coord of destination upper-left corner
     int nwidth,  // width of destination rectangle
     int nheight, // height of destination rectangle
     intptr hdcsrc,  // handle to source dc
     int nxsrc,   // x-coordinate of source upper-left corner
     int nysrc,   // y-coordinate of source upper-left corner
     uint32 dwrop  // raster operation code
     );

    [dllimport("gdi32.dll")]
    public static extern int deletedc(
     intptr hdc          // handle to dc
     );
     

     

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表