Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to change display settings: screen resolution, screen refresh rate
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Custom GDI+ class
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Converting Unicode data from the Clipboard to a character string using a given code page
Winsock: sending email messages (SMTP, port 25)
Enumerating data formats currently available on the clipboard
Custom FTP Class for Visual FoxPro application
Splash Screen for the VFP application
How to download a file from the FTP server using FtpGetFile
How to activate Windows Calculator
Detecting changes in connections to removable drives (VFP9)
How to put a vertical text scrolling on the form (a movie cast)
Custom HttpRequest class (WinHTTP)
How to convert a bitmap file to monochrome format (1 bpp)
Displaying bitmap using the AlphaBlend function
Establishing connection using the SQLDriverConnect
Using WM_COPYDATA for interprocess communication (VFP9)
Mapping and disconnecting network drives
GDI+: copying to the Clipboard (a) image of active FoxPro window/form, (b) image file
Using Font and Text functions
Displaying animated images on FoxPro form with BitBlt and StretchBlt functions
How to play AVI file on the _screen
Enumerating raw input devices attached to the system (keyboard, mouse, human interface device)
Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera

User rating: 9.33/10 (3 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:

This code sample has been tested with Logitech QuickCam Pro 4000 and with Dimera 350C USB Camera (an ancient camera from the year 2000) both operating under Windows XP.

Also I tested the code with Microsoft LifeCam VS-6000 on Vista Business 64-bit PC (the picture below).



No ActiveX controls are required, actually not much of programming is required either. Two API functions, capCreateCaptureWindow and SendMessage do practically all the job. The FoxPro form hosts a child window (the capture window) connected to existing capturing driver.

See also:
  • FoxTalk article: Using the Video Capture API in Visual FoxPro
  •  
    using System;
    using System.Runtime.InteropServices;
     
    namespace WindowsApplication1
    {
        public class VideoCapture
        {
     
            [StructLayout(LayoutKind.Sequential)] 
                public struct BITMAPINFOHEADER
            {
                [MarshalAs(UnmanagedType.I4)] public Int32 biSize ;
                [MarshalAs(UnmanagedType.I4)] public Int32 biWidth ;
                [MarshalAs(UnmanagedType.I4)] public Int32 biHeight ;
                [MarshalAs(UnmanagedType.I2)] public short biPlanes;
                [MarshalAs(UnmanagedType.I2)] public short biBitCount ;
                [MarshalAs(UnmanagedType.I4)] public Int32 biCompression;
                [MarshalAs(UnmanagedType.I4)] public Int32 biSizeImage;
                [MarshalAs(UnmanagedType.I4)] public Int32 biXPelsPerMeter;
                [MarshalAs(UnmanagedType.I4)] public Int32 biYPelsPerMeter;
                [MarshalAs(UnmanagedType.I4)] public Int32 biClrUsed;
                [MarshalAs(UnmanagedType.I4)] public Int32 biClrImportant;
            } 
     
            [StructLayout(LayoutKind.Sequential)] public struct BITMAPINFO
            {
                [MarshalAs(UnmanagedType.Struct, SizeConst=40)] 
                    public BITMAPINFOHEADER bmiHeader;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)] 
                    public Int32[] bmiColors;
            }
     
            private const int WS_CHILD = 0x40000000;
            private const int WS_VISIBLE = 0x10000000;
            private const int SWP_SHOWWINDOW = 0x0040;
            private const int WM_CAP_START = 0x0400;
            private const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START+10);
            private const int WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START+11);
            private const int WM_CAP_SET_PREVIEWRATE = (WM_CAP_START+52);
            private const int WM_CAP_SET_PREVIEW = (WM_CAP_START+50);
            private const int WM_CAP_GRAB_FRAME = (WM_CAP_START+60);
            private const int WM_CAP_GET_VIDEOFORMAT = (WM_CAP_START+44);
     
            private IntPtr hCapture=(IntPtr) 0;
            private int width=0;
            private int height=0;
     
            public bool CreateCaptureWindow(IntPtr hParent, int x, int y) 
            {
                hCapture = capCreateCaptureWindow("", 
                    WS_CHILD | WS_VISIBLE,
                    x, y, 32, 24, hParent, 0);
                if ((int) hCapture==0) {return false;}
     
                SendMessage(hCapture, WM_CAP_DRIVER_CONNECT, 0, 0);
                GetVideoFormat();
                SetWindowPos(hCapture, 0, x, y, width, height, 
                    SWP_SHOWWINDOW);
                return true;
            }
     
            public void ReleaseCaptureWindow() 
            {
                if ((int) hCapture != 0) 
                {
                    SendMessage(hCapture, WM_CAP_DRIVER_DISCONNECT, 0, 0);
                    DestroyWindow(hCapture);
                    hCapture= (IntPtr) 0;
                }
            }
     
            public void StartPreview() 
            {
                SendMessage(hCapture, WM_CAP_SET_PREVIEWRATE, 15, 0);
                SendMessage(hCapture, WM_CAP_SET_PREVIEW, 1, 0);
            }
     
            public void StopPreview() 
            {
                SendMessage(hCapture, WM_CAP_SET_PREVIEW, 0, 0);
            }
     
            public void GetFrame() 
            {
                SendMessage(hCapture, WM_CAP_GRAB_FRAME, 0, 0);
            }
     
            private void GetVideoFormat() 
            {
                BITMAPINFO buffer = new BITMAPINFO();
                int size = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
                buffer.bmiHeader.biSize = size;
     
                SendMessage(hCapture, WM_CAP_GET_VIDEOFORMAT, 
                    size, ref buffer);
     
                width = buffer.bmiHeader.biWidth;
                height = buffer.bmiHeader.biHeight;
            }
     
            [DllImport("User32.dll")]
            private static extern bool SendMessage(IntPtr hWnd, 
                int wMsg, int wParam, ref BITMAPINFO lParam);
     
            [DllImport("User32.dll")]
            private static extern bool SendMessage(IntPtr hWnd, 
                int wMsg, int wParam, int lParam);
     
            [DllImport("User32.dll")] 
            private static extern int SetWindowPos(IntPtr hWnd, 
                int hWndInsertAfter, int x, int y, 
                int cx, int cy, int wFlags);
     
            [DllImport("user32.dll")]
            private static extern int DestroyWindow(IntPtr hwnd);
     
            [DllImport("avicap32.dll")]
            private static extern IntPtr capCreateCaptureWindow
                (string lpszWindowName, int dwStyle,
                int x, int y, int nWidth, int nHeight, 
                IntPtr hWnd, int nID);
     
        }
    }
     
     
     

    User rating: 9.33/10 (3 votes)
    Rate this code sample:
    • ~
    3797 bytes  
    Created: 2004-06-04 18:30:44  
    Modified: 2011-12-10 09:20:22  
    Visits in 7 days: 166  
    Listed functions:
    capCreateCaptureWindow
    DestroyWindow
    SendMessage
    SetWindowPos
    Printer friendly API declarations
    My comment:
    Apr.15, 2008: C++ version added
    May 27, 2007: all capWindow functionality moved to Custom control
    Oct.12, 2004: SaveToDib method captures and saves a single image as a device-independent bitmap (DIB).

    Windows Image Acquisition Automation Layer

    The Microsoft Windows Image Acquisition (WIA) Automation Layer 2.0 is a full-featured image manipulation component that provides end-to-end image processing capabilities. The WIA Automation Layer makes it easy to acquire images on digital cameras, scanners, or Web cameras, and to rotate, scale, and annotate your image files.

    Applications that use the WIA Automation LayerAPI require Windows XPService Pack 1 (SP1) or later. Earlier versions of Windows are not supported. You will need WIAAut.dll to use the WIA Automation Layer.

    * * *
    You may have several capture windows created and fed from different Video Sources. You can not feed two capture windows from same source.

    For example, now I have two digital cameras connected through USB -- Dimera 350C and Logitech QuickCam -- and one more video source, ATI TV Wonder Pro card that receives TV channels through coaxial cable.


    With slightly modified code I can create three capture windows, one for each video source. All three windows simultaneously display preview from their sources.

    * * *
    Window styles WS_SYSMENU, WS_CAPTION and WS_THICKFRAME applied to the capture window make it movable and resizable. WS_CHILD must be present in any combination of window styles.
    Word Index links for this example:
    Translate this page:
      Spanish    Portuguese    German    French    Italian  
    FreeTranslation.com offers instant, free translations of text or web pages.
    User Contributed Notes:
    nestor hdz | 2005-05-16 11:48:49
    esta muy bueno, pero no sabras algo acerca de los lectores digitales ya que no he podido poner el que tenemos y es un U.are.U 4000sensor de digitalpersona

    mi correo es nareas@parkerseal.com
    stuff@data-lite.com | 2005-07-22 17:58:31
    I get "Failed to connect to video input device." when I run this code in VFP 9.0.

    Clearly I need to be able to select the camera, but I fail to figure out how to select a camera at this time!

    Please help!

    Dennis Kean
    A.M. | 2005-07-22 18:05:34
    Dennis, I tried but found nothing in the API that could allow to select video source.

    When testing I had three sources connected to my computer -- two USB cameras and TV card. Every time the system tries to connect to the last device that was active.
    Alex | 2005-10-14 02:58:20
    Hi,

    I have not tested it, but think it might help.

    Have a look at

    WM_CAP_GET_MCI_DEVICE
    WM_CAP_SET_MCI_DEVICE

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_wm_cap_get_videoformat.asp


    Regards
    Alex
    A.M. | 2005-10-14 07:23:24
    Alex, I think I already tried those two and it did not work. Will give another try.
    robertosegovia@hotmail.com | 2007-01-15 12:31:14
    I have the complete program that you sent in June 2005 and it operate. But upon executing this program by means of a command button of a form selected from a option of menu of user, it don't show the image of video.

    They could help me, thank you
    help | 2007-06-22 09:03:34
    Dear sir
    guide me to retrieve the saved image (.dib) and display in the form pls
    regards
    balu
    A.M. | 2007-06-25 21:41:58
    The SaveToDib method in the code above saves current frame in a DIB file.

    In VFP8/9 such DIB file can be directly assigned to the Picture property of VFP Image control, no format conversion is required. The control will display the image.

    As well such DIB image can be directly stored in General field of VFP table, for example:

    CREATE CURSOR csTmp (filename C(30), imagedata G)
    APPEND BLANK
    REPLACE filename WITH 'GRAB_3G17JDE3.DIB'
    APPEND GENERAL imagedata FROM 'GRAB_3G17JDE3.DIB'

    Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

    Privacy policy
    Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (107.22.127.92)
    4 sec.Example: 'How to delete a service object'
    2.36 hrs.Function: 'SHRegOpenUSKey'
    Function group: 'Registry'
    2.73 hrs.Function: 'FindResource'
    5.71 hrs.
    Function group: 'Shell Lightweight Utility APIs -- misc. functions'
     Function: 'FindWindow'
    6.07 hrs.Function: 'PlaySound'
    Function group: 'Windows Multimedia'
    6.08 hrs.Example: 'How to hot-track menu item selection in top-level form (requires VFP9)'
    7.94 hrs.Example: 'Retrieving the System Time adjustment'
     Example: 'How to assemble an array of strings and pass it to external function'
    9.22 hrs.Function: 'SHGetFolderPath'
    Google
    Advertise here!