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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| 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.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
Class Window1
Private writeableBitmap As WriteableBitmap
Private bi As BitmapImage
Private webcam As WebCam
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' 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)
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)
'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
Private Sub i_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
DrawPixel(e)
End Sub
Private Sub i_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
ErasePixel(e)
End Sub
Private Sub i_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
If e.LeftButton = MouseButtonState.Pressed Then
DrawPixel(e)
ElseIf e.RightButton = MouseButtonState.Pressed Then
ErasePixel(e)
End If
End Sub
Private Sub DrawPixel(ByVal e As MouseEventArgs)
Dim column As Integer = CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96)
Dim row As Integer = CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96)
'-----C'EST => DU BITMAP.LOCKBITS-----
'Reserve the back buffer for updates.
writeableBitmap.Lock()
'Get a pointer to the back buffer.
Dim pBackBuffer As Integer = writeableBitmap.BackBuffer.ToInt32
'Find the address of the pixel to draw.
pBackBuffer += row * writeableBitmap.BackBufferStride
pBackBuffer += column * 4
'Compute the pixel's color.
Dim color_data As Integer = 255 << 16 ' R
color_data = color_data Or 255 << 8 ' G
color_data = color_data Or 0 << 0 ' B
Marshal.WriteInt32(pBackBuffer, color_data)
'Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
'-----C'EST => DU BITMAP.UNLOCKBITS-----
'Release the back buffer and make it available for display.
writeableBitmap.Unlock()
End Sub
Private Sub ErasePixel(ByVal e As MouseEventArgs)
Dim ColorData() As Byte = {
255, 255, 255, 255
} ' B G R
'-----EQUIVALENT = > SETPIXEL WINFORM--------
Dim rect As Int32Rect = New Int32Rect(
CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96),
CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96),
1,
1)
writeableBitmap.WritePixels(rect, ColorData, 4, 0)
End Sub
Private Sub Window_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
Dim m As Matrix = i.RenderTransform.Value
If e.Delta > 0 Then
m.ScaleAt(
1.5,
1.5,
e.GetPosition(Me).X,
e.GetPosition(Me).Y)
Else
m.ScaleAt(
1.0 / 1.5,
1.0 / 1.5,
e.GetPosition(Me).X,
e.GetPosition(Me).Y)
End If
i.RenderTransform = New MatrixTransform(m)
End Sub |
Partager