locked
String and Array of Byte RRS feed

  • Question

  • Hi

    I used system.Text.Encoding to convert byte()  and string each other but it totally destroys files

    I need only two functions

    One is to convert Byte Array into String

    And other is String to Byte

    I've tried "For Each" method but it is very heavy

    Please help me!


    Do the Impossible

    Sunday, January 20, 2013 11:51 AM

Answers

  • I have clarified that i need two functions for inter-conversion of byte array and string

    http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

    http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
    • Edited by Thorsten Gudera Monday, January 21, 2013 6:41 AM
    • Proposed as answer by Heslacher Monday, January 21, 2013 6:42 AM
    • Marked as answer by NaxAlpha Monday, January 21, 2013 8:19 AM
    Monday, January 21, 2013 6:39 AM
  • Hey where are you answer me

    Do the Impossible

    Ok, you can create your own format to combine multiple files into a single file, but doing this does not involve using strings. Doing this involves manipulating binary data. Here is an example, it is quite lengthy, and you would still have to make your own format depending on your needs, and it gets really easy to mess things up if you're not paying attention. But here is the example:

    Option Strict On
    Imports System.Text
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CustomBinaryFile As New MyBinaryFile
            Dim OpenFileDialog As New OpenFileDialog
            OpenFileDialog.Filter = "pn (*.png)|*.png|jpg  (*.jpg)|*.jpg|bitmaps (*.bmp)|*.bmp"
            Dim Image1 As Image
            Dim Image2 As Image
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image1 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage1(Image1)
            Else
                Exit Sub
            End If
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image2 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage2(image2)
            Else
                Exit Sub
            End If
            Dim SaveFileDialog As New SaveFileDialog
            Dim SaveFileName As String
            SaveFileDialog.Filter = "Custom Format (*.myext)|*.myext"
            If SaveFileDialog.ShowDialog = DialogResult.OK Then
                SaveFileName = SaveFileDialog.FileName
                CustomBinaryFile.Save(SaveFileDialog.FileName)
            Else
                Exit Sub
            End If
            Dim LoadedCustomBinaryFile As MyBinaryFile = MyBinaryFile.FromFile(SaveFileName)
            PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox1.BackgroundImage = LoadedCustomBinaryFile.GetImage1
            PictureBox2.BackgroundImage = LoadedCustomBinaryFile.GetImage2
            MsgBox("These Images were loaded from your custom file!")
        End Sub
    End Class
    Public Class MyBinaryFile
        Private Property _Image1ByteCount As UInt64
        Private Property _Image1Bytes As Byte()
        Private Property _Image2ByteCount As UInt64
        Private Property _Image2Bytes As Byte()
        Private Property Image1Bytes As Byte()
            Get
                Return _Image1Bytes
            End Get
            Set(ByVal value As Byte())
                _Image1Bytes = value
                _Image1ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image1ByteCount As UInt64
            Get
                Return _Image1ByteCount
            End Get
        End Property
        Private Property Image2Bytes As Byte()
            Get
                Return _Image2Bytes
            End Get
            Set(ByVal value As Byte())
                _Image2Bytes = value
                _Image2ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image2ByteCount As UInt64
            Get
                Return _Image2ByteCount
            End Get
        End Property
        Public Sub SetImage1(ByVal Image As Image)
            Image1Bytes = GetImageBytes(Image)
        End Sub
        Public Sub SetImage2(ByVal Image As Image)
            Image2Bytes = GetImageBytes(Image)
        End Sub
        Public Function GetImage1() As Image
            Return Image.FromStream(New IO.MemoryStream(Image1Bytes))
        End Function
        Public Function GetImage2() As Image
            Return Image.FromStream(New IO.MemoryStream(Image2Bytes))
        End Function
        Public Shared Function FromFile(ByVal FileName As String) As MyBinaryFile
            If Not IO.File.Exists(FileName) Then Return New MyBinaryFile
            Dim LoadedFile As New MyBinaryFile
            Dim FileBytes As Byte() = IO.File.ReadAllBytes(FileName)
            Dim Image1ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 0)
            Dim Image1 As Byte() = GetBinaryField(FileBytes, 8, Image1ByteCount)
            Dim Image2ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 8 + Image1.Count)
            Dim Image2 As Byte() = GetBinaryField(FileBytes, Convert.ToUInt64(8 + Image1.Count + 8), Image2ByteCount)
            LoadedFile.SetImage1(Image.FromStream(New IO.MemoryStream(Image1)))
            LoadedFile.SetImage2(Image.FromStream(New IO.MemoryStream(Image2)))
            Return LoadedFile
        End Function
        Private Shared Function GetBinaryField(ByVal ByteArray As Byte(), ByVal Offset As UInt64, ByVal DataLength As UInt64) As Byte()
            Dim Bytes As New List(Of Byte)
            For I As UInt64 = Offset To CULng((Offset + DataLength - 1))
                Bytes.Add(ByteArray(CInt(I)))
            Next
            Return Bytes.ToArray
        End Function
        Public Sub Save(ByVal FileName As String)
            Dim FileBytes As Byte() = {}
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image1ByteCount))
            FileBytes = CombineArrays(FileBytes, Image1Bytes)
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image2ByteCount))
            FileBytes = CombineArrays(FileBytes, Image2Bytes)
            IO.File.WriteAllBytes(FileName, FileBytes)
        End Sub
        Function CombineArrays(ByVal Array1() As Byte, ByVal Array2() As Byte) As Byte()
            Dim AllResults(Array1.Length + Array2.Length - 1) As Byte
            Array1.CopyTo(AllResults, 0)
            Array2.CopyTo(AllResults, Array1.Length)
            Return AllResults
        End Function
        Private Function GetImageBytes(ByVal Image As Image) As Byte()
            Dim Bytes As Byte()
            Using ImageStream As IO.MemoryStream = New IO.MemoryStream
                Image.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Png)
                Bytes = ImageStream.ToArray()
            End Using
            Return Bytes
        End Function
    End Class


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.


    • Edited by Paul Ishak Monday, January 21, 2013 7:41 AM
    • Marked as answer by NaxAlpha Monday, January 21, 2013 8:20 AM
    Monday, January 21, 2013 7:31 AM

