Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends ,

here iam going to do in my project saving a image through webcam into a folder/database viceversa retriving also for displaying . please help me out how to solve this problem.


here i tried this one but not able to save an image to a folder

code:
C#
namespace SilverlightDemo
{
    public partial class MainPage : UserControl
    {
        CaptureSource _captureSource;
        ObservableCollection<writeablebitmap> _images = new ObservableCollection<writeablebitmap>();

        private WriteableBitmap selectedSnapshot;
        public WriteableBitmap SelectedSnapshot
        {
            get { return selectedSnapshot; }
            set { selectedSnapshot = value; }
        }

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);            
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Get list of the VIDEO Sources and bind
            VideoSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();

            // Get list of the AUDIO Sources and bind
            AudioSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices();

            // Select the default devices
            if (VideoSources.Items.Count > 0)
                VideoSources.SelectedIndex = 0;

            if (AudioSources.Items.Count > 0)
                AudioSources.SelectedIndex = 0;

            // Creating CaptureSource
            _captureSource = new CaptureSource();

            // Handle CaptureImageAsync Completed event handler
            _captureSource.CaptureImageCompleted += (s, ev) =>
            {
                ProcessImage(ev.Result);
            };

            // Bind snapshots
            Snapshots.ItemsSource = _images;

            // Disable the capture button, it'll be enabled when capture source is ready
            CaptureWebcam.IsEnabled = false;
            SaveImage.IsEnabled = false;
        }

        private void StartStopWebcam_Click(object sender, RoutedEventArgs e)
        {
            StartStopWebcamCapture();
        }

        private void CaptureWebcam_Click(object sender, RoutedEventArgs e)
        {
            // Verify the device is started
            if (_captureSource.VideoCaptureDevice != null && _captureSource.State == CaptureState.Started)
            {
                // Capture the current frame
                _captureSource.CaptureImageAsync();
            }
        }

        private void StartStopWebcamCapture()
        {
            try
            {
                if (_captureSource != null)
                {
                    //Start Capturing
                    if (_captureSource.State != CaptureState.Started)
                    {
                        // Set the devices
                        _captureSource.VideoCaptureDevice = (VideoCaptureDevice)VideoSources.SelectedItem;
                        _captureSource.AudioCaptureDevice = (AudioCaptureDevice)AudioSources.SelectedItem;

                        // Video Brush
                        VideoBrush vBrush = new VideoBrush();
                        vBrush.SetSource(_captureSource);
                        Webcam.Fill = vBrush; // Paint the video brush on the rectangle

                        // Request user permission
                        if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
                        {
                            if (_captureSource.VideoCaptureDevice != null)
                            {
                                _captureSource.Start();
                                StartStopWebcam.Content = "Stop Camera";
                                CaptureWebcam.IsEnabled = true;
                            }
                        }
                    }
                    // Stop Capturing
                    else
                    {
                        if (_captureSource.VideoCaptureDevice != null)
                        {
                            _captureSource.Stop();
                            StartStopWebcam.Content = "Start Camera";
                            CaptureWebcam.IsEnabled = false;
                            SaveImage.IsEnabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error using webcam", MessageBoxButton.OK);
            }
        }

        private void ProcessImage(WriteableBitmap bitmap)
        {
            _images.Add(bitmap);
            SelectedSnapshot = bitmap;
            SaveImage.IsEnabled = true;
        }

        private void SaveImage_Click(object sender, RoutedEventArgs e)
        {
            // Gets the current captured raw bitmap
            var rawImage = SelectedSnapshot;

            // If image is selected in the Snapshots ListBox then set it as a save target
            if (Snapshots.SelectedItem != null)
                rawImage = (WriteableBitmap)Snapshots.SelectedItem;

            if (rawImage != null)
            {

                
               // Init the Save File Dialog
                //SaveFileDialog saveDialog = new SaveFileDialog();
                
                //saveDialog.Filter = "Image Files (*.jpg, *.jpeg)|*.jpg;*.jpeg|All Files (*.*)|*.*";
                //saveDialog.DefaultExt = ".jpg";
                //saveDialog.FilterIndex = 1;

                //// Show save dialog to the user
                //if ((bool)saveDialog.ShowDialog())
                //{
                //    using (Stream stream = saveDialog.OpenFile())
                //    {
                //        // Convert raw captured bitmap to the image that Image Tools understand with the extension method
                //        var image = rawImage.ToImage();
                //        // Declare jpeg encoder
                //        var encoder = new JpegEncoder();
                //        // Set the image quality
                //        //encoder.Quality = 90;
                //        // Finally encode raw bitmap and save it as a jpg image
                //        encoder.Encode(image, stream);
                //        // Close the stream
                //        stream.Close();
                    }
                }
            }
        }

    }


please help me out

instead of saving through a savedilog, should save in folder/db.

thanking you.
Posted
Updated 26-Dec-13 1:08am
v4

1 solution

If you are using any Service i.e. RIA, WCF service then you have to write your file writing code there. here is the example of file writing in RIA service.
Just get binaries from your snapshot and call this function written in service with parameters of binary and file name you want to save.


C#
public bool Upload(string p_szfileName, Byte[] p_bImage)
        {
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath = string.Empty;
            try
            {
                string szDirectory = WebConfigurationManager.AppSettings["VirtualDirectory"].ToString();
                filePath = HttpContext.Current.Server.MapPath(szDirectory + "Your Path--") + p_szfileName;                  

                if (p_bImage != null)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream);
                    writer.Write(p_bImage);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();
            }
        }
 
Share this answer
 
v2

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