Bonjour
J'avais posée une question il y peu que j'ai résolue, mais la suite me pose un petit problème, je suis sûr que c'est un truc à la "bip" que j'ai oublié en plus.
J'ai une zone dans laquelle je fais un snap sur une caméra, après je clique sur un bouton qui me change le curseur uniquement dans la zone ou la caméra est arreté, suite à ça je clique sur l'écran et cela me garde la forme rectangle de mon curseur sur l'écran via un rectangle dessiné. Puis ensuite j'obtiens la position de ma souris
Je veux lorsque je clique sur un autre bouton, que cela me remette le curseur initial (celà est ok) mais surtout que je n'ai plus l'empreinte du rectangle. engros que cela ne fasse plus le DrawSelection que vous allez voir dans le code.
code xaml :
code behind :
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 <Canvas HorizontalAlignment="Left" Margin="0,30,0,44" Width="310" Background="#FF353535"> <Border CornerRadius="10" BorderBrush="Gray" Background="#FF404040" BorderThickness="1" Padding="0" Height="51" Width="292" Canvas.Left="8" Canvas.Top="17"> <ToolBar Background="#FF404040" Margin="19,-1,0,0" RenderTransformOrigin="0.5,0.5" Style="{DynamicResource ToolBarStyle1}" VerticalAlignment="Top" Height="42" HorizontalAlignment="Left" Width="262"> <Button Background="{x:Null}" Width="24" Height="24" x:Name="bntStart" Click="bntStart_Click" RenderTransformOrigin="0.5,0.5" Margin="0,0,0,7" VerticalAlignment="Bottom" > <Image Source="C:\Users\Anthony kamax\Downloads\Icones\case-vert-icone-8663-32.png" x:Name="Image1" /> </Button> <Button Background="{x:Null}" x:Name="bntCapture" Click="bntCapture_Click" SnapsToDevicePixels="True" > <Image Source="Resources/appbar.camera.png" Height="30" /> </Button> <Button Background="{x:Null}" Width="24" Height="24" SnapsToDevicePixels="True" Margin="3,6,4,6" x:Name="bntContinue" Click="bntContinue_Click"> <Image Source="C:\Users\Anthony kamax\Downloads\Icones\camera.png" /> </Button> <Button Background="{x:Null}" Width="24" Height="24" SnapsToDevicePixels="True" Margin="3,6,4,6" > <Image Source="C:\Users\Anthony kamax\Downloads\Icones\loupe-zoom-avant-icone-8627-32.png" /> </Button> <Button Background="{x:Null}" Width="24" Height="24" SnapsToDevicePixels="True" Margin="3,6,4,6" > <Image Source="C:\Users\Anthony kamax\Downloads\Icones\loupe-zoom-arriere-icone-9037-32.png" /> </Button> <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="bntSaveImage" Click="bntSaveImage_Click" RenderTransformOrigin="0.5,0.5" Height="30" Margin="0,3.5" > <Image Source="Resources/appbar.save.png" Height="30" /> </Button> <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="bntSelectImage" Click="bntSelectImage_Click" RenderTransformOrigin="0.5,0.5" Height="30" Margin="0,3.5" > <Image Source="Resources/appbar.edit.box.png" Width="30" /> </Button> </ToolBar> </Border> <Border CornerRadius="10" BorderBrush="Gray" Background="#FF404040" BorderThickness="1" Padding="0" Height="402" Width="294" Canvas.Top="96" Canvas.Left="10"> <Canvas Margin="0,0,0,-46" Name="container" > <visualizationToolkit:Title Content="Live Cam" Canvas.Left="120" FontStyle="Italic" FontWeight="Bold"/> <Image Height="240" x:Name="imgVideo" Stretch="Fill" HorizontalAlignment="Left" Width="320" Canvas.Left="-15" Canvas.Top="59" RenderTransformOrigin="0.5,0.5"> <Image.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform Angle="-90"/> <TranslateTransform/> </TransformGroup> </Image.RenderTransform> </Image> </Canvas> </Border> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Canvas.Top="500" Canvas.Left="30" Width="245"> <Image Stretch="Fill"/> <!--Button x:Name="bntResolution" Content="Video Format" Width="120" Click="bntResolution_Click" /> <Button x:Name="bntSetting" Content="Video Source" Width="120" Margin="0,10,0,0" Click="bntSetting_Click" /--> </StackPanel> <Border CornerRadius="10" BorderBrush="Gray" Background="#FF404040" BorderThickness="1" Padding="0" Height="216.8" Width="294.5" Canvas.Left="8" Canvas.Top="537"> <Canvas Margin="0,0,0,-46"> <visualizationToolkit:Title Content="Sample parameters" Canvas.Left="93" FontStyle="Italic" FontWeight="Bold"/> <TextBlock Width="160" Height="20.04" Canvas.Top="31.96" TextWrapping="Wrap"><Run Text="T"/><Run Text="hick of histological section"/><Run Text=" :"/></TextBlock> <TextBlock Width="93" Height="20.04" Canvas.Top="54.96" TextWrapping="Wrap"><Run Text="Sample Number"/><Run Text=" :"/></TextBlock> <TextBlock Width="83" Height="20.04" Canvas.Top="82.96" TextWrapping="Wrap"><Run Text="Observations "/><Run Text=":"/></TextBlock> <TextBox Width="125.5" Height="22" Canvas.Left="159" Canvas.Top="25.96" Text="TextBox" TextWrapping="Wrap"/> <TextBox Width="171.5" Height="23.04" Canvas.Left="113" Canvas.Top="51.96" Text="TextBox" TextWrapping="Wrap"/> <TextBox Width="191.5" Height="119.04" Canvas.Left="93" Canvas.Top="82.96" Text="TextBox" TextWrapping="Wrap"/> </Canvas> </Border> <Image Source="C:\Users\Anthony kamax\Downloads\Icones\Logo KAMAX.gif" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="8" Canvas.Top="772.8" Width="91" Height="31"/> <ProgressBar Width="182" Height="13" Canvas.Left="113.76" Canvas.Top="782"/> </Canvas>
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 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.Forms.Cursor Imports System.Windows Imports System.Windows.Controls Imports System.Runtime.InteropServices 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 Imports System.Windows.Forms Class Window1 Private writeableBitmap As WriteableBitmap Private bi As BitmapImage Private webcam As WebCam Private element As SelectionBackElement Public Sub New() ' Cet appel est requis par le concepteur. InitializeComponent() 'Dim element As New SelectionBackElement(container) 'container.Children.Add(element) ' Ajoutez une initialisation quelconque après l'appel InitializeComponent(). RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor) RenderOptions.SetEdgeMode(i, EdgeMode.Aliased) End Sub 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) bntSelectImage.Background = Nothing bntCapture.Background = System.Windows.Media.Brushes.BlueViolet container.Cursor = Nothing container.Children(element) = Nothing 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) ms.Dispose() ' Me.imgFromByte.Source = bi.SourceRect(0, 0, 100, 100) 'copie image dans WriteableBitmap writeableBitmap = New WriteableBitmap(frame) i.Source = writeableBitmap i.Stretch = Stretch.None i.HorizontalAlignment = HorizontalAlignment.Left i.VerticalAlignment = VerticalAlignment.Top i.Height = frame.Height i.Width = frame.Width ' MsgBox(i.Height) ' MsgBox(i.Width) ' MsgBox(writeableBitmap.DpiX) '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 Public Sub bntSelectImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) ' Mouse.OverrideCursor 'container.Cursor = New System.Windows.Input.Cursor("C:\Users\Anthony kamax\Documents\Visual Studio 2013\Projects\Placement3\Placement\cursor.cur") bntSelectImage.Background = System.Windows.Media.Brushes.BlueViolet bntCapture.Background = Nothing Dim element As New SelectionBackElement(container) ' If bntSelectImage.Background Is System.Windows.Media.Brushes.BlueViolet Then container.Children.Add(element) 'End If container.Cursor = New System.Windows.Input.Cursor("C:\Users\Anthony kamax\Documents\Visual Studio 2013\Projects\Placement3\Placement\cursor.cur") End Sub Private Sub bntSelectImage2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Mouse.OverrideCursor = Nothing ' New System.Windows.Input.Cursor("C:\Users\Anthony kamax\Documents\Visual Studio 2013\Projects\Placement3\Placement\cursor.cur") End Sub Private Function CreateGraphics() As Graphics Throw New NotImplementedException End Function End Class
code permettant le dessin du rectangle :
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 Imports System.Windows Imports System.Windows.Media Imports System.Windows.Input Public Class SelectionBackElement Inherits FrameworkElement Private visual As DrawingVisual Private isDrawing As [Boolean] Private startPoint As Point Private selectionBrush As Brush Private selectionPen As New Pen(Brushes.Blue, 1) Private container As FrameworkElement Public Sub New(container As FrameworkElement) Dim color As Color = SystemColors.HighlightColor color.A = 128 selectionBrush = New SolidColorBrush(color) selectionPen = New Pen(SystemColors.HighlightBrush, 1) visual = New DrawingVisual() Me.AddVisualChild(visual) Me.container = container AddHandler container.MouseDown, AddressOf MouseDownHandler AddHandler container.MouseMove, AddressOf MouseMoveHandler AddHandler container.MouseUp, AddressOf MouseUpHandler End Sub Protected Overrides ReadOnly Property VisualChildrenCount() As Int32 Get Return 1 End Get End Property Protected Overrides Function GetVisualChild(index As Int32) As Visual Return visual End Function Private Sub MouseDownHandler(sender As [Object], e As MouseButtonEventArgs) startPoint = e.GetPosition(Me) isDrawing = True DrawSelection(selectionBrush, selectionPen, startPoint) End Sub Private Sub MouseMoveHandler(sender As [Object], e As MouseEventArgs) If isDrawing AndAlso e.LeftButton = MouseButtonState.Pressed Then Dim endPoint As Point = e.GetPosition(Me) DrawSelection(selectionBrush, selectionPen, startPoint) End If End Sub Private Sub MouseUpHandler(sender As [Object], e As MouseButtonEventArgs) startPoint = e.GetPosition(Me) isDrawing = False End Sub Private Sub DrawSelection(fill As Brush, pen As Pen, startPoint As Point) Using dc As DrawingContext = visual.RenderOpen() dc.DrawGeometry(fill, pen, New RectangleGeometry(New Rect(startPoint, New Size(21, 30)))) End Using MsgBox(startPoint.X) MsgBox(startPoint.Y) End Sub End Class
Partager