Monday, February 28, 2011

SAVE IM AGE TO SQL using WEBCAM

SAVE IM AGE TO SQL using WEBCAM

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Touchless.Vision.Camera;
  10. using System.Data.SqlClient;
  11. using System.IO;
  12. using System.Configuration;
  13.  
  14. namespace RadicalGuardMainMenu
  15. {
  16. public partial class Webcam : Form
  17. {
  18. public Webcam()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private void Webcam_Load(object sender, EventArgs e)
  24. {
  25. comboBoxCameras.DropDownStyle = ComboBoxStyle.DropDownList;
  26. // Refresh the list of available cameras
  27. comboBoxCameras.Items.Clear();
  28. foreach (Camera cam in CameraService.AvailableCameras)
  29. comboBoxCameras.Items.Add(cam);
  30.  
  31. if (comboBoxCameras.Items.Count > 0)
  32. comboBoxCameras.SelectedIndex = 0;
  33.  
  34. }
  35.  
  36.  
  37. private void btnCapture_Click(object sender, EventArgs e)
  38. {
  39. thrashOldCamera();
  40. }
  41.  
  42. private CameraFrameSource _frameSource;
  43. private static Bitmap _latestFrame;
  44.  
  45. private void btnStart_Click(object sender, EventArgs e)
  46. {
  47. // Early return if we've selected the current camera
  48. if (_frameSource != null && _frameSource.Camera == comboBoxCameras.SelectedItem)
  49. return;
  50.  
  51. thrashOldCamera();
  52. startCapturing();
  53. }
  54.  
  55.  
  56. private void startCapturing()
  57. {
  58. try
  59. {
  60. Camera c = (Camera)comboBoxCameras.SelectedItem;
  61. setFrameSource(new CameraFrameSource(c));
  62. _frameSource.Camera.CaptureWidth = 320;
  63. _frameSource.Camera.CaptureHeight = 240;
  64. _frameSource.Camera.Fps = 20;
  65. _frameSource.NewFrame += OnImageCaptured;
  66.  
  67. pictureBoxDisplay.Paint += new PaintEventHandler(drawLatestImage);
  68. _frameSource.StartFrameCapture();
  69. }
  70. catch (Exception ex)
  71. {
  72. comboBoxCameras.Text = "Select A Camera";
  73. MessageBox.Show(ex.Message);
  74. }
  75. }
  76.  
  77. private void drawLatestImage(object sender, PaintEventArgs e)
  78. {
  79. if (_latestFrame != null)
  80. {
  81. // Draw the latest image from the active camera
  82. e.Graphics.DrawImage(_latestFrame, 0, 0, _latestFrame.Width, _latestFrame.Height);
  83. }
  84. }
  85.  
  86. public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
  87. {
  88. _latestFrame = frame.Image;
  89. pictureBoxDisplay.Invalidate();
  90. }
  91.  
  92. private void setFrameSource(CameraFrameSource cameraFrameSource)
  93. {
  94. if (_frameSource == cameraFrameSource)
  95. return;
  96.  
  97. _frameSource = cameraFrameSource;
  98. }
  99.  
  100. //
  101.  
  102. private void thrashOldCamera()
  103. {
  104. // Trash the old camera
  105. if (_frameSource != null)
  106. {
  107. _frameSource.NewFrame -= OnImageCaptured;
  108. _frameSource.Camera.Dispose();
  109. setFrameSource(null);
  110. pictureBoxDisplay.Paint -= new PaintEventHandler(drawLatestImage);
  111. }
  112. }
  113.  
  114. private void btnSave_Click(object sender, EventArgs e)
  115. {
  116. string query = "insert into testImage values @picture";
  117. SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["radicalGuardDB"].ToString());
  118.  
  119. SqlCommand cmd = new SqlCommand(query, con);
  120.  
  121. MemoryStream stream = new MemoryStream();
  122.  
  123. pictureBoxDisplay.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  124.  
  125. byte[] picture = stream.ToArray();
  126.  
  127. cmd.Parameters.AddWithValue("@picture", picture);
  128.  
  129. con.Open();
  130. cmd.ExecuteNonQuery();
  131. con.Close();
  132. }
  133. }
  134. }
     
     
    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... 
    PLZ COMMENT ANY ONE KNOW PLz

No comments:

Post a Comment