Results 1 to 6 of 6

Thread: [RESOLVED] Problem saving video streams

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Resolved [RESOLVED] Problem saving video streams

    Sorry i didnt say in the title, this is in C#
    For the past few days iv been messing around with code that i found for using a webcam to take pictures and record videos. I had never worked with interopservices before so iv basically been going along looking up lines of code until i understood what everythings doing.
    My problem now is, as far as i understand everythings in place for the stop button save me a video file of what was recorded after the record button was pressed, but it doesnt... there are no errors but the file doesnt appear in my folder.
    The similar operation of taking a picture and saving that to my folder works fine.

    I did try using the capCaptureSequence and capFileSaveAs macros instead, but i didnt have much luck with them because i didnt know which part of the system they comunicated with.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WebCam
    {
    public partial class frmCamera : Form
    {
    private int webcam1;
    
    const int WM_CAP_START = 1024;
    const int WS_CHILD = 1073741824;
    const int WS_VISIBLE = 268435456;
    const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
    const int WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11);
    const int WM_CAP_EDIT_COPY = (WM_CAP_START + 30);
    const int WM_CAP_SEQUENCE = (WM_CAP_START + 62);
    const int WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23);
    const int WM_CAP_SET_SCALE = (WM_CAP_START + 53);
    const int WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52);
    const int WM_CAP_SET_PREVIEW = (WM_CAP_START + 50);
    const int SWP_NOMOVE = 2;
    const int SWP_NOSIZE = 1;
    const int SWP_NOZORDER = 4;
    const int webcam1_BOTTOM = 1;
    
    
    //---The capGetDriverDescription function retrieves the version description of the capture driver---
    [System.Runtime.InteropServices.DllImport("avicap32 .dll")]
    static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer);
    
    //---The capCreateCaptureWindow function creates a capture window---
    [System.Runtime.InteropServices.DllImport("avicap32 .dll")]
    static extern int capCreateCaptureWindow(
    string lpszWindowName, int dwStyle, int x, int y,int nWidth, short nHeight, int form, int nID);
    
    //---This function sends the specified message to a window or windows---
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")]
    static extern int SendMessage(int webcam1, int Msg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
    
    //---Sets the position of the window relative to the screen buffer---
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")]
    static extern int SetWindowPos(int webcam1, int webcam1InsertAfter, int x, int y, int cx, int cy, int wFlags);
    
    //--This function destroys the specified window--
    [System.Runtime.InteropServices.DllImport("user32")]
    static extern bool DestroyWindow(int destroy);
    
    //---preview the selected video source---
    
    public frmCamera()
    {
    InitializeComponent();
    }
    
    private void PreviewVideo(PictureBox pbox)
    {
    webcam1 = capCreateCaptureWindow("0", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbox.Handle.ToInt32(), 0);
    if (SendMessage(webcam1, WM_CAP_DRIVER_CONNECT, 0, 0) != 0)
    {
    SendMessage(webcam1, WM_CAP_SET_SCALE, 1, 0);
    SendMessage(webcam1, WM_CAP_SET_PREVIEWRATE, 30, 0);
    SendMessage(webcam1, WM_CAP_SET_PREVIEW, 1, 0);
    SetWindowPos(webcam1, webcam1_BOTTOM, 0, 0, pbox.Width, pbox.Height, SWP_NOMOVE | SWP_NOZORDER);
    }
    else
    {
    DestroyWindow(webcam1);
    }
    }
    
    private void btnStop_Click(object sender, EventArgs e)
    {
    btnRecord.Enabled = true;
    btnStop.Enabled = false;
    Application.DoEvents();
    //---save the recording to file---
    SendMessage(webcam1, WM_CAP_FILE_SAVEAS, 0, "I:\\My Videos" + System.DateTime.Now.ToFileTime() + ".avi");
    }
    
    private void btnRecord_Click(object sender, EventArgs e)
    {
    btnRecord.Enabled = false;
    btnStop.Enabled = true;
    Application.DoEvents();
    //---start recording---
    SendMessage(webcam1, WM_CAP_SEQUENCE, 0, 0);
    }
    
    private void btnSnapshot_Click(object sender, EventArgs e)
    {
    
    IDataObject data;
    Image bmap;
    //---copy the image to the Clipboard---
    SendMessage(webcam1, WM_CAP_EDIT_COPY, 0, 0);
    //---retrieve the image from Clipboard and convert it to the bitmap format---
    data = Clipboard.GetDataObject();
    
    if (data.GetDataPresent(typeof(System.Drawing.Bitmap) ))
    {
    bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap ))));
    bmap.Save("I:\\" + System.DateTime.Now.ToFileTime() + ".bmp");
    }
    }
    
    private void frmCamera_Load(object sender, EventArgs e)
    {
    //Here we put the video from the camera to the picturebox
    
    PreviewVideo(picFrame);
    }
    
    private void btnClose_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }
    }
    }
    Last edited by verian; Jan 21st, 2010 at 03:41 AM. Reason: didnt say it was C#

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width