PDA

Click to See Complete Forum and Search --> : Migrating off of System.Drawing.Image not working!


formlesstree4
Nov 4th, 2009, 08:47 PM
If anyone here remembers on the WinForms section, I had some major help in developing a format for not only storing images, but actually getting all the data and physically storing them properly with all the included content.

I've decided to move to WPF to see what all the fuss was about. I do like WPF, and the neat 'whiz-bang' features that it has (which are all pretty pointless to me at this point except for the animation :D).

What I'm really asking here is a substitute to make this work because it is driving me nuts!!!!

Next post will contain the format reader (it's to long to fit on one post)

formlesstree4
Nov 4th, 2009, 08:47 PM
Namespace IndividualFile
Public Class Saver
Private m_FileName As String = ""
Public PKMNumber As Integer = -1
Public PKMName As String = ""
Public PKMType As String = ""
Public PKMDescription As String = ""
Public PKMImage As Image
Public PKMHeight As String
Public PKMWidth As String
Public Sub New(ByVal filename As String)
m_FileName = filename
End Sub
Public Sub Save()
If IsInvalid() Then Throw New ArgumentException()
Dim fs As New FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim br As New BinaryWriter(fs)
br.Write(Me.PKMNumber & "-" & Me.PKMName)
br.Write(Me.PKMType)
br.Write("[description]")
br.Write(String.Format("{0}:{1}", PKMHeight, PKMWidth))
br.Write(Me.PKMDescription)
br.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(Me.PKMImage).Length.ToString))
For Each b As Byte In imageToByteArray(Me.PKMImage)
br.Write(b)
Next
br.Close()
End Sub
Private Function IsInvalid() As Boolean
Select Case True
Case Me.PKMNumber <= 0, Me.PKMName = "", Me.PKMType = "", m_FileName = "", Me.PKMImage Is Nothing
Return True
End Select
Return False
End Function
Private Function imageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, imageIn.RawFormat)
Dim B() As Byte = ms.GetBuffer()
Return ms.GetBuffer
End Function
End Class
Public Class Opener
Private m_FileName As String = ""
Public PKMNumber As Integer = -1
Public PKMName As String = ""
Public PKMType As String = ""
Public PKMDescription As String = ""
Public PKMImage As Image
Public PKMNHeight As String
Public PKMNWidth As String
Public Sub New(ByVal filename As String)
m_FileName = filename
End Sub
Public Function Open() As Boolean
If IsInvalid() Then Throw New ArgumentException()
Dim FS As New FileStream(m_FileName, FileMode.Open)
If FS.Length = 0 Then
MessageBox.Show("This file is empty.")
Return False
End If
Dim BR As New BinaryReader(FS)
'First Line = Pokemon Number - Pokemon Name
PKMName = BR.ReadString()
'The Pokemon name contains two things: Number-Name
Dim CHolder() As String = PKMName.Split("-")
PKMNumber = CInt(CHolder(0))
PKMName = CHolder(1)
'Second Line = Pokemon Type
PKMType = BR.ReadString()
'Third Line = Junk Data
BR.ReadString()
'Fourth Line = Height:Weight
Dim tempData() As String = BR.ReadString.Split(":")
PKMNHeight = tempData(0)
PKMNWidth = tempData(1)

'Fifth Line = Description, but could be multiline
Dim CurrLine As String = BR.ReadString()
Do Until CurrLine.Contains("[image]")
PKMDescription &= CurrLine
Try
CurrLine = BR.ReadString
Catch ex As Exception
Exit Do 'We're outside the loop.....so rawr
End Try
Loop
'So now the current line is [image], split it to get the number of bytes to read.
Dim BytesRead() As String = CurrLine.Split(":")
Dim BytesToRead As Integer = CInt(BytesRead(1))
'Now we have the position...
'Now read the rest of the file!
Dim imgbytes() As Byte
imgbytes = BR.ReadBytes(BytesToRead)
Me.PKMImage = byteArrayToImage(imgbytes)
BR.Close()
Return True
End Function
Private Function IsInvalid() As Boolean
Return Not (m_FileName <> "" AndAlso IO.File.Exists(m_FileName))
End Function
Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Return Image.FromStream(ms, True, True)
End Function
End Class
End Namespace

Part 1/2

formlesstree4
Nov 4th, 2009, 08:49 PM
Namespace RegionReader
Public Class PackAndRepack
#Region " Clean up "
Public Sub Cleanup()
PokemonDescription.Clear()
PokemonHeight.Clear()
PokemonImage.Clear()
PokemonName.Clear()
PokemonNumber.Clear()
PokemonType.Clear()
PokemonWeight.Clear()
End Sub
#End Region
#Region " Packing Files "
Public Function ReadRegion(ByVal pdxFile As String, ByVal exportLists As Boolean) As Boolean
'The Region Reader will just keep everything in memory, so let's hope we can do this!
'First clear the lists
PokemonName.Clear()
PokemonNumber.Clear()
PokemonHeight.Clear()
PokemonWeight.Clear()
PokemonDescription.Clear()
PokemonType.Clear()

