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

首頁 > 開發 > 綜合 > 正文

C#截取屏幕↑

2024-07-21 02:19:29
字體:
來源:轉載
供稿:網友
今天無聊翻代碼,翻出來一個以前寫的c#截屏的函數...拿出來和大家共享一下.
這段代碼是參照網上一段截屏的c++代碼改寫的.只不過把api都聲明了一下而已.
聲明的各api也附后.以供參照.如有問題歡迎指出.([email protected])





///
/// 截取部分屏幕
///
/// 左上角
/// 右下角
/// 是否全屏幕
/// 返回值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
);


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南开区| 衡山县| 江阴市| 大足县| 岳阳市| 托里县| 白玉县| 鹤庆县| 贵港市| 通江县| 尉犁县| 千阳县| 云阳县| 利辛县| 虎林市| 沧州市| 泾源县| 灌云县| 南郑县| 高淳县| 贵阳市| 隆德县| 绥中县| 甘德县| 休宁县| 博兴县| 澄迈县| 平塘县| 小金县| 南靖县| 铜山县| 都兰县| 兴安县| 宜昌市| 民权县| 东丽区| 读书| 巴彦淖尔市| 莱州市| 景洪市| 阿坝县|