[HELP] Webcam Problem

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
13 posts Page 1 of 2
Contributors
User avatar
albertuschw
New Member
New Member
Posts: 13
Joined: Thu Sep 11, 2014 8:46 am

[HELP] Webcam Problem
albertuschw
FYI: I still noob on coding. I use code for camera from VB with Easy Webcam on viewtopic.php?f=38&t=9406&p=68862&hilit=webcam#p68862

if i use my webcam USB, it will show dialog box like in that post which has been solved (in this post viewtopic.php?f=21&t=9297&p=68009&hilit=webcam#p68009
) , but on mine, it still not work when i plug in my webcam USB

And also i dont understand when add "If you want to get the item count and set the ComboBox index you could use something like this:"

can someone make it where i must to put that code?
form load or timer sub?

Can i choose which camera i want to use? camera on my laptop or my USB webcam

Another question from me, can anyone help me recode this to Vb.net.
I want to use some of this code, i just want to capture video from my webcam and take snapshot from it and i'll add some code for my program to control Autodesk Inventor API. But i still confuse about recode language to my Visual Studio Express 2013 for Windows Dekstop, windows form application VB.
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

// Reference path for the following assemblies --> C:\Program Files\Microsoft Expression\Encoder 4\SDK\
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder.Live;
using Microsoft.Expression.Encoder;

namespace EE4Test
{
    public partial class frmEE4WebCam : Form
    {
        /// <summary>
        /// Creates job for capture of live source
        /// </summary>
        private LiveJob _job;

        /// <summary>
        /// Device for live source
        /// </summary>
        private LiveDeviceSource _deviceSource;

        private bool _bStartedRecording = false;

        public frmEE4WebCam()
        {
            InitializeComponent();
        }

        private void frmEE4WebCam_Load(object sender, EventArgs e)
        {
            this.Text += " - ver. " + Application.ProductVersion;

            lstVideoDevices.ClearSelected();
            foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
            {
                lstVideoDevices.Items.Add(edv.Name);
            }
            lblVideoDeviceSelectedForPreview.Text = "";

            lstAudioDevices.ClearSelected();
            foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
            {
                lstAudioDevices.Items.Add(eda.Name);
            }
            lblAudioDeviceSelectedForPreview.Text = "";                       
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            EncoderDevice video = null;
            EncoderDevice audio = null;            

            GetSelectedVideoAndAudioDevices(out video, out audio);
            StopJob();

            if (video == null)
            {
                return;
            }

            // Starts new job for preview window
            _job = new LiveJob();
            
            // Checks for a/v devices
            if (video != null && audio != null)
            {
                // Create a new device source. We use the first audio and video devices on the system
                _deviceSource = _job.AddDeviceSource(video, audio);

                // Is it required to show the configuration dialogs ?
                if (checkBoxShowConfigDialog.Checked)
                {
                    // Yes
                    // VFW video device ?
                    if (lstVideoDevices.SelectedItem.ToString().EndsWith("(VFW)", StringComparison.OrdinalIgnoreCase))
                    {
                        // Yes
                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VfwFormatDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VfwFormatDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VfwSourceDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VfwSourceDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VfwDisplayDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VfwDisplayDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                    }
                    else
                    {
                        // No
                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VideoCapturePinDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VideoCapturePinDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }                            

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VideoCaptureDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VideoCaptureDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VideoCrossbarDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VideoCrossbarDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VideoPreviewPinDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VideoPreviewPinDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }

                        if (_deviceSource.IsDialogSupported(ConfigurationDialog.VideoSecondCrossbarDialog))
                        {
                            _deviceSource.ShowConfigurationDialog(ConfigurationDialog.VideoSecondCrossbarDialog, (new HandleRef(panelVideoPreview, panelVideoPreview.Handle)));
                        }
                    }
                }
                else
                {
                    // No
                    // Setup the video resolution and frame rate of the video device
                    // NOTE: Of course, the resolution and frame rate you specify must be supported by the device!
                    // NOTE2: May be not all video devices support this call, and so it just doesn't work, as if you don't call it (no error is raised)
                    // NOTE3: As a workaround, if the .PickBestVideoFormat method doesn't work, you could force the resolution in the 
                    //        following instructions (called few lines belows): 'panelVideoPreview.Size=' and '_job.OutputFormat.VideoProfile.Size=' 
                    //        to be the one you choosed (640, 480).
                    _deviceSource.PickBestVideoFormat(new Size(640, 480), 15);
                }

                // Get the properties of the device video
                SourceProperties sp = _deviceSource.SourcePropertiesSnapshot();

                // Resize the preview panel to match the video device resolution set
                panelVideoPreview.Size = new Size(sp.Size.Width, sp.Size.Height);