All replies

  • Hi  NaxAlpha,

    In the following link you can find the solution:

    http://www.chilkatsoft.com/faq/dotnetstrtobytes.html

    have a good day

    Ruggiero


    Ruggiero Lovreglio
    www.ruggierolovreglio.altervista.org

    Sunday, January 20, 2013 12:00 PM
  • Yea but what are you converting.

    A character is already for decennia not a byte anymore but an Unicode characters.

    Not the first time I write on a message around this to you I assume.

    Don't try to play trumpet on a piano.


     


    Success
    Cor

    Sunday, January 20, 2013 12:05 PM
  • Don't try to play trumpet on a piano.


    Success
    Cor

    haha:D

    i can play trumpet on my yamaha...


    thanks for any help

    Sunday, January 20, 2013 12:52 PM
  • What encoding does the file use when you read it in, and what do you use when you write it out?  If you change from input to output, why?

    --
    Mike
    Sunday, January 20, 2013 1:08 PM
  • Haaaaaaa!

    dkeddedkj!

    r

    lv2erlve[lge2l

    ewv!

    All replies are bad

    Nothing happened

    Dim b() As Byte = Filer.ReadAllBytes("1.png")
            Dim s As String = Encoding.UTF8.GetString(b)
            Filer.WriteAllText("2.png", s, False) 

    I tried any encoding but the file 1.png does not opens


    Do the Impossible

    Sunday, January 20, 2013 3:46 PM
  • What is being happened

    This function also does not work

        Function BTS(b() As Byte) As String
            Dim s As String = ""
            For Each x As Byte In b
                s += Chr(x)
            Next
            Return s
        End Function


    Do the Impossible

    Sunday, January 20, 2013 4:02 PM
  • If you want to copy a file, why not use IO.File.Copy?

    Converting a random selection of bytes (which is what a png file is w.r.t. a UTF-8 string) to a UTF-8 string will in general destroy the data because there are conversions which could be made, assuming the random bytes are even valid UTF-8.

    And what is the Filer method you write of?

    --
    Andrew

    Sunday, January 20, 2013 4:06 PM
  • sorry

    I have

    Imports Filer = Microsoft.VisualBasic.FileIO.FileSystem

    And also i tried utf 7,utf 8, utf 16 utf 32, unicode, bigunicode and ascii encoding but nothing done

    I have these functions

        Function BTS(b() As Byte) As String
            Dim s As String = ""
            For Each x As Byte In b
                s += Chr(x)
            Next
            Return s
        End Function
        Function STB(s As String) As Byte()
            Dim b As New List(Of Byte)
            For Each c As Char In s
                b.Add(Asc(c))
            Next
            Return b.ToArray
        End Function

    These are really good functions but when file is upto 100 kb it takes much time


    Do the Impossible

    Sunday, January 20, 2013 4:33 PM
  • What are you *really* trying to do?

    --
    Andrew

    Sunday, January 20, 2013 4:40 PM
  • I need fast functions for this task

    I need this

    

    F1(x) and F2(x) are two functions i need

    Only these two functions i am finding


    Do the Impossible

    Sunday, January 20, 2013 4:43 PM
  • Why do you need to do that? You know it breaks the data in the context you are using it. Maybe you want the FileStream Class.

    --
    Andrew

    Sunday, January 20, 2013 4:55 PM
  • It's a png file (i.e. binary data), so using a string is the wrong way.

    I'm trying to get NaxAlpha to say why he wants to do this, but he is being surprisingly resistant, lol.

    --
    Andrew

    Sunday, January 20, 2013 5:21 PM
  • It's a png file (i.e. binary data), so using a string is the wrong way.

    Oh lol, didn't notice that.... I'll remove it ;)

    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Sunday, January 20, 2013 5:24 PM
  • Dim b() As Byte = Filer.ReadAllBytes("1.png")

            Dim s As String = Encoding.UTF8.GetString(b)
            Filer.WriteAllText("2.png", s, False) 

    I tried any encoding but the file 1.png does not opens


    Do the Impossible

    You open an image file like this:

            Dim Image1 As Image = Image.FromFile("1.png")
            Image1.Save("1.png")
            Dim Image1 As Image = Image.FromFile("1.png")
       

    and this is how you save an image

         Image1.Save("1.png")

    Images are not strings, you're trying to convert the binary that is inside an image file to text, but there are characters that are lost when you attempt to store them as text, Even hex editors cannot display certain ascii characters(so they're replaced with a period in hex editors).

    I think you should answer Andrew's question of why you're trying to do it this way.


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.





    • Edited by Paul Ishak Sunday, January 20, 2013 5:46 PM
    Sunday, January 20, 2013 5:33 PM
  • I think I remember from one of his earlier threads that he wants to store multiple objects (images perhaps) in a single file and then be able to read the file and recreate the separate objects.

    If that's the case, I'm not sure where the idea of making each object a string came from.

    Sunday, January 20, 2013 6:08 PM
  • Ah, That's quite a bit of work, but totally possible. Sounds like a binary format to me...

    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Sunday, January 20, 2013 6:12 PM
  • Can your picture work for your requirements if the word String in the picture is replaced by the phrase "Byte Array"?

    --
    Mike
    Sunday, January 20, 2013 10:22 PM
  • What you think friends

    let me explain

    I did this using my two functions i told you

    Dim b() as Byte=Microsoft.VisualBasic.FileIO.FileSystem.ReadAllBytes("1.png")
    Dim s as string=BTS(b)
    Dim b2() as Byte=STB(s)
    Microsoft.VisualBasic.FileIO.FileSystem.ReadAllBytes("2.png",b2,False)

    These are my functions

      Function BTS(b() As Byte) As String
            Dim s As String = ""
            For Each x As Byte In b
                s += Chr(x)
            Next
            Return s
        End Function
        Function STB(s As String) As Byte()
            Dim b As New List(Of Byte)
            For Each c As Char In s
                b.Add(Asc(c))
            Next
            Return b.ToArray
        End Function

    But these are really very slow

    I need to make these functions fast

    And My questions is also that to make two functions for interconversion of String and Byte Array

    Please help me!

    And Don't fight with each others


    Do the Impossible

    Monday, January 21, 2013 4:41 AM
  • Ok, well I will rejoin the conversation when you answer the questions asked of you. Good luck

    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Monday, January 21, 2013 4:46 AM
  • Waht is problem with you

    I need to make two functions

    I've shown my own made but very slow functions

    I need some fast functions

    Please help me


    Do the Impossible

    Monday, January 21, 2013 4:52 AM
  • Waht is problem with you

    I have no problem. The problem is that people need clarification, and you have not answered the questions they have asked you in regards to your reasons for using a string. Please be courteous enough to provide the clarification that is requested of you, so YOUR problem can be solved. Thank you

    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.


    • Edited by Paul Ishak Monday, January 21, 2013 4:58 AM
    Monday, January 21, 2013 4:58 AM
  • I think I remember from one of his earlier threads that he wants to store multiple objects (images perhaps) in a single file and then be able to read the file and recreate the separate objects.

    ... if thats the case, either binary serialize an array of the files, or convert the files to base64 (memory overhead), setup a boundary between each file in the textfile/string so you can recreate the single files (just like in an email), or, probably the best, create your own filetype and binarywrite the contents of each file into your archive - apropos archive: how about zipping the files?

    Regards,

      Thorsten

    Monday, January 21, 2013 5:47 AM
  • I have clarified that i need two functions for inter-conversion of byte array and string faster than my slow functions!

    Please help me!


    Do the Impossible


    • Edited by NaxAlpha Monday, January 21, 2013 6:04 AM
    Monday, January 21, 2013 6:04 AM
  • Hey where are you answer me

    Do the Impossible

    Monday, January 21, 2013 6:30 AM
  • I have clarified that i need two functions for inter-conversion of byte array and string

    http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

    http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
    • Edited by Thorsten Gudera Monday, January 21, 2013 6:41 AM
    • Proposed as answer by Heslacher Monday, January 21, 2013 6:42 AM
    • Marked as answer by NaxAlpha Monday, January 21, 2013 8:19 AM
    Monday, January 21, 2013 6:39 AM
  • Hey where are you answer me

    Do the Impossible

    Ok, you can create your own format to combine multiple files into a single file, but doing this does not involve using strings. Doing this involves manipulating binary data. Here is an example, it is quite lengthy, and you would still have to make your own format depending on your needs, and it gets really easy to mess things up if you're not paying attention. But here is the example:

    Option Strict On
    Imports System.Text
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CustomBinaryFile As New MyBinaryFile
            Dim OpenFileDialog As New OpenFileDialog
            OpenFileDialog.Filter = "pn (*.png)|*.png|jpg  (*.jpg)|*.jpg|bitmaps (*.bmp)|*.bmp"
            Dim Image1 As Image
            Dim Image2 As Image
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image1 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage1(Image1)
            Else
                Exit Sub
            End If
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image2 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage2(image2)
            Else
                Exit Sub
            End If
            Dim SaveFileDialog As New SaveFileDialog
            Dim SaveFileName As String
            SaveFileDialog.Filter = "Custom Format (*.myext)|*.myext"
            If SaveFileDialog.ShowDialog = DialogResult.OK Then
                SaveFileName = SaveFileDialog.FileName
                CustomBinaryFile.Save(SaveFileDialog.FileName)
            Else
                Exit Sub
            End If
            Dim LoadedCustomBinaryFile As MyBinaryFile = MyBinaryFile.FromFile(SaveFileName)
            PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox1.BackgroundImage = LoadedCustomBinaryFile.GetImage1
            PictureBox2.BackgroundImage = LoadedCustomBinaryFile.GetImage2
            MsgBox("These Images were loaded from your custom file!")
        End Sub
    End Class
    Public Class MyBinaryFile
        Private Property _Image1ByteCount As UInt64
        Private Property _Image1Bytes As Byte()
        Private Property _Image2ByteCount As UInt64
        Private Property _Image2Bytes As Byte()
        Private Property Image1Bytes As Byte()
            Get
                Return _Image1Bytes
            End Get
            Set(ByVal value As Byte())
                _Image1Bytes = value
                _Image1ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image1ByteCount As UInt64
            Get
                Return _Image1ByteCount
            End Get
        End Property
        Private Property Image2Bytes As Byte()
            Get
                Return _Image2Bytes
            End Get
            Set(ByVal value As Byte())
                _Image2Bytes = value
                _Image2ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image2ByteCount As UInt64
            Get
                Return _Image2ByteCount
            End Get
        End Property
        Public Sub SetImage1(ByVal Image As Image)
            Image1Bytes = GetImageBytes(Image)
        End Sub
        Public Sub SetImage2(ByVal Image As Image)
            Image2Bytes = GetImageBytes(Image)
        End Sub
        Public Function GetImage1() As Image
            Return Image.FromStream(New IO.MemoryStream(Image1Bytes))
        End Function
        Public Function GetImage2() As Image
            Return Image.FromStream(New IO.MemoryStream(Image2Bytes))
        End Function
        Public Shared Function FromFile(ByVal FileName As String) As MyBinaryFile
            If Not IO.File.Exists(FileName) Then Return New MyBinaryFile
            Dim LoadedFile As New MyBinaryFile
            Dim FileBytes As Byte() = IO.File.ReadAllBytes(FileName)
            Dim Image1ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 0)
            Dim Image1 As Byte() = GetBinaryField(FileBytes, 8, Image1ByteCount)
            Dim Image2ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 8 + Image1.Count)
            Dim Image2 As Byte() = GetBinaryField(FileBytes, Convert.ToUInt64(8 + Image1.Count + 8), Image2ByteCount)
            LoadedFile.SetImage1(Image.FromStream(New IO.MemoryStream(Image1)))
            LoadedFile.SetImage2(Image.FromStream(New IO.MemoryStream(Image2)))
            Return LoadedFile
        End Function
        Private Shared Function GetBinaryField(ByVal ByteArray As Byte(), ByVal Offset As UInt64, ByVal DataLength As UInt64) As Byte()
            Dim Bytes As New List(Of Byte)
            For I As UInt64 = Offset To CULng((Offset + DataLength - 1))
                Bytes.Add(ByteArray(CInt(I)))
            Next
            Return Bytes.ToArray
        End Function
        Public Sub Save(ByVal FileName As String)
            Dim FileBytes As Byte() = {}
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image1ByteCount))
            FileBytes = CombineArrays(FileBytes, Image1Bytes)
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image2ByteCount))
            FileBytes = CombineArrays(FileBytes, Image2Bytes)
            IO.File.WriteAllBytes(FileName, FileBytes)
        End Sub
        Function CombineArrays(ByVal Array1() As Byte, ByVal Array2() As Byte) As Byte()
            Dim AllResults(Array1.Length + Array2.Length - 1) As Byte
            Array1.CopyTo(AllResults, 0)
            Array2.CopyTo(AllResults, Array1.Length)
            Return AllResults
        End Function
        Private Function GetImageBytes(ByVal Image As Image) As Byte()
            Dim Bytes As Byte()
            Using ImageStream As IO.MemoryStream = New IO.MemoryStream
                Image.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Png)
                Bytes = ImageStream.ToArray()
            End Using
            Return Bytes
        End Function
    End Class


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.


    • Edited by Paul Ishak Monday, January 21, 2013 7:41 AM
    • Marked as answer by NaxAlpha Monday, January 21, 2013 8:20 AM
    Monday, January 21, 2013 7:31 AM
  • Hey where are you answer me


    Do the Impossible

    Ok, you can create your own format to combine multiple files into a single file, but doing this does not involve using strings. Doing this involves manipulating binary data. Here is an example, it is quite lengthy, and you would still have to make your own format depending on your needs, and it gets really easy to mess things up if you're not paying attention. But here is the example:

    Option Strict On
    Imports System.Text
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CustomBinaryFile As New MyBinaryFile
            Dim OpenFileDialog As New OpenFileDialog
            OpenFileDialog.Filter = "pn (*.png)|*.png|jpg  (*.jpg)|*.jpg|bitmaps (*.bmp)|*.bmp"
            Dim Image1 As Image
            Dim Image2 As Image
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image1 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage1(Image1)
            Else
                Exit Sub
            End If
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                Image2 = Image.FromFile(OpenFileDialog.FileName)
                CustomBinaryFile.SetImage2(image2)
            Else
                Exit Sub
            End If
            Dim SaveFileDialog As New SaveFileDialog
            Dim SaveFileName As String
            SaveFileDialog.Filter = "Custom Format (*.myext)|*.myext"
            If SaveFileDialog.ShowDialog = DialogResult.OK Then
                SaveFileName = SaveFileDialog.FileName
                CustomBinaryFile.Save(SaveFileDialog.FileName)
            Else
                Exit Sub
            End If
            Dim LoadedCustomBinaryFile As MyBinaryFile = MyBinaryFile.FromFile(SaveFileName)
            PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox1.BackgroundImage = LoadedCustomBinaryFile.GetImage1
            PictureBox2.BackgroundImage = LoadedCustomBinaryFile.GetImage2
            MsgBox("These Images were loaded from your custom file!")
        End Sub
    End Class
    Public Class MyBinaryFile
        Private Property _Image1ByteCount As UInt64
        Private Property _Image1Bytes As Byte()
        Private Property _Image2ByteCount As UInt64
        Private Property _Image2Bytes As Byte()
        Private Property Image1Bytes As Byte()
            Get
                Return _Image1Bytes
            End Get
            Set(ByVal value As Byte())
                _Image1Bytes = value
                _Image1ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image1ByteCount As UInt64
            Get
                Return _Image1ByteCount
            End Get
        End Property
        Private Property Image2Bytes As Byte()
            Get
                Return _Image2Bytes
            End Get
            Set(ByVal value As Byte())
                _Image2Bytes = value
                _Image2ByteCount = Convert.ToUInt64(value.Count)
            End Set
        End Property
        Private ReadOnly Property Image2ByteCount As UInt64
            Get
                Return _Image2ByteCount
            End Get
        End Property
        Public Sub SetImage1(ByVal Image As Image)
            Image1Bytes = GetImageBytes(Image)
        End Sub
        Public Sub SetImage2(ByVal Image As Image)
            Image2Bytes = GetImageBytes(Image)
        End Sub
        Public Function GetImage1() As Image
            Return Image.FromStream(New IO.MemoryStream(Image1Bytes))
        End Function
        Public Function GetImage2() As Image
            Return Image.FromStream(New IO.MemoryStream(Image2Bytes))
        End Function
        Public Shared Function FromFile(ByVal FileName As String) As MyBinaryFile
            If Not IO.File.Exists(FileName) Then Return New MyBinaryFile
            Dim LoadedFile As New MyBinaryFile
            Dim FileBytes As Byte() = IO.File.ReadAllBytes(FileName)
            Dim Image1ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 0)
            Dim Image1 As Byte() = GetBinaryField(FileBytes, 8, Image1ByteCount)
            Dim Image2ByteCount As UInt64 = BitConverter.ToUInt64(FileBytes, 8 + Image1.Count)
            Dim Image2 As Byte() = GetBinaryField(FileBytes, Convert.ToUInt64(8 + Image1.Count + 8), Image2ByteCount)
            LoadedFile.SetImage1(Image.FromStream(New IO.MemoryStream(Image1)))
            LoadedFile.SetImage2(Image.FromStream(New IO.MemoryStream(Image2)))
            Return LoadedFile
        End Function
        Private Shared Function GetBinaryField(ByVal ByteArray As Byte(), ByVal Offset As UInt64, ByVal DataLength As UInt64) As Byte()
            Dim Bytes As New List(Of Byte)
            For I As UInt64 = Offset To CULng((Offset + DataLength - 1))
                Bytes.Add(ByteArray(CInt(I)))
            Next
            Return Bytes.ToArray
        End Function
        Public Sub Save(ByVal FileName As String)
            Dim FileBytes As Byte() = {}
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image1ByteCount))
            FileBytes = CombineArrays(FileBytes, Image1Bytes)
            FileBytes = CombineArrays(FileBytes, BitConverter.GetBytes(Image2ByteCount))
            FileBytes = CombineArrays(FileBytes, Image2Bytes)
            IO.File.WriteAllBytes(FileName, FileBytes)
        End Sub
        Function CombineArrays(ByVal Array1() As Byte, ByVal Array2() As Byte) As Byte()
            Dim AllResults(Array1.Length + Array2.Length - 1) As Byte
            Array1.CopyTo(AllResults, 0)
            Array2.CopyTo(AllResults, Array1.Length)
            Return AllResults
        End Function
        Private Function GetImageBytes(ByVal Image As Image) As Byte()
            Dim Bytes As Byte()
            Using ImageStream As IO.MemoryStream = New IO.MemoryStream
                Image.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Png)
                Bytes = ImageStream.ToArray()
            End Using
            Return Bytes
        End Function
    End Class


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.


    Wow That's wonderful!

    Do the Impossible

    Monday, January 21, 2013 8:18 AM
  • I have clarified that i need two functions for inter-conversion of byte array and string

    http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

    http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

    Amazing

    You solved my all problems


    Do the Impossible

    Monday, January 21, 2013 8:19 AM