• Source
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.IO;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. using AForge.Video;
    12. using AForge.Video.DirectShow;
    13.  
    14. namespace WebCamUygulamasi
    15. {
    16. public partial class FormWebCam : Form
    17. {
    18. public FormWebCam()
    19. {
    20. InitializeComponent();
    21. }
    22.  
    23. private FilterInfoCollection webcamsayisi;
    24. private VideoCaptureDevice kamera;
    25.  
    26. private void FormWebCam_Load(object sender, EventArgs e)
    27. {
    28. webcamsayisi = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    29. foreach (FilterInfo videocapturedevice in webcamsayisi)
    30. {
    31. comboBoxKameralar.Items.Add(videocapturedevice.Name);
    32. }
    33. comboBoxKameralar.SelectedIndex = 0;
    34. }
    35.  
    36. private void kullanilacakcihaz_NewFrame(object sender, NewFrameEventArgs eventArgs)
    37. {
    38. Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
    39. pictureBoxKamera.Image = bit;
    40.  
    41. }
    42. private void btnkamerabaslat_Click(object sender, EventArgs e)
    43. {
    44. kamera = new VideoCaptureDevice(webcamsayisi[comboBoxKameralar.SelectedIndex].MonikerString);
    45. kamera.NewFrame += new NewFrameEventHandler(kullanilacakcihaz_NewFrame);
    46. kamera.Start();
    47. }
    48.  
    49. private void btnkameradurdur_Click(object sender, EventArgs e)
    50. {
    51. if (kamera.IsRunning) //kamera açıksa kapatıyoruz.
    52. {
    53. kamera.Stop();
    54. }
    55.  
    56. }
    57.  
    58. private void btnkamerakaydet_Click(object sender, EventArgs e)
    59. {
    60. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    61. saveFileDialog1.Filter = "Jpeg Resmi|*.jpg|Bitmap Resmi|*.bmp|Gif Resmi|*.gif";
    62. saveFileDialog1.Title = "Resmi Kaydet";
    63. saveFileDialog1.ShowDialog();
    64.  
    65. if (saveFileDialog1.FileName != "")
    66. {
    67. FileStream DosyaAkisi = (FileStream)saveFileDialog1.OpenFile();
    68.  
    69. switch (saveFileDialog1.FilterIndex)
    70. {
    71. case 1:
    72. pictureBoxKamera.Image.Save(DosyaAkisi, System.Drawing.Imaging.ImageFormat.Jpeg);
    73. break;
    74.  
    75. case 2:
    76. pictureBoxKamera.Image.Save(DosyaAkisi, System.Drawing.Imaging.ImageFormat.Bmp);
    77. break;
    78.  
    79. case 3:
    80. pictureBoxKamera.Image.Save(DosyaAkisi, System.Drawing.Imaging.ImageFormat.Gif);
    81. break;
    82. }
    83. DosyaAkisi.Close();
    84. }
    85. }
    86.  
    87. private void FormWebCam_FormClosing(object sender, FormClosingEventArgs e)
    88. {
    89. if (kamera.IsRunning)
    90. {
    91. kamera.Stop();
    92. }
    93. }
    94. }
    95. }