Click here to Skip to main content
15,885,067 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello How can i convert an image into binary array ??
Posted
Comments
Sergey Alexandrovich Kryukov 7-Jan-13 20:06pm    
Why? And, importantly, what do you use: System.Drawing? WPF? Something else? In all cases, read on imaging there; everything else is explained.
—SA

If you use System.Drawing, you can try this code

VB
Imports System.Drawing
Imports System.Drawing.Imaging

    Public Shared Function GetBinary(ByVal image As Image, ByVal format As ImageFormat) As Byte()
        Using ms As New System.IO.MemoryStream
            If (format Is Nothing) Then
                format = image.RawFormat ' use image original format
            End If
            image.Save(ms, format)
            Return ms.ToArray()
        End Using
    End Function


Example:

VB
Dim binaryArray As Byte()
' Get image from PictureBox and GetBinary (use original format)
binaryArray = GetBinary(PictureBox1.Image, Nothing)

' load image from file and GetBinary (formatted to png)
binaryArray = GetBinary(Image.FromFile(sourceImageFileName), ImageFormat.Png)

' To save current binaryArray to image file:
System.IO.File.WriteAllBytes(destinationImageFileName, binaryArray)
 
Share this answer
 
You can search for articles in the search box above. Like Convert Image File to Bytes and Back [^], for example.
 
Share this answer
 
Comments
Saad Mahdi Salh 7-Jan-13 20:03pm    
is there some simpler solution
Christian Graus 7-Jan-13 20:05pm    
No, there isn't. Why should there be - that looks straightforward to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900