ozeki camera is not saving current image

42 views Asked by At

I have a camera device in camera device there is button when i press the button the image is save i have use the function for capering image videoDevice_SnapshotFrame but when i click on the camera device button the image is save in the last guna2Button2_Click image which i capture with form camera save button this is a code for save image with camera device

private void videoDevice_SnapshotFrame(object sender, NewFrameEventArgs eventArgs)
            {
                if (InvokeRequired)
                {
                    // If we're not on the UI thread, invoke the method on the UI thread
                    Invoke(new Action(() => videoDevice_SnapshotFrame(sender, eventArgs)));
                    return;
                }
                if (!string.IsNullOrWhiteSpace(patientIDInput))
                {
                    _cameraType = "Camera";
                    DermaChecks(sender, null);
    
                    // Define the folder path where you want to save the image
                    string folderPath = folderNewPath; // Replace with your desired folder path
    
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
    
                    int MaxCount = 5;
                    int Count = 0;
                    var snapshot = snapshotHandler.TakeSnapshot();
    
                    while (snapshot == null && Count < MaxCount)
                    {
                        snapshot = snapshotHandler.TakeSnapshot();
                        Count++;
    
                        if (snapshot == null)
                        {
                            System.Threading.Thread.Sleep(1000);
                        }
                    }
    
                    if (snapshot != null)
                    {
    
                        var img = (System.Drawing.Image)snapshot.ToImage();
    
                        if (img != null)
                        {
                            int newWidth = 1350; // Replace with your desired width
                            int newHeight = 650; // Replace with your desired height
    
                            //Resize the image
                            var resizedImg = new Bitmap(img, newWidth, newHeight);
    
                            // Generate a file name using a custom format
                            string fileName = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss tt") + ".jpg";
                            string filePath = Path.Combine(folderPath, fileName);
                            resizedImg.Save(filePath, ImageFormat.Jpeg);
                            resizedImg.Dispose();
    
                            //Save the image in the created folder
                            guna2HtmlLabel1.Text = "Saved to: " + filePath;
                            ShowMessageBoxNew("Image saved successfully.", "Caption", 3);
                        }
                        else
                        {
                            MessageBox.Show("Failed to capture the snapshot.");
                        }
    
    
                    }
                    else
                    {
                        MessageBox.Show("First You Have To Select Form Image Capture.");
                    }
    
                    _cameraType = "Camera";
                    Dermascope_Load(sender, null);
                }
     else
                {
                    MessageBox.Show("Please enter a patient ID and select a visual device before saving.");
                }
    }
     private void DermaChecks(object sender, EventArgs e)
        {
            if (_cameraTypes != "Camera")
            {
                //_cameraTypes = "Aforge";
                _cameraTypes = "ozeki";
                Disconnects();
                _camera = new OzekiCamera(globalCamera);
                _connector.Connect(_camera.VideoChannel, zoom);
                _connector.Connect(zoom, _Provider);
    
                // For Recording and SnapShot
    
                _connector.Connect(zoom, snapshotHandler);
    
                videoSender = zoom;     // zoom for recording with zoom feature, otherwise _camera.VideoChannel
    
                zoom.Start();
    
                Viewerozeki.Show();
                Viewerozeki.BringToFront();
                Viewerozeki.Start();
                _camera.Start();
                Viewerozeki.BringToFront();
                guna2HtmlLabel1.BringToFront();
            }
            else
            {
                //_cameraTypes = "Ozeki";
                Disconnects();
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                videoDevice = new AForge.Video.DirectShow.VideoCaptureDevice(_camera.Moniker);
                videoCapabilities = videoDevice.VideoCapabilities;
                snapshotCapabilities = videoDevice.SnapshotCapabilities;
                if (videoDevices != null)
                {
    
                    if ((snapshotCapabilities != null) && (snapshotCapabilities.Length != 0))
                    {
                        videoDevice.ProvideSnapshots = true;
    
                        videoDevice.SnapshotFrame += new NewFrameEventHandler(videoDevice_SnapshotFrame);
                    }
    
                    videoSourcePlayer.VideoSource = videoDevice;
                    _camera.Stop();
                    Viewerozeki.SendToBack();
                    videoSourcePlayer.BringToFront();
                    //videoSourcePlayer.NewFrame += videoDevice_SnapshotFrame;
                    videoSourcePlayer.Start();
                    guna2HtmlLabel1.BringToFront();
                    if (!string.IsNullOrWhiteSpace(patientIDInput))
                    {
    
                        // Define the folder path where you want to save the image
                        string folderPath = folderNewPath; // Replace with your desired folder path
    
                        if (!Directory.Exists(folderPath))
                        {
                            Directory.CreateDirectory(folderPath);
                        }
    
                        int MaxCount = 15;
                        int Count = 0;
                        var snapshot = snapshotHandler.TakeSnapshot();
    
                        while (snapshot == null && Count < MaxCount)
                        {
                            snapshot = snapshotHandler.TakeSnapshot();
                            Count++;
    
                            if (snapshot == null)
                            {
                                System.Threading.Thread.Sleep(1000);
                            }
                        }
    
                        if (snapshot != null)
                        {
                            var img = (System.Drawing.Image)snapshot.ToImage();
    
                            if (img != null)
                            {
                                int newWidth = 1350; // Replace with your desired width
                                int newHeight = 650; // Replace with your desired height
    
                                //Resize the image
                                var resizedImg = new Bitmap(img, newWidth, newHeight);
    
                                // Generate a file name using a custom format
                                string fileName = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss tt") + ".jpg";
                                string filePath = Path.Combine(folderPath, fileName);
                                resizedImg.Save(filePath, ImageFormat.Jpeg);
                                resizedImg.Dispose();
                                //Save the image in the created folder
                                //img.Save(filePath, ImageFormat.Jpeg);
                                //img.Dispose();
                                //ShowAutoClosingMessageBox("Image saved successfully.", "Caption");
                                //MessageBox.Show("Image saved successfully.");
                                //ShowSecondMessageBox("Image saved successfully.", "Caption", 3);
                                guna2HtmlLabel1.Text = "Saved to: " + filePath;
                                MessageBox.Show("Image saved successfully");
                                //ShowMessageBoxNew("Image saved successfully.", "Caption", 3);
    
    
                            }
                            else
                            {
                                MessageBox.Show("Failed to capture the snapshot.");
                            }
                        }
                    }
            }
        }
        }
     private void Disconnects()
        {
            if (_cameraTypes != "Ozeki")
            {
                if (videoSourcePlayer.VideoSource != null)
                {
                    videoSourcePlayer.SignalToStop();
                    //videoSourcePlayer.WaitForStop();
                    videoSourcePlayer.VideoSource = null;
    
                    if (videoDevice.ProvideSnapshots)
                    {
                        videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame);
                    }
                }
            }
            else
            {
                if (videoSourcePlayer.VideoSource != null)
                {
                    videoSourcePlayer.SignalToStop();
                    videoSourcePlayer.WaitForStop();
                    videoSourcePlayer.VideoSource = null;
    
                    if (videoDevice.ProvideSnapshots)
                    {
                        videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame);
                    }
                }
            }
        }