                // Setup the output video resolution file as the preview
                _job.OutputFormat.VideoProfile.Size = new Size(sp.Size.Width, sp.Size.Height);

                // Display the video device properties set
                toolStripStatusLabel1.Text = sp.Size.Width.ToString() + "x" + sp.Size.Height.ToString() + "  " + sp.FrameRate.ToString() + " fps";

                // Sets preview window to winform panel hosted by xaml window
                _deviceSource.PreviewWindow = new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));

                // Make this source the active one
                _job.ActivateSource(_deviceSource);

                btnStartStopRecording.Enabled = true;
                btnGrabImage.Enabled = true;

                toolStripStatusLabel1.Text = "Preview activated";
            }
            else
            {
                // Gives error message as no audio and/or video devices found
                MessageBox.Show("No Video/Audio capture devices have been found.", "Warning");
                toolStripStatusLabel1.Text = "No Video/Audio capture devices have been found.";
            }
        }

        private void btnStartStopRecording_Click(object sender, EventArgs e)
        {
            // Is it Recoring ?
            if (_bStartedRecording)
            {
                // Yes
                // Stops encoding
                _job.StopEncoding();
                btnStartStopRecording.Text = "Start Recording";
                toolStripStatusLabel1.Text = "";
                _bStartedRecording = false;
            }
            else
            {
                // Sets up publishing format for file archival type
                FileArchivePublishFormat fileOut = new FileArchivePublishFormat();

                // Sets file path and name
                fileOut.OutputFileName = String.Format("C:\\WebCam{0:yyyyMMdd_hhmmss}.wmv", DateTime.Now);
                
                // Adds the format to the job. You can add additional formats as well such as
                // Publishing streams or broadcasting from a port
                _job.PublishFormats.Add(fileOut);

                // Start encoding
                _job.StartEncoding();

                btnStartStopRecording.Text = "Stop Recording";
                toolStripStatusLabel1.Text = fileOut.OutputFileName;
                _bStartedRecording = true;
            }
        }

        private void cmdGrabImage_Click(object sender, EventArgs e)        
        {
            // Create a Bitmap of the same dimension of panelVideoPreview (Width x Height)
            using (Bitmap bitmap = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height))
            { 
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Get the paramters to call g.CopyFromScreen and get the image
                    Rectangle rectanglePanelVideoPreview = panelVideoPreview.Bounds;
                    Point sourcePoints = panelVideoPreview.PointToScreen(new Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y));
                    g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size); 
                }

                string strGrabFileName = String.Format("C:\\Snapshot_{0:yyyyMMdd_hhmmss}.jpg", DateTime.Now);
                toolStripStatusLabel1.Text = strGrabFileName;
                bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg);                
            } 
        }

        private void Broadcast_Click(object sender, EventArgs e)
        {
            EncoderDevice video = null;
            EncoderDevice audio = null;

            GetSelectedVideoAndAudioDevices(out video, out audio);
            StopJob();

            if (video == null)
            {
                return;
            }
            
            _job = new LiveJob();

            _deviceSource = _job.AddDeviceSource(video, audio);
            _job.ActivateSource(_deviceSource);         
            
            // Finds and applys a smooth streaming preset        
            _job.ApplyPreset(LivePresets.VC1256kDSL16x9);

            // Creates the publishing format for the job
            PullBroadcastPublishFormat format = new PullBroadcastPublishFormat();
            format.BroadcastPort = 8080;
            format.MaximumNumberOfConnections = 2;

            // Adds the publishing format to the job
            _job.PublishFormats.Add(format);

            // Starts encoding
            _job.StartEncoding();

            toolStripStatusLabel1.Text = "Broadcast started on localhost at port 8080, run WpfShowBroadcast.exe now to see it";
        }

        private void GetSelectedVideoAndAudioDevices(out EncoderDevice video, out EncoderDevice audio)
        {
            video = null;
            audio = null;

            lblVideoDeviceSelectedForPreview.Text = "";
            lblAudioDeviceSelectedForPreview.Text = "";

            if (lstVideoDevices.SelectedIndex < 0 || lstAudioDevices.SelectedIndex < 0)
            {
                MessageBox.Show("No Video and Audio capture devices have been selected.\nSelect an audio and video devices from the listboxes and try again.", "Warning");
                return;
            }

            // Get the selected video device            
            foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
            {
                if (String.Compare(edv.Name, lstVideoDevices.SelectedItem.ToString()) == 0)
                {
                    video = edv;
                    lblVideoDeviceSelectedForPreview.Text = edv.Name;
                    break;
                }
            }

            // Get the selected audio device            
            foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
            {
                if (String.Compare(eda.Name, lstAudioDevices.SelectedItem.ToString()) == 0)
                {
                    audio = eda;
                    lblAudioDeviceSelectedForPreview.Text = eda.Name;
                    break;
                }
            }
        }

        void StopJob()
        {
            // Has the Job already been created ?
            if (_job != null)
            {
                // Yes
                // Is it capturing ?
                //if (_job.IsCapturing)
                if (_bStartedRecording)
                {
                    // Yes
                    // Stop Capturing
                    btnStartStopRecording.PerformClick();
                }

                _job.StopEncoding();

                // Remove the Device Source and destroy the job
                _job.RemoveDeviceSource(_deviceSource);

                // Destroy the device source
                _deviceSource.PreviewWindow = null;                
                _deviceSource = null;                
            }
        }

        private void frmEE4WebCam_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopJob();
        }
    }
}
Source of this code: http://www.codeproject.com/Articles/202 ... x4899348xx

