Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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;
            }
        }
    }

>and this is a snapshotForm
C#
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
C#
 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.");
                    }
                }
}


What I have tried:

Can any one help me with the above issue please
Posted
Updated 28-Nov-23 6:18am
v3
Comments
Richard MacCutchan 27-Nov-23 4:11am    
In the last code sample above you are changing the image size, maybe that is the issue.
Babar Ali Nov2023 27-Nov-23 4:23am    
No i have tried every thing but image is cutting i have add a picture in stack over flow i am giving you the link check https://stackoverflow.com/questions/77541217/aforge-net-when-i-save-image-with-camera-device-the-image-is-not-saving-image-in
i have describe in detail
Richard MacCutchan 27-Nov-23 4:42am    
You need to show just the code that does not work and explain exactly what the problem is. Just saying that the image is the wrong size does not help. You need to explain exactly what size you expect and how that is different from what you see. Please use the Improve question link above, and add complete details of what is not working.
Babar Ali Nov2023 27-Nov-23 5:39am    
no code is working fine but the image is not saving in original form as I have show in the link https://stackoverflow.com/questions/77541217/aforge-net-when-i-save-image-with-camera-device-the-image-is-not-saving-image-in
[no name] 27-Nov-23 11:39am    
I'd let the camera do it's job and pick up whatever "files" it creates by default instead of getting "in between" first. You're working with "stills" after all.

1 solution

PLEASE TRY REPLACING SOME OF YOUR CODE WITH THE FOLLOWING:

//use GUID to get unique name for every capture file. Something like:

string guid = Guid.NewGuid().ToString();
captureBitmap.Save(@"D:\Capture-" + guid + ".jpg",ImageFormat.Jpeg);

//OR

//use current date and time for that, like this:

string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
captureBitmap.Save(@"D:\Capture-" + timestamp + ".jpg",ImageFormat.Jpeg);

//Thank you and try again!!!
 
Share this answer
 
Comments
Richard MacCutchan 5-Dec-23 8:20am    
This has nothing to do with the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900