Results 1 to 3 of 3

Thread: Migrating off of System.Drawing.Image not working!

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Migrating off of System.Drawing.Image not working!

    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 ).

    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)

  2. #2

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Migrating off of System.Drawing.Image not working!

    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

  3. #3

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Migrating off of System.Drawing.Image not working!

    vb.net Code:
    1. Namespace RegionReader
    2.         Public Class PackAndRepack
    3. #Region " Clean up "
    4.             Public Sub Cleanup()
    5.                 PokemonDescription.Clear()
    6.                 PokemonHeight.Clear()
    7.                 PokemonImage.Clear()
    8.                 PokemonName.Clear()
    9.                 PokemonNumber.Clear()
    10.                 PokemonType.Clear()
    11.                 PokemonWeight.Clear()
    12.             End Sub
    13. #End Region
    14. #Region " Packing Files "
    15.             Public Function ReadRegion(ByVal pdxFile As String, ByVal exportLists As Boolean) As Boolean
    16.                 'The Region Reader will just keep everything in memory, so let's hope we can do this!
    17.                 'First clear the lists
    18.                 PokemonName.Clear()
    19.                 PokemonNumber.Clear()
    20.                 PokemonHeight.Clear()
    21.                 PokemonWeight.Clear()
    22.                 PokemonDescription.Clear()
    23.                 PokemonType.Clear()
    24.  
    25.                 Try
    26.                     Dim CurrentLine As String = String.Empty
    27.                     Dim ReadStream As New FileStream(pdxFile, FileMode.Open)
    28.                     If ReadStream.Length = 0 Then Return False
    29.                     Dim BR As New BinaryReader(ReadStream)
    30.                     CurrentLine = BR.ReadString()
    31.                     Try
    32.                         Do Until CurrentLine = "[END OF LINE]"
    33.                             Do Until CurrentLine = "[END OF POKEMON]"
    34.                                 'We are at the start of a section.
    35.                                 'First Line = Pokemon Number - Pokemon Name
    36.                                 'Second Line = Pokemon Type
    37.                                 'Third Line = Junk Data
    38.                                 'Fourth Line = Height:Weight
    39.                                 'Fifth Line = Description, but could be multiline
    40.                                 'Sixth Line = [image]:imagesize
    41.                                 'Seventh Line till [END OF POKEMON] = Image
    42.                                 'Line 1
    43.                                 PokemonNumber.Add(CurrentLine.Split("-")(0))
    44.                                 PokemonName.Add(CurrentLine.Split("-")(1))
    45.                                 CurrentLine = BR.ReadString()
    46.                                 'Line 2
    47.                                 PokemonType.Add(CurrentLine)
    48.                                 CurrentLine = BR.ReadString()
    49.                                 'Line 3, extra crap
    50.                                 CurrentLine = BR.ReadString()
    51.                                 'Line 4
    52.                                 PokemonHeight.Add(CurrentLine.Split(":")(0))
    53.                                 PokemonWeight.Add(CurrentLine.Split(":")(1))
    54.                                 CurrentLine = BR.ReadString()
    55.                                 'Line 5
    56.                                 Dim Desc As String = String.Empty
    57.                                 Do Until CurrentLine.Contains("[image]")
    58.                                     Desc &= CurrentLine
    59.                                     CurrentLine = BR.ReadString()
    60.                                 Loop
    61.                                 PokemonDescription.Add(Desc)
    62.                                 'Line 6, this is the image line.
    63.                                 Dim ByteCount As Integer = CInt(CurrentLine.Split(":")(1))
    64.                                 Dim b() As Byte = BR.ReadBytes(ByteCount)
    65.                                 PokemonImage.Add(byteArrayToImage(b))
    66.                                 Do Until CurrentLine.Contains("[END OF POKEMON]")
    67.                                     CurrentLine = BR.ReadString()
    68.                                 Loop
    69.                             Loop
    70.                             CurrentLine = BR.ReadString()
    71.                         Loop
    72.                         BR.Close()
    73.                         ReadStream.Close()
    74.                     Catch ex As Exception
    75.                         GC.Collect()
    76.                         MessageBox.Show(ex.ToString)
    77.                     End Try
    78.                 Catch ex As Exception
    79.                     GC.Collect()
    80.                     MessageBox.Show(ex.ToString)
    81.                 End Try
    82.                 GC.Collect()
    83.                 If exportLists = True Then Call SaveLists()
    84.                 Return True
    85.             End Function
    86.             Public Sub SaveLists()
    87.                 'This is really easy actually :D
    88.                 Dim Count As Integer = 0
    89.                 Dim Amount As Integer = PokemonName.Count
    90.                 If Amount = 0 Then
    91.                     MessageBox.Show("No files were unpacked, aborting!", "No files to rewrite.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    92.                     Exit Sub
    93.                 End If
    94.                 'Now we have files to write
    95.                 Do While Count < Amount
    96.                     Using FS As New FileStream(String.Format("{0}.{1}", PokemonName(Count), "pidf"), FileMode.Create)
    97.                         Using BW As New BinaryWriter(FS)
    98.                             BW.Write(PokemonNumber(Count) & "-" & PokemonName(Count))
    99.                             BW.Write(PokemonType(Count))
    100.                             BW.Write("[description]")
    101.                             BW.Write(String.Format("{0}:{1}", PokemonHeight(Count), PokemonWeight(Count)))
    102.                             BW.Write(PokemonDescription(Count))
    103.                             BW.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(PokemonImage(Count)).Length.ToString))
    104.                             For Each b As Byte In imageToByteArray(PokemonImage(Count))
    105.                                 BW.Write(b)
    106.                             Next
    107.                         End Using
    108.                     End Using
    109.                     Count += 1
    110.                 Loop
    111.             End Sub
    112.             Public Sub PackFiles(ByVal pdxFileName As String, ByVal sourceDirectory As String)
    113.                 'We are going to read the files and write them at the same time.
    114.                 Try
    115.                     pdxFileName = If(pdxFileName.EndsWith(".pdfx") = True, pdxFileName, String.Format("{0}.{1}", pdxFileName, "pdfx"))
    116.                     Dim WriteStream As New FileStream(sourceDirectory & "\" & pdxFileName, FileMode.Create)
    117.                     Dim WriteBS As New BinaryWriter(WriteStream)
    118.                     For Each f As String In Directory.GetFiles(sourceDirectory, "*.pidf")
    119.                         Using ReadStream As New FileStream(f, FileMode.Open)
    120.                             Using ReadBR As New BinaryReader(ReadStream)
    121.                                 WriteBS.Write(ReadBR.ReadBytes(ReadStream.Length))
    122.                             End Using
    123.                             'WriteBS.Write(0)
    124.                             WriteBS.Write("[END OF POKEMON]")
    125.                         End Using
    126.                     Next
    127.                     WriteBS.Write("[END OF LINE]")
    128.                     WriteBS.Close()
    129.                     WriteStream.Close()
    130.                 Catch ex As Exception
    131.                     MessageBox.Show(ex.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
    132.                 End Try
    133.             End Sub
    134. #End Region
    135. #Region " Byte Image Manipulation "
    136.             Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
    137.                 Dim ms As New MemoryStream(byteArrayIn)
    138.                 Return Image.FromStream(ms, True, True)
    139.             End Function
    140.             Private Function imageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
    141.                 Dim ms As New MemoryStream()
    142.                 imageIn.Save(ms, imageIn.RawFormat)
    143.                 Dim B() As Byte = ms.GetBuffer()
    144.                 Return ms.GetBuffer
    145.             End Function
    146. #End Region
    147.         End Class
    148.     End Namespace

    Rest of the code...and a little extra

    vb.net Code:
    1. Public Module PublicVariables
    2.     Public PokemonName As New List(Of String)
    3.     Public PokemonNumber As New List(Of Integer)
    4.     Public PokemonHeight As New List(Of String)
    5.     Public PokemonWeight As New List(Of String)
    6.     Public PokemonDescription As New List(Of String)
    7.     Public PokemonType As New List(Of String)
    8.     Public PokemonImage As New List(Of Image)
    9. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width