I was ask to the owner but he is not respond yet. so anyone please help me.
Here are the code that i was edit:
Code: Select all
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Diagnostics
 
'Reference path for the following assemblies --> C:\Program Files\Microsoft Expression\Encoder 4\SDK\
Imports Microsoft.Expression.Encoder.Devices
Imports Microsoft.Expression.Encoder.Live
Imports Microsoft.Expression.Encoder
 
Public Class Form1
    Private LiveJob As Global.Microsoft.Expression.Encoder.Live.LiveJob
    Private LiveDeviceSource As LiveDeviceSource
    'Private bStartedRecording As Boolean = False

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.ClearSelected()
        For Each edv In EncoderDevices.FindDevices(EncoderDeviceType.Video)
            ListBox1.Items.Add(edv.Name)
        Next
 
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If (ListBox1.SelectedIndex < 0) Then
            MessageBox.Show("No Video capture devices have been selected.\nSelect a video devices from the listboxes and try again.", "Warning")
            Return
        End If
 
        Dim audio As LiveDeviceSource.EnableInputAudioLine(x)
        For Each edv In EncoderDevices.FindDevices(EncoderDeviceType.Video)
            If (String.Compare(edv.Name, ListBox1.SelectedItem.ToString()) = 0) Then
                video = edv
                'break()

                'Checks for a/v devices
                LiveDeviceSource = LiveJob.AddDeviceSource(edv, audio)
 
                'No
                'Setup the video resolution and frame rate of the video device
                'NOTE: Of course, the resolution and frame rate you specify must be supported by the device!
                'NOTE2: May be not all video devices support this call, and so it just doesn't work, as if you don't call it (no error is raised)
                'NOTE3: As a workaround, if the .PickBestVideoFormat method doesn't work, you could force the resolution in the 
                'following instructions (called few lines belows): 'panelVideoPreview.Size=' and '_job.OutputFormat.VideoProfile.Size=' 
                'to be the one you choosed (640, 480).
                LiveDeviceSource.PickBestVideoFormat(New Size(640, 480), 15)
 
                'Get the properties of the device video
                Dim sp As SourceProperties = LiveDeviceSource.SourcePropertiesSnapshot()
 
                'Resize the preview panel to match the video device resolution set
                Panel1.Size = New Size(sp.Size.Width, sp.Size.Height)
 
                'Setup the output video resolution file as the preview
                LiveJob.OutputFormat.VideoProfile.Size = New Size(sp.Size.Width, sp.Size.Height)
 
                'Sets preview window to winform panel hosted by xaml window
                'PreviewWindow = New PreviewWindow(New HandleRef(Panel1, Panel1.Handle))

                'Make this source the active one
                LiveJob.ActivateSource(LiveDeviceSource)
 
                'Button1.Enabled = True
            End If
        Next
 

    End Sub
End Class
My problem are: 1. how to remove audio option. I try with "Dim audio As LiveDeviceSource.EnableInputAudioLine(x)" but i dont know about the value of integer x
2. Variable video is not declared. I try to declare it with string, integer, boolean, char cannot work either
3. Can you make stop capture more friendly code? because i cant found how to use NULL option in VB

Thanks
Albertus Chandra Wijayanto
Mechanical Engineering
University Indonesia
+62 8811898921
albertuschw@yahoo.com
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: [HELP] Webcam Problem
clanc789
This thread is posted in the 'Tutorials' sections, I think you should've posted it in the 'Coding and Help' section.

1. Can not help you with that, sorry, you can check the contents of the argument of the EnableInputAudioline() (so what does x represent?)
2. In the first code you provided "video" is declared as "Encodr Device video = null;"
3. Can not help you with that.

