-1

So I am using this one library that I found online that is supposed to make it super easy to use my webcam with C#, so I decided to give it a go. The project I am mimicing is using WPF or WinForms, there are two of them. I decided I wanted to create a console version of this, now there are a few minor issue, regarding controls and what not but I seem to be all of that under control the issue I am facing is that it's stepping out of its logic for reasons unknown.

This is how it's supposed to flow. (and it does in WPF)

webcam.Start(); -> webcam.Start(0); Starts the webcam here -> webcam_ImageCaptured() -> LoadBitmap() -> webcam_ImageCaptured() -> LoadBitmap() -> webcam_ImageCaptured() and loop through those last two

And here is what mine does (Console Application)

webcam.Start(); -> webcam.Start(0); Starts the webcam here -> and then it steps out of webcam.Start(); and closes.

I have no idea what so ever to why it behaves like this and I would really like to know, Been debugging back and forth but I really cant seem to solve the mystery here.

Here is the Console applications Main

class Program
    {
        static WebCam webcam;

        static System.Windows.Controls.Image image = new System.Windows.Controls.Image();
        [STAThread]
        static void Main(string[] args)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref image);

            webcam.Start();
        }
    }

And the WebCam class https://ghostbin.com/paste/kvvx2

And the Helper class https://ghostbin.com/paste/vk8qz

And this is the Main for the WPF application

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        WebCam webcam;

        private void mainFrm_Loaded(object sender, RoutedEventArgs e)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgMain);
        }

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            webcam.Start();
        }
    }

Here is the original post where I found everything https://sites.google.com/site/webcamlibrarydotnet/wpf-and-csharp-sample-code-and-download

JohnA
  • 564
  • 1
  • 5
  • 20
  • From your comment below: _"it's not supposed to step out of webcam.Start();"_ -- why not? In the WPF program, it seems almost certain that it does, i.e. the `Start()` method returns shortly after being called. Otherwise the UI would freeze up. So, why do you think the `Start()` method shouldn't return in the console program scenario? – Peter Duniho Feb 01 '18 at 06:56
  • And to be clear, from all indications in what you've posted so far, this is nothing more than the `Main()` method returning immediately, causing the program to exit, which is addressed by the marked duplicate. If you believe otherwise, you need to improve the question so it's clear why we should believe the `Start()` method should block and keep the program running. – Peter Duniho Feb 01 '18 at 06:59
  • @PeterDuniho I was debugging them side by side, the console application closes after it ran once while the WPF version does not step out, is there a way I could capture a frame with the Console application to see if its even getting anything? – JohnA Feb 01 '18 at 07:04
  • The WPF program has an event-pumping loop (hidden from you...it's in the `Application.Run()` method that is implicitly called by your own `App` class), which keeps the program running. You call `Start()` in the `btnCapture_Click()` method, which is apparently the event handler for the `Click` event on a button in your WPF UI. That method isn't the entry point of the program, the way `Main()` is the entry point of a console program, and so returning from it doesn't terminate the WPF program the way returning from `Main()` in your console program would. – Peter Duniho Feb 01 '18 at 07:08
  • @PeterDuniho That explains a few things, I also tried using the SaveImageCapture like this `Helper.SaveImageCapture((BitmapSource)image.Source);` but that threw me a error at line `encoder.Frames.Add(BitmapFrame.Create(bitmap));` saying `System.ArgumentNullException: 'Value cannot be null. Parameter name: source'` I'm pretty sure I casted my image.Source as a BitmapSource – JohnA Feb 01 '18 at 07:11

1 Answers1

0

In console application, add Console.ReadLine() at the end, so app remains running.

 static void Main(string[] args)
         {
             webcam = new WebCam();
             webcam.InitializeWebCam(ref image);

             webcam.Start();
             Console.ReadLine();
         }

Then just ensure, that your webcam_ImageCaptured - events are firing. Even if Start-method returns immediately, it might be designed to behave so that calling thread is not blocked.

Risto M
  • 2,919
  • 1
  • 14
  • 27
  • I did, but it still keeps stepping out, it's not supposed to step out of webcam.Start(); – JohnA Feb 01 '18 at 06:50
  • Do you get those webcam_ImageCaptured - events in your console-app. Even if Start-method call returns immediately, maybe camera is still working? – Risto M Feb 01 '18 at 06:56
  • It starts the webcam, it turns on because the light turns green, there is no way to display it though beacuse no controls, but I want to maybe save a frame to the clipboard just to test it out. – JohnA Feb 01 '18 at 06:59