我正在使用 Aaron Ballman 的 Windows 功能套件从我的网络摄像头捕获视频。它工作正常,但... webcam.startpreview 启动出现的相机图像,webcam.stoppreview 照它说的那样做并停止视频。
我的问题是,在 stoppreview 之后,我在画布控件中留下了一张静止图像,我需要知道如何将该图像保存到磁盘 - 最好是作为 jpg 文件。
在这种情况下,画布控件实际上并不包含图像;它仅用于指定直接显示在 Canvas 顶部的系统管理窗口的尺寸和父级。
要获取当前帧,您需要捕获此系统管理窗口的内容。
例如,将此功能添加到 WebCamWFS 模块:
Function CaptureFrame(SourceCanvas As Canvas) As Picture
Declare Function GetDC Lib "User32" (HWND As Integer) As Integer
Declare Function BitBlt Lib "GDI32" (DCdest As Integer, xDest As Integer, yDest As Integer, nWidth As Integer, _
nHeight As Integer, DCdource As Integer, xSource As Integer, ySource As Integer, rasterOp As Integer) As Boolean
Declare Function ReleaseDC Lib "User32" (HWND As Integer, DC As Integer) As Integer
Const SRCCOPY = &h00CC0020
Const CAPTUREBLT = &h40000000
Dim hDC, w, h As Integer
hDC = GetDC(mWnd)
w = SourceCanvas.Width
h = SourceCanvas.Height
x = SourceCanvas.Left + SourceCanvas.Window.Left
y = SourceCanvas.Top + SourceCanvas.Window.Top
Dim capture As New Picture(w, h, 24)
Call BitBlt(Capture.Graphics.Handle(1), 0, 0, w, h, hDC, 0, 0, SRCCOPY Or CAPTUREBLT)
Call ReleaseDC(mWnd, hDC)
Return capture
End Function
使用 Canvas 控件的DrawInto方法将图像复制到Picture
对象,然后将图片保存到文件中:
Dim mypic As New Picture(TargetCanvas.Width, TargetCanvas.Height, 32)
TargetCanvas.Drawinto(mypic.Graphics, 0, 0)
Dim saveto As FolderItem = GetSaveFolderItem("", "mypic.jpg")
mypic.Save(saveto, Picture.SaveAsJPEG)