I read an article about versatile webcam and I successfully worked it out. The thing is I can't save the image from the pictureBoxDisplay to SQL. Please I really need help on this. I'm converting the pictureBox into a byte array but there's an error in this line (line 123), saying that object reference is not set to an instance...

pictureBoxDisplay.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

Here's the complete code

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 Touchless.Vision.Camera;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace RadicalGuardMainMenu
{
    public partial class Webcam : Form
    {
        public Webcam()
        {
            InitializeComponent();
        }

        private void Webcam_Load(object sender, EventArgs e)
        {
            comboBoxCameras.DropDownStyle = ComboBoxStyle.DropDownList;
            // Refresh the list of available cameras
            comboBoxCameras.Items.Clear();
            foreach (Camera cam in CameraService.AvailableCameras)
                comboBoxCameras.Items.Add(cam);

            if (comboBoxCameras.Items.Count > 0)
                comboBoxCameras.SelectedIndex = 0;
           
        }


        private void btnCapture_Click(object sender, EventArgs e)
        {
            thrashOldCamera();
        }

        private CameraFrameSource _frameSource;
        private static Bitmap _latestFrame;

        private void btnStart_Click(object sender, EventArgs e)
        {
            // Early return if we've selected the current camera
            if (_frameSource != null && _frameSource.Camera == comboBoxCameras.SelectedItem)
                return;

            thrashOldCamera();
            startCapturing();
        }


        private void startCapturing()
        {
            try
            {
                Camera c = (Camera)comboBoxCameras.SelectedItem;
                setFrameSource(new CameraFrameSource(c));
                _frameSource.Camera.CaptureWidth = 320;
                _frameSource.Camera.CaptureHeight = 240;
                _frameSource.Camera.Fps = 20;
                _frameSource.NewFrame += OnImageCaptured;

                pictureBoxDisplay.Paint += new PaintEventHandler(drawLatestImage);
                _frameSource.StartFrameCapture();
            }
            catch (Exception ex)
            {
                comboBoxCameras.Text = "Select A Camera";
                MessageBox.Show(ex.Message);
            }
        }

        private void drawLatestImage(object sender, PaintEventArgs e)
        {
            if (_latestFrame != null)
            {
                // Draw the latest image from the active camera
                e.Graphics.DrawImage(_latestFrame, 0, 0, _latestFrame.Width, _latestFrame.Height);
            }
        }

        public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
        {
            _latestFrame = frame.Image;
            pictureBoxDisplay.Invalidate();
        }

        private void setFrameSource(CameraFrameSource cameraFrameSource)
        {
            if (_frameSource == cameraFrameSource)
                return;

            _frameSource = cameraFrameSource;
        }

        //

        private void thrashOldCamera()
        {
            // Trash the old camera
            if (_frameSource != null)
            {
                _frameSource.NewFrame -= OnImageCaptured;
                _frameSource.Camera.Dispose();
                setFrameSource(null);
                pictureBoxDisplay.Paint -= new PaintEventHandler(drawLatestImage);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string query = "insert into testImage values @picture";
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["radicalGuardDB"].ToString());

            SqlCommand cmd = new SqlCommand(query, con);

            MemoryStream stream = new MemoryStream();

            pictureBoxDisplay.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] picture = stream.ToArray();

            cmd.Parameters.AddWithValue("@picture", picture);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

I really need some help.. :(

Recommended Answers

All 3 Replies

I tried to copy the picturebox where the photo had been taken to another picturebox and it shows nothing. It only means that the button capture is setting the picturebox to null to stop the stream of webcam. So now what do I do?

I don't see anywhere in your code that you set the image property of the picturebox control. Is there code missing?

No. I just tried adding a button and put the line

pictureBoxDisplay.Image = anotherpictureBox.Image
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.