现在的位置: 首页 > 综合 > 正文

关于摄像头捕获照相问题

2013年12月11日 ⁄ 综合 ⁄ 共 2363字 ⁄ 字号 评论关闭
主  题:   再散100分,关于摄像头捕获照相问题,还没有解决,解决了再加分
作  者:   nangangshi (小米)
等  级:  
信 誉 值:   98
所属社区:   .NET技术 C#
问题点数:   100
回复次数:   7
发表时间:   2006-5-2 20:39:09

看起来很难的问题,其实实现起来很简单,你用捕获当前窗口的图像,得到一个BitMap对象,就像那种抓图软件一样,再根据"矩形框"的位置,剪下所抓的图片的相应部份,对BitMap对象的操作是很简单的,抓图稍微麻烦一点,用到的API函数有:CreateDC,GetWindowDC,PrintWindow,SelectObject,BitBlt等等,这些都是gdi32.dll里的。我写一段代码在下面以供参考:
public class Snap
{
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver,        // driver name驱动名
string lpszDevice,        // device name设备名
string lpszOutput,        // not used; should be NULL
IntPtr lpInitData  // optional printer data
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC目标设备的句柄
int nXDest,  // x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest,  // y-coord of destination upper-left corner目标对象的左上角的Y坐标
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源对象的左上角的X坐标
int nYSrc,   // y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop  // raster operation code光栅的操作值
);

[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);

[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 DeleteDC(
IntPtr hdc          // handle to DC
);

[DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd,               // Window to copy,Handle to the window that will be copied.
IntPtr  hdcBlt,             // HDC to print into,Handle to the device context.
UInt32 nFlags              // Optional flags,Specifies the drawing options. It can be one of the following values.
);

[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);
public Snap()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public  static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
MainC.RECT rc=MainC.GetWindowSize(hWnd);
int wh=rc.Width();
int ht=rc.Height();
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, wh, ht);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
}

抱歉!评论已关闭.