I can not help much since this code is either C# or C++, and I know nothing of that.

Do keep in mind as well that 'asking for help' is not the same as 'please make this for me'. Not saying you are trying to make us a lot of your program, but the way you phrase it, it seems a bit like it.

I hope you succeed with your application as I know how frustrating it can be when you're stuck in a piece of code.

Cheers
Practice makes perfect!

VIP since: 6-10-2011
User avatar
albertuschw
New Member
New Member
Posts: 13
Joined: Thu Sep 11, 2014 8:46 am

Re: [HELP] Webcam Problem
albertuschw
Thanks for reply and sorry for my wrong phrase and wrong directory on forum idoit;
comment for answer: 1. in object browser it said
Code: Select all
Public Sub EnableInputAudioLine(audioLineIndex As Integer)
     Member of Microsoft.Expression.Encoder.Live.LiveDeviceSource
Summary:
Sets the current audio input line.

Parameters:
audioLineIndex: The audio line to make active
, i think it code number for device?
2. I still dont know how to use that Encoder Device function and is that Null function on VB.net?
3. Ok. Thx

Yes that's why i trying here cryer;

How about my question on Easy Webcam on top post? puko;
Thank for your attention and kindness cooll;
Albertus Chandra Wijayanto
Mechanical Engineering
University Indonesia
+62 8811898921
albertuschw@yahoo.com
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: [HELP] Webcam Problem
CodenStuff
This is the application converted to VB but I'm not sure how to do what you're asking for :?

I found webcams have always been annoying to work with in VB.
ECam.zip
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
albertuschw
New Member
New Member
Posts: 13
Joined: Thu Sep 11, 2014 8:46 am

Re: [HELP] Webcam Problem
albertuschw
thx admin boogy;
that code flowchart was choose video device and audio device then preview on form,
i just need choose video device only, so i want to remove option for audio device, but on preview code
Code: Select all
_deviceSource = _job.AddDeviceSource(video, audio)
maybe there was another code for preview without audio, or just give random or primary audio on laptop. But i don't know how to do that. :(
Albertus Chandra Wijayanto
Mechanical Engineering
University Indonesia
+62 8811898921
albertuschw@yahoo.com
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: [HELP] Webcam Problem
clanc789
Currently not at home but I'll take a look at the converted app when I am home since I do understand VB.NET way better than the other language :)

To summarize: you want the audio part removed or also the 3 questions above answered? Because that is a bit unclear for me right now.

I see forward to your reply :)
Practice makes perfect!

VIP since: 6-10-2011
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: [HELP] Webcam Problem
mandai
albertuschw wrote:
I use code for camera from VB with Easy Webcam on viewtopic.php?f=38&t=9406&p=68862&hilit=webcam#p68862

Can i choose which camera i want to use? camera on my laptop or my USB webcam
If you want to pick a second webcam device you would need to set the iDevice variable (it seems the author did not add this in the original code).
User avatar
albertuschw
New Member
New Member
Posts: 13
Joined: Thu Sep 11, 2014 8:46 am

Re: [HELP] Webcam Problem
albertuschw
thx clanc789 :)
the main target was: I want the audio part removed
but on the progress to do that, i just caught that 3 question.
I use 2 code to view video from webcam, that was converted and easy webcam, if you can answer my question on top post (about viewtopic.php?f=21&t=9297&p=68009&hilit=webcam#p68009), you may skip that about another code.
Albertus Chandra Wijayanto
Mechanical Engineering
University Indonesia
+62 8811898921
albertuschw@yahoo.com
User avatar
albertuschw
New Member
New Member
Posts: 13
Joined: Thu Sep 11, 2014 8:46 am

Re: [HELP] Webcam Problem
albertuschw
mandai wrote:
albertuschw wrote:
I use code for camera from VB with Easy Webcam on viewtopic.php?f=38&t=9406&p=68862&hilit=webcam#p68862

Can i choose which camera i want to use? camera on my laptop or my USB webcam
If you want to pick a second webcam device you would need to set the iDevice variable (it seems the author did not add this in the original code).
hey thx for coming to this post too.
that question was same one with i Private Message you sir.
how to do that sir?
Albertus Chandra Wijayanto
Mechanical Engineering
University Indonesia
+62 8811898921
albertuschw@yahoo.com
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: [HELP] Webcam Problem
mandai
albertuschw wrote:
how to do that sir?
You could use this code:
Code: Select all
    Private Sub lstDevices_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstDevices.SelectedIndexChanged

        If lstDevices.SelectedIndex > -1 Then
            iDevice = lstDevices.SelectedIndex
        End If
    End Sub
13 posts Page 1 of 2
Return to “Coding Help & Support”