vb.net Code:
  1. Namespace IndividualFile
  2.         Public Class Saver
  3.             Private m_FileName As String = ""
  4.             Public PKMNumber As Integer = -1
  5.             Public PKMName As String = ""
  6.             Public PKMType As String = ""
  7.             Public PKMDescription As String = ""
  8.             Public PKMImage As Image
  9.             Public PKMHeight As String
  10.             Public PKMWidth As String
  11.             Public Sub New(ByVal filename As String)
  12.                 m_FileName = filename
  13.             End Sub
  14.             Public Sub Save()
  15.                 If IsInvalid() Then Throw New ArgumentException()
  16.                 Dim fs As New FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write)
  17.                 Dim br As New BinaryWriter(fs)
  18.                 br.Write(Me.PKMNumber & "-" & Me.PKMName)
  19.                 br.Write(Me.PKMType)
  20.                 br.Write("[description]")
  21.                 br.Write(String.Format("{0}:{1}", PKMHeight, PKMWidth))
  22.                 br.Write(Me.PKMDescription)
  23.                 br.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(Me.PKMImage).Length.ToString))
  24.                 For Each b As Byte In imageToByteArray(Me.PKMImage)
  25.                     br.Write(b)
  26.                 Next
  27.                 br.Close()
  28.             End Sub
  29.             Private Function IsInvalid() As Boolean
  30.                 Select Case True
  31.                     Case Me.PKMNumber <= 0, Me.PKMName = "", Me.PKMType = "", m_FileName = "", Me.PKMImage Is Nothing
  32.                         Return True
  33.                 End Select
  34.                 Return False
  35.             End Function
  36.             Private Function imageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
  37.                 Dim ms As New MemoryStream()
  38.                 imageIn.Save(ms, imageIn.RawFormat)
  39.                 Dim B() As Byte = ms.GetBuffer()
  40.                 Return ms.GetBuffer
  41.             End Function
  42.         End Class
  43.         Public Class Opener
  44.             Private m_FileName As String = ""
  45.             Public PKMNumber As Integer = -1
  46.             Public PKMName As String = ""
  47.             Public PKMType As String = ""
  48.             Public PKMDescription As String = ""
  49.             Public PKMImage As Image
  50.             Public PKMNHeight As String
  51.             Public PKMNWidth As String
  52.             Public Sub New(ByVal filename As String)
  53.                 m_FileName = filename
  54.             End Sub
  55.             Public Function Open() As Boolean
  56.                 If IsInvalid() Then Throw New ArgumentException()
  57.                 Dim FS As New FileStream(m_FileName, FileMode.Open)
  58.                 If FS.Length = 0 Then
  59.                     MessageBox.Show("This file is empty.")
  60.                     Return False
  61.                 End If
  62.                 Dim BR As New BinaryReader(FS)
  63.                 'First Line = Pokemon Number - Pokemon Name
  64.                 PKMName = BR.ReadString()
  65.                 'The Pokemon name contains two things: Number-Name
  66.                 Dim CHolder() As String = PKMName.Split("-")
  67.                 PKMNumber = CInt(CHolder(0))
  68.                 PKMName = CHolder(1)
  69.                 'Second Line = Pokemon Type
  70.                 PKMType = BR.ReadString()
  71.                 'Third Line = Junk Data
  72.                 BR.ReadString()
  73.                 'Fourth Line = Height:Weight
  74.                 Dim tempData() As String = BR.ReadString.Split(":")
  75.                 PKMNHeight = tempData(0)
  76.                 PKMNWidth = tempData(1)
  77.  
  78.                 'Fifth Line = Description, but could be multiline
  79.                 Dim CurrLine As String = BR.ReadString()
  80.                 Do Until CurrLine.Contains("[image]")
  81.                     PKMDescription &= CurrLine
  82.                     Try
  83.                         CurrLine = BR.ReadString
  84.                     Catch ex As Exception
  85.                         Exit Do 'We're outside the loop.....so rawr
  86.                     End Try
  87.                 Loop
  88.                 'So now the current line is [image], split it to get the number of bytes to read.
  89.                 Dim BytesRead() As String = CurrLine.Split(":")
  90.                 Dim BytesToRead As Integer = CInt(BytesRead(1))
  91.                 'Now we have the position...
  92.                 'Now read the rest of the file!
  93.                 Dim imgbytes() As Byte
  94.                 imgbytes = BR.ReadBytes(BytesToRead)
  95.                 Me.PKMImage = byteArrayToImage(imgbytes)
  96.                 BR.Close()
  97.                 Return True
  98.             End Function
  99.             Private Function IsInvalid() As Boolean
  100.                 Return Not (m_FileName <> "" AndAlso IO.File.Exists(m_FileName))
  101.             End Function
  102.             Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
  103.                 Dim ms As New MemoryStream(byteArrayIn)
  104.                 Return Image.FromStream(ms, True, True)
  105.             End Function
  106.         End Class
  107.     End Namespace

Part 1/2