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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
'pour FileStream
Imports System.IO
' pour XamlWriter
Imports System.Windows.Markup
' pour ObservableCollection
Imports System.Collections.ObjectModel
Partial Public Class Window3
Private lstChilds As ObservableCollection(Of FrameworkElement) = New ObservableCollection(Of FrameworkElement)
Private ndxElemToSave As Integer = -1
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
For Each child As FrameworkElement In Me.myInkCanvas.Children
If child.GetType IsNot GetType(Image) Then
lstChilds.Add(child)
End If
Next
Me.myLVChildren.ItemsSource = lstChilds
Me.myCBOChildren.ItemsSource = lstChilds
End Sub
'dessine un textbox et un bouton
Private Sub DrawText_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim NewTextBox = New TextBox
NewTextBox.Name = "myTextBlock"
NewTextBox.Text = "TextBlock content"
NewTextBox.Background = Brushes.Linen
NewTextBox.BorderBrush = Nothing
NewTextBox.TextWrapping = TextWrapping.Wrap
NewTextBox.AcceptsReturn = True
InkCanvas.SetTop(NewTextBox, 100)
InkCanvas.SetLeft(NewTextBox, 100)
Me.myInkCanvas.Children.Add(NewTextBox)
lstChilds.Add(NewTextBox)
'plus un gros bouton
Dim NewButton = New Button
NewButton.Name = "MyButton"
NewButton.Content = "MyButton"
NewButton.Background = Brushes.Chartreuse
NewButton.BorderBrush = Nothing
NewButton.HorizontalContentAlignment = Windows.HorizontalAlignment.Center
NewButton.Opacity = 0.6
NewButton.Height = 80
NewButton.Width = 100
InkCanvas.SetTop(NewButton, 150)
InkCanvas.SetLeft(NewButton, 100)
Me.myInkCanvas.Children.Add(NewButton)
lstChilds.Add(NewButton)
End Sub
'charge une image pour etre dessiner
Private Sub DrawImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'ajoute une image dans InkCanvas
Dim NewImage = New Image
Dim pathImage As String = String.Empty
pathImage = GetPathImage()
If Len(pathImage) <> 0 Then
NewImage.Name = "MyNewImage"
NewImage.Source = New BitmapImage(New Uri(pathImage))
NewImage.Width = Me.myInkCanvas.ActualWidth
NewImage.Height = Me.myInkCanvas.ActualHeight
InkCanvas.SetTop(NewImage, 100)
InkCanvas.SetLeft(NewImage, 150)
Me.myInkCanvas.Children.Add(NewImage)
lstChilds.Add(NewImage)
End If
End Sub
'Sub specialise pour chargement des images
Private Function GetPathImage() As String
'Specify the filePath for image
Dim fileNameImage As String = String.Empty
'Configure open file dialog box
Dim dlg As New Microsoft.Win32.OpenFileDialog()
dlg.FileName = "Images" ' Default file name
dlg.DefaultExt = ".png" ' Default file extension
dlg.Filter = "Fichiers Images(*.png,*.jpg)|*.png;*.jpg" ' Filter files by extension
' Show open file dialog box
Dim result As Boolean = dlg.ShowDialog()
' Process open file dialog box results
If result = True Then
' Open image
fileNameImage = dlg.FileName
End If
If Len(fileNameImage) <> 0 Then
Return fileNameImage
Else
MessageBox.Show("specifier un nom de fichier image .s.v.p..")
End If
Return Nothing
End Function
'sauve tout InkCanvas comme une image bitmap => methode 2
Private Sub SaveAllAsImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'Specify the filePath for saving your drawing
Dim fileName As String = String.Empty
'Configure save file dialog box
Dim dlg As New Microsoft.Win32.SaveFileDialog()
dlg.FileName = "DrawingImage" ' Default file name
dlg.DefaultExt = ".png" ' Default file extension
dlg.Filter = "DrawingImage(.png)|*.png" ' Filter files by extension
' Show save file dialog box
Dim result As Boolean = dlg.ShowDialog()
' Process open file dialog box results
If result = True Then
' Save document
fileName = dlg.FileName
End If
If Len(fileName) <> 0 Then
Dim myUri As Uri = New Uri(fileName)
ExportToPng(myUri, Me.myInkCanvas)
Else
MessageBox.Show("specifier un nom de fichier.s.v.p..")
End If
End Sub
Public Sub ExportToPng(ByVal pathUri As Uri, ByVal objInkCanvas As InkCanvas)
If pathUri.OriginalString = String.Empty Then Return
'Save current canvas transform
Dim transform As Transform = objInkCanvas.LayoutTransform
'reset current transform (in case it is scaled or rotated)
objInkCanvas.LayoutTransform = Nothing
'Get the size of canvas
Dim size As Size = New Size(objInkCanvas.ActualWidth, objInkCanvas.ActualHeight)
'Measure and arrange the surface
'VERY IMPORTANT
objInkCanvas.Measure(size)
objInkCanvas.Arrange(New Rect(size))
'Create a render bitmap and push the surface to it
Dim renderBitmap As RenderTargetBitmap = _
New RenderTargetBitmap( _
CType(size.Width, Integer), _
CType(size.Height, Integer), _
96D, _
96D, _
PixelFormats.Pbgra32)
renderBitmap.Render(objInkCanvas)
'Create a file stream for saving image
Using outStream As FileStream = New FileStream(pathUri.LocalPath, FileMode.Create)
'Use png encoder for our data
Dim encoder As PngBitmapEncoder = New PngBitmapEncoder()
'push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap))
'save the data to the stream
encoder.Save(outStream)
End Using
'Restore previously saved layout
objInkCanvas.LayoutTransform = transform
End Sub
'sauve les childrens sauf image dans un fichier xaml=> methode 3
Private Sub saveChildrens_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'La selection vide retour
If ndxElemToSave = -1 Then Return
'Specify the filePath for saving your drawing
Dim fileName As String = String.Empty
'Configure save file dialog box
Dim dlg As New Microsoft.Win32.SaveFileDialog()
dlg.FileName = "MyDrawing" ' Default file name
dlg.DefaultExt = ".xaml" ' Default file extension
dlg.Filter = "Drawing documents (.xaml)|*.xaml" ' Filter files by extension
' Show save file dialog box
Dim result As Boolean = dlg.ShowDialog()
' Process open file dialog box results
If result = True Then
' Save document
fileName = dlg.FileName
End If
If Len(fileName) <> 0 Then
Dim cnvTemp As Canvas = New Canvas
Dim childToSave = Me.myInkCanvas.Children.Item(ndxElemToSave)
For Each child As FrameworkElement In Me.myInkCanvas.Children
If child.GetType IsNot GetType(Image) Then
ExportXaml(New Uri(fileName), child)
End If
Next
Else
MessageBox.Show("specifier un nom de fichier.s.v.p..")
End If
End Sub
'Exporting Child to the XAML
'Ill give you the most stupid sample that can be found everywhere
Public Sub ExportXaml(ByVal pathUri As Uri, ByVal childToSave As UIElement)
If pathUri.OriginalString = String.Empty Then Return
If childToSave Is Nothing Then Return
'Save Child UIElement comme un string en memoire.
Dim stringUI As String = XamlWriter.Save(childToSave)
'Save Child dans un File Xaml.
File.WriteAllText(pathUri.LocalPath, stringUI)
End Sub
'Exporting canvas to the XAML
'Ill give you the most stupid sample that can be found everywhere
Public Sub ExportXamlPänel(ByVal pathUri As Uri, ByVal tempCanvas As Canvas)
If pathUri.OriginalString = String.Empty Then Return
If tempCanvas Is Nothing Then Return
'Save UIElement comme un string en memoire.
Dim stringUI As String = XamlWriter.Save(tempCanvas)
'Save tempCanvas dans un File Xaml.
File.WriteAllText(pathUri.LocalPath, stringUI)
End Sub
'charge un children depuis un fichier xaml => methode 3
' ou plusieurs si on a sauve plusieurs........
Private Sub loadChildrens_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'Specify the filePath for opening your drawing
Dim fileName As String = String.Empty
'Configure open file dialog box
Dim dlg As New Microsoft.Win32.OpenFileDialog()
dlg.FileName = "MyDrawing" ' Default file name
dlg.DefaultExt = ".xaml" ' Default file extension
dlg.Filter = "Drawing documents (.xaml)|*.xaml" ' Filter files by extension
' Show open file dialog box
Dim result As Boolean = dlg.ShowDialog()
' Process open file dialog box results
If result = True Then
' Open document
fileName = dlg.FileName
End If
If Len(fileName) <> 0 Then
Me.myInkCanvas.Children.Clear()
Dim child As UIElement
child = ImportXaml(fileName)
Me.myInkCanvas.Children.Add(child)
Me.lstChilds.Add(child)
Else
MessageBox.Show("specifier un nom de fichier.s.v.p..")
End If
End Sub
Public Function ImportXaml(ByVal pathFile As String) As UIElement
If pathFile = String.Empty Then Return Nothing
' Ouvre le file comme un string.
Dim myReader As StreamReader = New StreamReader(pathFile, True)
'lit le fichier comme un stringReader
Dim stringUIElement As StringReader = New StringReader(myReader.ReadToEnd)
Dim xmlReader As Xml.XmlReader = Xml.XmlReader.Create(stringUIElement)
'charge le file comme UIElement
Dim readerInkCanvas As UIElement = CType(Markup.XamlReader.Load(xmlReader), UIElement)
myReader.Close()
Return readerInkCanvas
End Function
Private Sub LVChildren_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
'tu peux reprendre integralement le code dans le combo ci-dessous
End Sub
Private Sub ComboChildren_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
ndxElemToSave = -1
If (myCBOChildren.SelectedItem IsNot Nothing) Then
Dim elem As FrameworkElement = CType(myCBOChildren.SelectedItem, FrameworkElement)
If Me.myInkCanvas.Children.Contains(elem) Then
ndxElemToSave = Me.myInkCanvas.Children.IndexOf(elem)
MessageBox.Show("msg :" & ndxElemToSave.ToString)
End If
End If
End Sub
End Class |
Partager