Visual Basic Code Snippet - Convert file to byte array
(VB) Visual Basic code snippet convert external file to byte array. Converting file into byte array important to store binary file in database, send to other systems using remoting.
Bookmark:
Visual Basic Code Snippet - Convert file to byte array
This .Net Visual Basic code snippet convert external file to byte array. Converting file into byte array important to store binary file in database, send to other systems using remoting. To use this function simply provide file path to external file. This function uses System.IO name space to open file using FileStream and reading from BinaryReader. Modify the exception handling section to as your project requirements.
06 | Public Function FileToByteArray( ByVal _FileName As String ) As Byte () |
07 | Dim _Buffer() As Byte = Nothing |
11 | Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read) |
14 | Dim _BinaryReader As New System.IO.BinaryReader(_FileStream) |
17 | Dim _TotalBytes As Long = New System.IO.FileInfo(_FileName).Length |
20 | _Buffer = _BinaryReader.ReadBytes( CInt (Fix(_TotalBytes))) |
26 | Catch _Exception As Exception |
28 | Console.WriteLine( "Exception caught in process: {0}" , _Exception.ToString()) |
Here is a simple example showing how to use above function (FileToByteArray) load external image in a file to byte array and show it in a picturebox (this is just to demonstrate this function, there are other methods to load image in picturebox).
1 | Dim _MemoryStream As New System.IO.MemoryStream(FileToByteArray( "C:\sample-image.jpg" )) |
2 | pictureBox1.Image = System.Drawing.Image.FromStream(_MemoryStream) |
Here is a simple example showing how to use above function (FileToByteArray) to store image in sql server database. This example shows how to read external file and convert it into byte array and then insert binary data into SQL server as a new record using sql parameter.
02 | Dim _SqlConnection As New System.Data.SqlClient.SqlConnection() |
05 | _SqlConnection.ConnectionString = "Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;" |
13 | Dim _SQL As String = "INSERT INTO sampletable (name, price, image) VALUES ('sample product name', 22.75, @image)" |
16 | Dim _SqlCommand As New System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection) |
19 | Dim _SqlParameter As New System.Data.SqlClient.SqlParameter( "@image" , SqlDbType.Image) |
22 | _SqlParameter.Value = FileToByteArray( "C:\sample-image.jpg" ) |
23 | _SqlCommand.Parameters.Add(_SqlParameter) |
26 | _SqlCommand.ExecuteNonQuery() |
32 | Catch _Exception As Exception |
34 | Console.WriteLine( "Exception caught in process: {0}" , _Exception.ToString()) |
VB Keywords Used:
- FileStream
- BinaryReader
- FileInfo
- FileMode
- byte
- Exception
Code Snippet Information:
- Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, SQL, FileStream, BinaryReader, FileMode, File to byte array, Remoting, Store binary in database, SQL Server Binary Data
- Programming Language : Visual Basic (VB)
External Resources: