Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
#Include Once "dshow.inc"
%IDC_Graphic = 500
Global hDlg, hGraphic As Dword
Global CameraZoom As Single
Global pGraph As IGraphBuilder 'Filter Graph Manager
Global pBuild As ICaptureGraphBuilder2 'Capture Graph Builder
Global pSysDevEnum As ICreateDevEnum 'enumeration object
Global pEnumCat As IEnumMoniker
Global pMoniker As IMoniker 'contains information about other objects
Global pceltFetched As Dword
Global pCap As IBaseFilter 'Video capture filter
Global pControl As IMediaControl
Global pWindow As IVideoWindow 'Display Window
Function PBMain() As Long
Dialog New Pixels, 0, "First Camera Test",300,300,400,300, %WS_OverlappedWindow Or %WS_ClipChildren To hDlg
Control Add Graphic, hDlg, %IDC_Graphic,"", 50,50,300,200, %WS_Border Or %WS_ClipChildren
Control Handle hDlg, %IDC_Graphic To hGraphic
Graphic Attach hDlg, %IDC_Graphic
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
CameraZoom = 1
DisplayFirstCamera
Case %WM_LButtonUp
CameraZoom += 0.25
VideoResize(640/480)
Case %WM_ContextMenu
CameraZoom -= 0.25
VideoResize(640/480)
Case %WM_Size
GraphicResize_Pixels(640/480,20)
VideoResize(640/480)
End Select
End Function
Sub GraphicResize_Pixels(AR As Single, sBorder As Long)
Local x,y,w,h,wNew, hNew As Long
Dialog Get Client hDlg To w,h
w -= sBorder*2 : h -= sBorder*2
wNew = AR / Max(AR / w, 1 / h)
hNew = 1 / Max(AR / w, 1 / h)
x = sBorder + (w-wNew)/2 : y = sBorder + (h-hNew)/2
Control Set Loc hDlg, %IDC_Graphic, x,y
Control Set Size hDlg, %IDC_Graphic, wNew, hNew
End Sub
Sub VideoResize(AR As Single)
Local x,y,w,h,wNew, hNew As Long
Control Get Size hDlg, %IDC_Graphic To w,h 'graphic size
wNew = AR / Max(AR / w, 1 / h)*CameraZoom 'new video width
hNew = 1 / Max(AR / w, 1 / h)*CameraZoom 'new video height
x = (w-wNew)/2 : y = (h-hNew)/2 'xy origina of video window
pWindow.SetWindowPosition(x,y,wNew,hNew)
End Sub
Sub DisplayFirstCamera
pGraph = NewCom ClsId $CLSID_FilterGraph 'filter graph
pBuild = NewCom ClsId $CLSID_CaptureGraphBuilder2 'capture graph builder
pSysDevEnum = NewCom ClsId $CLSID_SystemDeviceEnum 'enumeration object
If pSysDevEnum.CreateClassEnumerator($CLSID_VideoInputDeviceCategory, pEnumCat, 0) <> %S_Ok Then Exit Sub
pEnumCat.next(1, pMoniker, pceltFetched) 'cycle through monikders
pMoniker.BindToObject(Nothing, Nothing, $IID_IBaseFilter, pCap) 'create device filter for the chosen device
pGraph.AddFilter(pCap,"First Camera") 'add chosen device filter to the filter graph
pBuild.SetFilterGraph(pGraph) 'initialize pBuild
pBuild.RenderStream $Pin_Category_Preview, $MediaType_Video, pCap, Nothing, Nothing 'render the live source
pWindow = pGraph : pWindow.Owner = hGraphic : pWindow.WindowStyle = %WS_Child Or %WS_ClipChildren 'video window settings
pWindow.MessageDrain = hDlg
pControl = pGraph
pControl.Run
End Sub
http://www.garybeene.com/sw/gbsnippets.htm