this is code when i click on the form save button

private void guna2Button2_Click(object sender, EventArgs eventArgs)
        {            
            guna2Button2.BackgroundImage = global::Sojro.Properties.Resources._4383919;
            if (!string.IsNullOrWhiteSpace(patientIDInput))
            {
                _cameraType = "camera";
                Dermascope_Load(sender, null);

                // Define the folder path where you want to save the image
                string folderPath = folderNewPath; // Replace with your desired folder path

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                int MaxCount = 5;
                int Count = 0;
                var snapshot = snapshotHandler.TakeSnapshot();

                while (snapshot == null && Count < MaxCount)
                 {
                    snapshot = snapshotHandler.TakeSnapshot();
                    Count++;

                    if (snapshot == null)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }

                if (snapshot != null)
                {
                    if (testing == false)
                    {
                        testing = true;
                        guna2Button2.PerformClick();
                    }
                    else
                    {
                        var img = (System.Drawing.Image)snapshot.ToImage();

                        if (img != null)
                        {
                            int newWidth = 1350; // Replace with your desired width
                            int newHeight = 650; // Replace with your desired height

                            //Resize the image
                            var resizedImg = new Bitmap(img, newWidth, newHeight);

                            // Generate a file name using a custom format
                            string fileName = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss tt") + ".jpg";
                            string filePath = Path.Combine(folderPath, fileName);
                            resizedImg.Save(filePath, ImageFormat.Jpeg);
                            resizedImg.Dispose();                       
                            guna2HtmlLabel1.Text = "Saved to: " + filePath;
                            ShowMessageBoxNew("Image saved successfully.", "Caption", 3);
                            testing = false;
                        }
                        else
                        {
                            MessageBox.Show("Failed to capture the snapshot.");
                        }
                    }
                    
                }
       _cameraType = "Camera";
                Dermascope_Load(sender, null);
            }
            else
            {
                MessageBox.Show("Please enter a patient ID and select a visual device before saving.");
            }
            guna2Button2.BackgroundImage = global::Sojro.Properties.Resources.image;
        }   
    private void Dermascope_Load(object sender, EventArgs e)
        {
            if (_cameraType != "Camera")
            {
                //_cameraTypes = "Aforge";
                Disconnect();

                _camera = new OzekiCamera(globalCamera);
                _connector.Connect(_camera.VideoChannel, zoom);
                _connector.Connect(zoom, _Provider);

                //For Recording and SnapShot

                _connector.Connect(zoom, snapshotHandler);

                videoSender = zoom;     //zoom for recording with zoom feature, otherwise _camera.VideoChannel

                zoom.Start();

                Viewerozeki.Show();
                Viewerozeki.BringToFront();
                Viewerozeki.Start();
                _camera.Start();
                Viewerozeki.BringToFront();
                guna2HtmlLabel1.BringToFront();

            
            }
            else
            {
                //_cameraTypes = "Ozeki";
                Disconnect();
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                videoDevice = new AForge.Video.DirectShow.VideoCaptureDevice(_camera.Moniker);
                videoCapabilities = videoDevice.VideoCapabilities;
                snapshotCapabilities = videoDevice.SnapshotCapabilities;
                if (videoDevices != null)
                {

                    if ((snapshotCapabilities != null) && (snapshotCapabilities.Length != 0))
                    {
                        videoDevice.ProvideSnapshots = true;
                        
                        videoDevice.SnapshotFrame += new NewFrameEventHandler(videoDevice_SnapshotFrame);
                    }

                    videoSourcePlayer.VideoSource = videoDevice;
                    _camera.Stop();
                    Viewerozeki.SendToBack();
                    videoSourcePlayer.BringToFront();
                    //videoSourcePlayer.NewFrame += videoDevice_SnapshotFrame;
                    videoSourcePlayer.Start();
                    guna2HtmlLabel1.BringToFront();

                }
            }

        }
 private void Disconnect()
        {
            if (videoSourcePlayer.VideoSource != null)
            {
                // stop video device
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
                videoSourcePlayer.VideoSource = null;

                if (videoDevice.ProvideSnapshots)
                {
                    videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame);
                }

            }
        }
0

There are 0 answers