Try
Dim CurrentLine As String = String.Empty
Dim ReadStream As New FileStream(pdxFile, FileMode.Open)
If ReadStream.Length = 0 Then Return False
Dim BR As New BinaryReader(ReadStream)
CurrentLine = BR.ReadString()
Try
Do Until CurrentLine = "[END OF LINE]"
Do Until CurrentLine = "[END OF POKEMON]"
'We are at the start of a section.
'First Line = Pokemon Number - Pokemon Name
'Second Line = Pokemon Type
'Third Line = Junk Data
'Fourth Line = Height:Weight
'Fifth Line = Description, but could be multiline
'Sixth Line = [image]:imagesize
'Seventh Line till [END OF POKEMON] = Image
'Line 1
PokemonNumber.Add(CurrentLine.Split("-")(0))
PokemonName.Add(CurrentLine.Split("-")(1))
CurrentLine = BR.ReadString()
'Line 2
PokemonType.Add(CurrentLine)
CurrentLine = BR.ReadString()
'Line 3, extra crap
CurrentLine = BR.ReadString()
'Line 4
PokemonHeight.Add(CurrentLine.Split(":")(0))
PokemonWeight.Add(CurrentLine.Split(":")(1))
CurrentLine = BR.ReadString()
'Line 5
Dim Desc As String = String.Empty
Do Until CurrentLine.Contains("[image]")
Desc &= CurrentLine
CurrentLine = BR.ReadString()
Loop
PokemonDescription.Add(Desc)
'Line 6, this is the image line.
Dim ByteCount As Integer = CInt(CurrentLine.Split(":")(1))
Dim b() As Byte = BR.ReadBytes(ByteCount)
PokemonImage.Add(byteArrayToImage(b))
Do Until CurrentLine.Contains("[END OF POKEMON]")
CurrentLine = BR.ReadString()
Loop
Loop
CurrentLine = BR.ReadString()
Loop
BR.Close()
ReadStream.Close()
Catch ex As Exception
GC.Collect()
MessageBox.Show(ex.ToString)
End Try
Catch ex As Exception
GC.Collect()
MessageBox.Show(ex.ToString)
End Try
GC.Collect()
If exportLists = True Then Call SaveLists()
Return True
End Function
Public Sub SaveLists()
'This is really easy actually :D
Dim Count As Integer = 0
Dim Amount As Integer = PokemonName.Count
If Amount = 0 Then
MessageBox.Show("No files were unpacked, aborting!", "No files to rewrite.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'Now we have files to write
Do While Count < Amount
Using FS As New FileStream(String.Format("{0}.{1}", PokemonName(Count), "pidf"), FileMode.Create)
Using BW As New BinaryWriter(FS)
BW.Write(PokemonNumber(Count) & "-" & PokemonName(Count))
BW.Write(PokemonType(Count))
BW.Write("[description]")
BW.Write(String.Format("{0}:{1}", PokemonHeight(Count), PokemonWeight(Count)))
BW.Write(PokemonDescription(Count))
BW.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(PokemonImage(Count)).Length.ToString))
For Each b As Byte In imageToByteArray(PokemonImage(Count))
BW.Write(b)
Next
End Using
End Using
Count += 1
Loop
End Sub
Public Sub PackFiles(ByVal pdxFileName As String, ByVal sourceDirectory As String)
'We are going to read the files and write them at the same time.
Try
pdxFileName = If(pdxFileName.EndsWith(".pdfx") = True, pdxFileName, String.Format("{0}.{1}", pdxFileName, "pdfx"))
Dim WriteStream As New FileStream(sourceDirectory & "\" & pdxFileName, FileMode.Create)
Dim WriteBS As New BinaryWriter(WriteStream)
For Each f As String In Directory.GetFiles(sourceDirectory, "*.pidf")
Using ReadStream As New FileStream(f, FileMode.Open)
Using ReadBR As New BinaryReader(ReadStream)
WriteBS.Write(ReadBR.ReadBytes(ReadStream.Length))
End Using
'WriteBS.Write(0)
WriteBS.Write("[END OF POKEMON]")
End Using
Next
WriteBS.Write("[END OF LINE]")
WriteBS.Close()
WriteStream.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region " Byte Image Manipulation "
Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Return Image.FromStream(ms, True, True)
End Function
Private Function imageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, imageIn.RawFormat)
Dim B() As Byte = ms.GetBuffer()
Return ms.GetBuffer
End Function
#End Region
End Class
End Namespace

Rest of the code...and a little extra

Public Module PublicVariables
Public PokemonName As New List(Of String)
Public PokemonNumber As New List(Of Integer)
Public PokemonHeight As New List(Of String)
Public PokemonWeight As New List(Of String)
Public PokemonDescription As New List(Of String)
Public PokemonType As New List(Of String)
Public PokemonImage As New List(Of Image)
End Module

The referenced lists used in the region reader.
I'm just basically asking is there a way to make this code fully compatible with the Image control on WPF without major changes, because if not, I'm just going to keep this project on WinForms.