Bonjour,
J'essaye d'afficher une caméra de résolution 2592*1944 sur mon écran. Or au bout de quelques dizaines de secondes j'ai un message indiquant mémoire insuffisante qui s'affiche.
Mon but est d'afficher l'image de la caméra en live et de faire un snapshot afin de travailler sur l'image obtenue. Quand je mets en route ma webcam au bout de 50 secondes environs j'ai le message suivant "An error ocurred while capturing the video image. The video capture will now be terminated. une erreur générique s'est produite dans GDI+"
si je fais le snapshot sur l'image alors arrétée et que je relance, j'obtiens l'exception suivante :
code vb.net correspondant (la ligne 61 correspond à encoder.save(ms)) :System.OutOfMemoryException: Mémoire insuffisante pour continuer l'exécution du programme.
à System.Windows.Media.Imaging.BitmapEncoder.SaveFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions, BitmapFrame frame)
à System.Windows.Media.Imaging.BitmapEncoder.Save(Stream stream)
à Placement.Window1.bntCapture_Click(Object sender, RoutedEventArgs e) dans C:\Users\Anthony kamax\Documents\Visual Studio 2013\Projects\Placement3\Placement\Window1.xaml.vb:ligne 61
à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
à System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
à System.Windows.Controls.Primitives.ButtonBase.OnClick()
à System.Windows.Controls.Button.OnClick()
à System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
à System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
à System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
à System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
à System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
à System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
à System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
à System.Windows.Input.InputManager.ProcessStagingArea()
à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
à System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
à System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
à System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
à System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
à System.Windows.Threading.Dispatcher.Run()
à System.Windows.Application.RunDispatcher(Object ignore)
à System.Windows.Application.RunInternal(Window window)
à System.Windows.Application.Run(Window window)
à System.Windows.Application.Run()
à Placement.Application.Main()le code correspondant à webcam.vb :
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 Imports System Imports System.IO Imports System.Linq Imports System.Collections.Generic Imports System.Collections.ObjectModel Imports System.Text Imports System.Windows.Threading Imports System.Windows.Media.Animation Imports System.Threading Imports System.Windows.Resources Imports System.Reflection Imports WebCam_Capture Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Data Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports System.Windows.Shapes Imports System.Drawing Imports System.Drawing.Imaging Imports System.Net Imports System.Threading.Tasks Class Window1 Private bi As BitmapImage Private webcam As WebCam Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) webcam = New WebCam() webcam.InitializeWebCam(imgVideo) End Sub Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) webcam.Start() End Sub Private Sub bntStop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) webcam.Stop() End Sub Private Sub bntContinue_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) webcam.Continue() End Sub Private Sub bntCapture_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) imgCapture.Source = imgVideo.Source Dim frame As BitmapFrame ' = DirectCast(Me.imgCapture.Source, BitmapFrame) frame = BitmapFrame.Create(imgCapture.Source) Dim ms As New MemoryStream() Dim encoder As New BmpBitmapEncoder() encoder.Frames.Add(BitmapFrame.Create(frame)) encoder.Save(ms) 'AFFICHE DU MemoryStream bi = New BitmapImage() 'bi.BeginInit() 'ms.Position = 0 ' TRES TRES IMPORTANT CECI ...!!! 'bi.CacheOption = BitmapCacheOption.OnLoad 'Mise en cached memoire 'bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat 'bi.StreamSource = ms 'bi.EndInit() ' ' WHATEVER YOU WANT : storage dans un buffer pixels Dim buffer As Byte() = New Byte(ms.Length - 1) {} ms.Position = 0 ms.Read(buffer, 0, CInt(ms.Length) - 1) 'test buffer modifié mis dans image Dim buffer2 As Byte() = New Byte(ms.Length - 1) {} buffer2 = buffer Dim a As Long = 76854 Do While a < 254000 buffer2(a) = 255 a = a + 1 Loop MsgBox(buffer.Length) ms.Position = 0 ms.Write(buffer2, 0, CInt(ms.Length) - 1) bi.BeginInit() ms.Position = 0 ' TRES TRES IMPORTANT CECI ...!!! bi.CacheOption = BitmapCacheOption.OnLoad 'Mise en cached memoire bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat bi.StreamSource = ms bi.EndInit() ' Dim destRect1 As Rect(200, 10, 20, 16) ' Me.imgFromByte.Source = bi.SourceRect(0, 0, 100, 100) 'Libere Ressources ms.Close() encoder = Nothing webcam.Stop() End Sub Private Sub bntSaveImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Helper.SaveImageCapture(DirectCast(imgCapture.Source, BitmapSource)) End Sub ' Private Sub bntResolution_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) ' webcam.ResolutionSetting() ' End Sub ' Private Sub bntSetting_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) ' webcam.AdvanceSetting() ' End Sub End Classet celui correspondant à helper.vb :
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 Imports System Imports System.IO Imports System.Linq Imports System.Text Imports WebCam_Capture Imports System.Windows.Controls Imports System.Collections.Generic Imports System.Windows.Media.Imaging 'Design by Pongsakorn Poosankam Class WebCam Private webcam As WebCamCapture Private _FrameImage As System.Windows.Controls.Image Private FrameNumber As Integer = 2 Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Controls.Image) webcam = New WebCamCapture() webcam.FrameNumber = CULng((0)) webcam.TimeToCapture_milliseconds = FrameNumber AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured _FrameImage = ImageControl End Sub Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal e As WebcamEventArgs) _FrameImage.Source = Helper.LoadBitmap(DirectCast(e.WebCamImage, System.Drawing.Bitmap)) ' webcam.[Stop]() End Sub Public Sub Start() webcam.TimeToCapture_milliseconds = FrameNumber webcam.CaptureHeight = 1944 webcam.CaptureWidth = 2592 webcam.Start(0) End Sub Public Sub [Stop]() webcam.[Stop]() End Sub Public Sub [Continue]() ' change the capture time frame webcam.TimeToCapture_milliseconds = FrameNumber ' resume the video capture from the stop webcam.Start(Me.webcam.FrameNumber) End Sub 'Public Sub ResolutionSetting() ' webcam.Config() 'End Sub 'Public Sub AdvanceSetting() ' webcam.Config2() ' End Sub End Class
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 Imports System Imports System.IO Imports System.Linq Imports System.Text Imports System.Collections.Generic Imports System.Windows.Media.Imaging 'Design by Pongsakorn Poosankam Class Helper 'Block Memory Leak <System.Runtime.InteropServices.DllImport("gdi32.dll")> _ Public Shared Function DeleteObject(ByVal handle As IntPtr) As Boolean End Function Public Shared bs As BitmapSource Public Shared ip As IntPtr Public Shared Function LoadBitmap(ByVal source As System.Drawing.Bitmap) As BitmapSource 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 End Function Public Shared Sub SaveImageCapture(ByVal bitmap As BitmapSource) Dim encoder As New JpegBitmapEncoder() encoder.Frames.Add(BitmapFrame.Create(bitmap)) encoder.QualityLevel = 100 ' Configure save file dialog box Dim dlg As 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 Dim result As Nullable(Of Boolean) = dlg.ShowDialog() ' Process save file dialog box results If result = True Then ' Save Image Dim filename As String = dlg.FileName Dim fstream As New FileStream(filename, FileMode.Create) encoder.Save(fstream) fstream.Close() End If End Sub End Class
Je suppose que le souci est lié à la ligne.
Code : Sélectionner tout - Visualiser dans une fenêtre à part bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())
Merci d'avance pour votre aide.
Partager