Bonjour à tous,
J'ai récupérer une librairie pour gérer l utilisation de la webcam (EasyWebcam) mais je n'arrive pas à afficher la webcam à l'écran. J'ai toujours un message d'erreur qui me dit qu un objet n'a pas été instancié.
voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Media.Imaging; namespace CHAT_Csharp.classes_parametre { public class Helper { //Block Memory Leak [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr handle); public static BitmapSource bs; public static IntPtr ip; public static BitmapSource LoadBitmap(System.Drawing.Bitmap source) { ip = source.GetHbitmap(); bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ip); return bs; } public static void SaveImageCapture(BitmapSource bitmap) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.QualityLevel = 100; // Configure save file dialog box Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "Image"; // Default file name dlg.DefaultExt = ".Jpg"; // Default file extension dlg.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension // Show save file dialog box Nullable<bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save Image string filename = dlg.FileName; FileStream fstream = new FileStream(filename, FileMode.Create); encoder.Save(fstream); fstream.Close(); } } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 using System; using System.IO; using System.Linq; using System.Text; using WebCam_Capture; using System.Windows.Controls; using System.Collections.Generic; using System.Windows.Media.Imaging; namespace CHAT_Csharp.classes_parametre { public class WebCam { private WebCamCapture webcam; private Image _FrameImage; private int FrameNumber = 30; public void InitializeWebCam(ref Image ImageControl) { webcam = new WebCamCapture(); webcam.FrameNumber = ((ulong)(0ul)); webcam.TimeToCapture_milliseconds = FrameNumber; webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured); _FrameImage = ImageControl; } private void webcam_ImageCaptured(object source, WebcamEventArgs e) { _FrameImage.Source = Helper.LoadBitmap((System.Drawing.Bitmap)e.WebCamImage); } public void Start() { try { webcam.TimeToCapture_milliseconds = FrameNumber; webcam.Start(0); } catch (Exception ex) { throw ex; } } public void Stop() { webcam.Stop(); } public void Continue() { // change the capture time frame webcam.TimeToCapture_milliseconds = FrameNumber; // resume the video capture from the stop webcam.Start(this.webcam.FrameNumber); } public void ResolutionSetting() { webcam.Config(); } public void AdvanceSetting() { webcam.Config2(); } } }
C'est lorsque j'utilise la fonction start que ça plante.
Merci d avance
Partager