How to save a picture from a webcam

1.1k views Asked by At

I am using Aaron Ballman's Windows Functionality Suite to capture video from my webcam. It works fine but ... webcam.startpreview starts the camera images appearing and webcam.stoppreview does as it says and stops the video.

My question is that, after stoppreview I am left with a still image in the canvas control and I need to know how to save that image to disk - preferably as a jpg file.

1

There are 1 answers

6
Andrew Lambert On BEST ANSWER

The canvas control doesn't actually contain the image in this case; it's only used to specify the dimensions and parent of a system-managed window that gets displayed directly on top of the Canvas.

To grab the current frame you'll need to capture the contents of this system-managed window.

e.g. add this function to the WebCamWFS module:

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

Use the DrawInto method of the Canvas control to copy the image to a Picture object, then save the Picture to a file:

  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)