Option Explicit On
Imports System.IO
Imports System.Windows.Printing
Imports System.Windows.Media.Imaging
Imports System.Collections.Generic
Imports System.Xml
Imports System.Text
Imports System.IO.IsolatedStorage
Partial Public Class MainPage
Inherits UserControl
Private lClouds As New System.Collections.Generic.List(Of cFigure)
Private iCountClouds As Integer = -1
Public Shared iActualCloud As Integer
Public Enum eFigure
leftCloud
rightCloud
leftOval
rightOval
leftBox
rightBox
Box
End Enum
Public Sub New()
InitializeComponent()
End Sub
Private Sub Canvas_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs) Handles MyBase.Drop, box1.Drop, box2.Drop, box3.Drop, box4.Drop, box5.Drop, box6.Drop
Dim dataObject As IDataObject = e.Data 'For know what is the canvas of the image was dropped
Dim dropPoint As Point = e.GetPosition(sender) 'For know the position of the image was dropped
If dataObject.GetDataPresent(DataFormats.FileDrop) Then 'If a file was dropped
Dim files As FileInfo() = DirectCast(dataObject.GetData(DataFormats.FileDrop), FileInfo()) 'Get the info of the file dropped
If files.Length > 0 Then
sender.Children.Clear()
'A filter to only accept images files (.jpg, .png or gifs)
If files(0).Extension.ToLower = ".jpg" Or files(0).Extension.ToLower = ".png" Or files(0).Extension.ToLower = ".gif" Then
Dim bitmapImage As New System.Windows.Media.Imaging.BitmapImage() 'For copy the image dropped
bitmapImage.SetSource(files(0).OpenRead()) 'Copy the image to a bitmap
Dim newImage As New Image() 'Create a new image for copy the image
'For put the image on the coordinates 0,0
dropPoint.Y = 0
dropPoint.X = 0
'Resize the image to the size of the canvas
newImage.Width = sender.Width
newImage.Height = sender.Height
newImage.Source = bitmapImage
newImage.SetValue(Canvas.TopProperty, dropPoint.Y)
newImage.SetValue(Canvas.LeftProperty, dropPoint.X)
newImage.Stretch = Stretch.Fill
'Add the image at the canvas
sender.Children.Add(newImage)
End If
End If
End If
End Sub
Private Sub Canvas2_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles canvasComic.MouseLeftButtonDown
Dim dropPoint As Point = e.GetPosition(canvasComic)
If lClouds.Count > 0 Then
lClouds(iActualCloud).bAux = True
lClouds(iActualCloud).SetPosition(dropPoint)
End If
End Sub
Private Sub Canvas2_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles canvasComic.MouseMove
If lClouds.Count > 0 Then
If lClouds(iActualCloud).bAux Then
Dim dropPoint As Point = e.GetPosition(canvasComic)
If dropPoint.Y + lClouds(iActualCloud).newImage.Height < canvasComic.Height And dropPoint.X + lClouds(iActualCloud).newImage.Width < canvasComic.Width Then
lClouds(iActualCloud).SetPosition(dropPoint)
End If
End If
End If
End Sub
Private Sub Canvas2_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles canvasComic.MouseLeftButtonUp
If lClouds.Count > 0 Then
lClouds(iActualCloud).bAux = False
End If
End Sub
Private Function CanvasBeginPrint(ByVal s As Object, ByVal args As Object) As Object
Return Nothing
End Function
Private Function CanvasPrintPage(ByVal s As Object, ByVal args As Object, ByVal dynamicPanel As Canvas) As Object
args.PageVisual = dynamicPanel
Return Nothing
End Function
Private Function CanvasEndPrint(ByVal s As Object, ByVal args As Object, ByVal dynamicPanel As Canvas) As Object
dynamicPanel = Nothing ' cleanup
Return Nothing
End Function
Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cbBallons.SelectionChanged
iActualCloud = Me.cbBallons.SelectedIndex
End Sub
''' <summary>
''' Set the ballon to draw
''' </summary>
''' <param name="eFigure">The type of figure</param>
''' <remarks></remarks>
Private Sub SaveFigure(ByVal eFigure As [Enum])
Dim dropPoint As Point 'The location for the cloud
Dim iText As Integer 'The number of the ballon
Dim oCloud As New cFigure() 'The object to save a cloud
dropPoint.X = 0
dropPoint.Y = 0
iCountClouds += 1 'Add a new cloud to the counter
iText = iCountClouds + 1 'Add the text for the cloud
iActualCloud = iCountClouds 'Set the actual cloud
Me.cbBallons.Items.Add(eFigure.ToString + iText.ToString()) 'Add the ballon to the combobox
oCloud.SetImage(Me.tbText.Text, dropPoint, eFigure, iCountClouds)
oCloud.iChildren = Me.canvasComic.Children.Count + 1
lClouds.Add(oCloud)
canvasComic.Children.Add(lClouds(oCloud.iNumber).newImage)
canvasComic.Children.Add(lClouds(oCloud.iNumber).tb)
Me.tbText.Text = ""
End Sub
Private Sub SaveFigure(ByVal type As eFigure, ByVal pPoint As Point)
Dim dropPoint As Point
Dim iText As Integer
Dim oNube As New cFigure()
dropPoint.X = pPoint.X
dropPoint.Y = pPoint.Y
iCountClouds += 1
iText = iCountClouds + 1
Me.cbBallons.Items.Add(type.ToString + iText.ToString())
oNube.SetImage(Me.tbText.Text, dropPoint, type, iCountClouds)
oNube.iChildren = Me.canvasComic.Children.Count + 1
lClouds.Add(oNube)
canvasComic.Children.Add(lClouds(oNube.iNumber).newImage)
canvasComic.Children.Add(lClouds(oNube.iNumber).tb)
Me.tbText.Text = ""
End Sub
''' <summary>
'''Saves to isolated storage
''' </summary>
''' <param name="buffer">The buffer</param>
''' <param name="fileName"></param>
Private Shared Sub _SaveToDisk(ByVal buffer As Byte(), ByVal fileName As String)
Using iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Using stream As New IsolatedStorageFileStream(fileName, FileMode.CreateNew, iso)
stream.Write(buffer, 0, buffer.Length)
End Using
End Using
End Sub
''' <summary>
''' Gets the buffer to save to disk from the writable bitmap
''' </summary>
''' <param name="bitmap">The bitmap image</param>
''' <returns>The buffer of bytes</returns>
Private Shared Function _GetSaveBuffer(ByVal bitmap As WriteableBitmap) As Byte()
Dim matrixSize As Long = bitmap.PixelWidth * bitmap.PixelHeight
Dim byteSize As Long = matrixSize * 4 + 4
Dim retVal As Byte() = New Byte(byteSize - 1) {}
Dim bufferPos As Long = 0
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.PixelWidth / 256) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.PixelWidth Mod 256) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.PixelHeight / 256) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.PixelHeight Mod 256) And &HFF)
For matrixPos As Integer = 0 To matrixSize - 1
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.Pixels(matrixPos) >> 24) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.Pixels(matrixPos) >> 16) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.Pixels(matrixPos) >> 8) And &HFF)
retVal(System.Math.Max(System.Threading.Interlocked.Increment(bufferPos), bufferPos - 1)) = CByte((bitmap.Pixels(matrixPos)) And &HFF)
Next
Return retVal
End Function
Private Sub bLeftCloud_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bLeftCloud.Click
Me.SaveFigure(eFigure.leftCloud)
End Sub
Private Sub bRightCloud_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bRightCloud.Click
Me.SaveFigure(eFigure.rightCloud)
End Sub
Private Sub bLeftOval_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bLeftOval.Click
Me.SaveFigure(eFigure.leftOval)
End Sub
Private Sub bRightOval_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bRightOval.Click
Me.SaveFigure(eFigure.rightOval)
End Sub
Private Sub bLeftBox_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bLeftBox.Click
Me.SaveFigure(eFigure.leftBox)
End Sub
Private Sub bRightBox_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bRightBox.Click
Me.SaveFigure(eFigure.rightBox)
End Sub
Private Sub bBox_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bBox.Click
Me.SaveFigure(eFigure.Box)
End Sub
Private Sub bDelete_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bDelete.Click
Me.canvasComic.Children.RemoveAt(lClouds(iActualCloud).iChildren)
Me.canvasComic.Children.RemoveAt(lClouds(iActualCloud).iChildren - 1)
Me.lClouds.RemoveAt(iActualCloud)
Me.cbBallons.Items.RemoveAt(iActualCloud)
If iActualCloud > 0 Then
iActualCloud -= 1
End If
If iCountClouds >= 0 Then
iCountClouds -= 1
End If
End Sub
Private Sub bNuevo_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles bNew.Click
'There are 12 children for the main Canvas, then delete of the childrenx except the first 12
For i = canvasComic.Children.Count - 1 To 12 Step -1
Me.canvasComic.Children.RemoveAt(i)
Next
If Me.box1.Children.Count > 0 Then
box1.Children.Clear()
End If
If Me.box2.Children.Count > 0 Then
box2.Children.Clear()
End If
If Me.box3.Children.Count > 0 Then
box3.Children.Clear()
End If
If Me.box4.Children.Count > 0 Then
box4.Children.Clear()
End If
If Me.box5.Children.Count > 0 Then
box5.Children.Clear()
End If
If Me.box6.Children.Count > 0 Then
box6.Children.Clear()
End If
Me.cbBallons.Items.Clear()
iActualCloud = 0
iCountClouds = -1
Me.lClouds.Clear()
End Sub
Private Sub bAbrir_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles bOpen.Click
Dim output As StringBuilder = New StringBuilder()
Dim sfile As String
Dim iPos As Integer = 0
Dim oFigure As New cFigure()
Dim dropPoint As Point
Using isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
' Open the file again for reading.
If isoStore.FileExists("WebComic.xml") Then
Using readerfile As New StreamReader(isoStore.OpenFile("WebComic.xml", FileMode.Open))
sfile = readerfile.ReadToEnd()
End Using
Using reader As XmlReader = XmlReader.Create(New StringReader(sfile))
Dim ws As XmlWriterSettings = New XmlWriterSettings()
ws.Indent = True
' Parse the file and display each of the nodes.
Me.lClouds.Clear()
iActualCloud = 0
iCountClouds = -1
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
Select Case reader.Name
Case "ballon"
iPos += 1
Case "X"
If reader.Read() Then
oFigure.iPosition.X = CInt(reader.Value)
dropPoint.X = oFigure.iPosition.X
End If
Case "Y"
If reader.Read() Then
oFigure.iPosition.Y = CInt(reader.Value)
dropPoint.Y = oFigure.iPosition.Y
End If
Case "Type"
If reader.Read() Then
oFigure.iPosition = dropPoint
oFigure.type = DirectCast([Enum].Parse(GetType(eFigure), reader.Value.ToString, True), eFigure)
End If
Case "Text"
If reader.Read() Then
oFigure.tb.Text = reader.Value
Me.tbText.Text = oFigure.tb.Text
Me.SaveFigure(oFigure.type, dropPoint)
oFigure.SetPosition(dropPoint)
Me.tbText.Text = ""
End If
End Select
Case XmlNodeType.Text
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.ProcessingInstruction
Case XmlNodeType.Comment
Case XmlNodeType.EndElement
End Select
End While
End Using
End If
End Using
TextBlock3.Text = output.ToString()
End Sub
Private Sub bImprimir_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles bPrint.Click
' create an instance of PrintDocument
Dim document As New PrintDocument()
' Add the handlers for print canvasComic is the canvas that contains the box of the page
AddHandler document.BeginPrint, Function(s, args) CanvasBeginPrint(s, args)
AddHandler document.PrintPage, Function(s, args) CanvasPrintPage(s, args, canvasComic)
AddHandler document.EndPrint, Function(s, args) CanvasEndPrint(s, args, canvasComic)
' call the Print() with a proper name which will be visible in the Print Queue
document.Print("Printing Comic")
End Sub
Private Sub bGuardar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles bSave.Click
Dim iImagen As New Image()
'If Me.box1.Children.Count > 0 Then
' Dim bitmap As New WriteableBitmap(box1, New TransformGroup())
' _SaveToDisk(_GetSaveBuffer(bitmap), "c:\test.jpg")
'End If
Using isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
' Create new file
Using isoStream As IsolatedStorageFileStream = _
New IsolatedStorageFileStream("WebComic.xml", _
FileMode.Create, isoStore)
' Write to the Isolated Storage for the user.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
' Create an XmlWriter.
Using writer As XmlWriter = XmlWriter.Create(isoStream, settings)
writer.WriteComment("Web Comic")
writer.WriteStartElement("figures")
For i = 0 To lClouds.Count - 1
' Write an element (this one is the root).
writer.WriteStartElement("ballon")
Dim oGlogo As New cFigure()
' Write an element (this one is the root).
writer.WriteElementString("X", lClouds(i).iPosition.X.ToString())
writer.WriteElementString("Y", lClouds(i).iPosition.Y.ToString())
writer.WriteElementString("Type", lClouds(i).type.ToString())
writer.WriteElementString("Text", lClouds(i).tb.Text)
writer.WriteEndElement()
Next
' Write the close tag for the root element.
writer.WriteEndElement()
writer.WriteEndDocument()
' Write the XML to the file.
writer.Flush()
End Using
End Using
End Using
End Sub
End Class