国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

活用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
     );
     

     

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 白朗县| 宁河县| 汉源县| 榕江县| 林芝县| 邓州市| 十堰市| 安宁市| 昌宁县| 永嘉县| 长泰县| 东宁县| 前郭尔| 靖远县| 通海县| 天津市| 潜江市| 台山市| 曲麻莱县| 从江县| 巨鹿县| 南阳市| 静宁县| 板桥市| 静乐县| 松原市| 曲周县| 碌曲县| 凤冈县| 郓城县| 定远县| 葵青区| 时尚| 柞水县| 平阳县| 湘乡市| 河曲县| 运城市| 琼中| 宁陕县| 河曲县|