Aforge.net when i save image with camera device the image is not saving image in original location and cutting

91 Views Asked by At

I have a camera device in device there is button in camera to capture the image I have having a issue when I capture the image with camera device the camera location is not in original size and image is cutting when I click on the form save button the image location is in original size and image is not cutting can anyone can help this is code when I capture the image with camera device button

private void videoDevice_SnapshotFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Console.WriteLine(eventArgs.Frame.Size);

        ShowSnapshot((Bitmap)eventArgs.Frame.Clone());

    }

    private void ShowSnapshot(Bitmap snapshot)
    {
        if (InvokeRequired)
        {
            Invoke(new Action<Bitmap>(ShowSnapshot), snapshot);
        }
        else
        {
            if (snapshotForm == null)
            {
                snapshotForm = new SnapshotForm();
                snapshotForm.FormClosed += new FormClosedEventHandler(snapshotForm_FormClosed);
                snapshotForm.Show();
            }

            snapshotForm.SetImage((Bitmap)snapshot.Clone()); // Use a clone to avoid modifying the original image

            if (!string.IsNullOrWhiteSpace(patientIDInput))
            {
                string folderPath = folderNewPath;

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                string fileName = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss") + ".jpg";
                string filePath = Path.Combine(folderPath, fileName);

                snapshot.Save(filePath, ImageFormat.Jpeg);

                ShowMessageBoxNew("Image saved successfully.", "Caption", 3);
                guna2HtmlLabel1.Text = "Saved to: " + filePath;
                snapshotForm.Close();
                snapshotForm = null;
            }
        }
    }

when I capture the image with camera device button when I capture the image with camera device button

and this is a snapshotForm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Sojro.Myforms
{
    public partial class SnapshotForm : Form
    {
        public SnapshotForm()
        {
            InitializeComponent();
        }

        public void SetImage(Bitmap bitmap)
        {
            timeBox.Text = DateTime.Now.ToLongTimeString();
            lock (this)
            {
                Bitmap old = (Bitmap)pictureBox.Image;
                //pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
                pictureBox.Image = bitmap;
                if (old != null)
                {
                    old.Dispose();
                }
            }
        }
       

        private void PictureBox_Click(object sender, EventArgs e)
        {
            Process.Start("umer.exe");
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void SnapshotForm_Load(object sender, EventArgs e)
        {

        }

       

        private void Guna2Button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

and this a code when i save the image with 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)
                {
                    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();
                        ShowMessageBoxNew("Image saved successfully.", "Caption", 3);
                        guna2HtmlLabel1.Text = "Saved to: " + filePath;
                    }
                    else
                    {
                        MessageBox.Show("Failed to capture the snapshot.");
                    }
                }
}

this is same image when I click on the form save button the is not cutting enter image description here this is form which i capture the image with camera device button and save button enter image description here

0

There are 0 best solutions below