I am trying to retrieve an image from an Embedded resource, and displaying it in its RAW DATA format (i.e. -> junk text data).
Basically, I am running into a wall with everything I attempt.
Can someone show me how to do this properly?
You can use the following msdn ref
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(fileName)
That will give you a Stream which you can then use code cite to convert to a byte array which is pretty much the raw data.
private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
Dim streamLength As Integer = Convert.ToInt32(stream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
' Read the file into a byte array
stream.Read(fileData, 0, streamLength)
stream.Close()
Return fileData
End Function
Related
I use the HDF5DotNet libraries with VB.net. I need to read a string dataset (3000 items, each item len = 16).
I use a byte array to store all the values but it's not easy to parse : I need to get a string by line and not a part of string. Do you know a better way to store and parse the result ?
Here my code:
'Load the file
Dim HDF5TestFileID As HDF5DotNet.H5FileId
HDF5TestFileID = H5F.open("C:\test.hdf5", H5F.OpenMode.ACC_RDONLY)
'Get datset and group id
Dim GroupRootId As HDF5DotNet.H5GroupId = H5G.open(HDF5TestFileID, "/")
Dim dataSetRN As H5DataSetId = H5D.open(GroupRootId, "MyItemsNames")
'Build byte array from the dataset
Dim readDataBackRN(16 * 3000) As Byte
Dim h5DataBackRN As New H5Array(Of Byte)(readDataBackRN)
Dim typeIdRN As H5DataTypeId = H5D.GetType(dataSetRN)
H5D.read(dataSetRN, typeIdRN, h5DataBackRN)
'try to parse the result but not easy to use data
Dim content as string = System.Text.Encoding.UTF8.GetString(readDataBackRN).Replace(" ", "<br>")
You normally read the buffer, check if the string is complete and if not continue reading and append the new contents to the previous fragment and keep doing that until you find the end of line.
Can you do this?
In this case the string is complete but it's possible that one day I reach the limit of size. I cannot modify the H5D.read method. I wonder if an simple array of byte is the best way ?
The better way would be to store result in a an array of array of bytes (1 array of byte per line of string) and not store all items in a single array of bytes ?
But I don't knwon if it's possible.
I wrote the follow function to convert an image to bytes:
Public Function ConvertImage(ByVal myImage As Image) As Byte()
'store image in memory before converting and
'create memory stream, save image in proper format
Dim mStream As New MemoryStream
myImage.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg)
'convert new stream into bytes and indicate size
Dim myBytes(mStream.Length - 1) As Byte
mStream.Position = 0
mStream.Read(myBytes, 0, mStream.Length)
Return myBytes
End Function
The function works great, however, I am struggling in trying to figure out how to best modify my function to also check if an image exist. if an image exists, then convert, if no image exists, then return Nothing from a particular path and then convert. Can I do it on this function, or do I have to write another function?
I have a web service,which return data in byte array.Now i want to read that data in my console project.How can i do that,i already add the desire references to access that web service.I am using vb.net VS2012.Thanks.My web service method is as follow.
Public Function GetFile() As Byte()
Dim response As Byte()
Dim filePath As String = "D:\file.txt"
response = File.ReadAllBytes(filePath)
Return response
End Function
Something like,
Dim result As String
Using (Dim data As New MemoryStream(response))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
if you knew the encoding, lets say it was UTF-8 you could do,
Dim result = System.Text.UTF8Encoding.GetString(response)
Following on from your comments, I think you are asserting this.
Dim response As Byte() 'Is the bytes of a Base64 encoded string.
So, we know all the bytes will be valid ASCII (because its Base64,) so the string encoding is interchangable.
Dim base64Encoded As String = System.Text.UTF8Encoding.GetString(response)
Now, base64Encoded is the string Base64 representation of some binary.
Dim decodedBinary As Byte() = Convert.FromBase64String(base64Encoded)
So, we've changed the encoded base64 into the binary it represents. Now, because I can see that in your example, you are reading a file called "D:/file.txt" I'm going to make the assumption that the contents of the file is a character encoded string, but I don't know the encoding of the string. The StreamReader class has some logic in the constructor that can make an educated guess at character encoding.
Dim result As String
Using (Dim data As New MemoryStream(decodedBinary))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
Hopefully, now result contains the context of the text file.
I want to write a byte array to the end of an existing file. How to append byte array to file?
Here is the solution. Just use the following sub and provide the parameters as required:
Parameters description:
FilepathToAppendTo is the filepath you need to append the byte array
Content is your byte array
Private Sub AppendByteToDisk(ByVal FilepathToAppendTo As String, ByRef Content() As Byte)
Dim s As New System.IO.FileStream(FilepathToAppendTo, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
s.Write(Content, 0, Content.Length)
s.Close()
End Sub
Use System.IO.FileStream class methods. Open/create a FileStream in append file mode.
System.IO.FileStream(filename,System.IO.FileMode.Append)
Dim bufData As Byte()
' write the entire buffer in one line of code
My.Computer.FileSystem.WriteAllBytes("BinaryFile.DAT", bufData, append := True)
Assumptions.
You want to use UTF8 encoding.
using( var stream = File.AppendText(#"D:\test.txt"))
{
stream.WriteLine(Encoding.UTF8.GetString( b ) );
}
VB Version:
Using stream = File.AppendText("D:\test.txt")
stream.WriteLine(Encoding.UTF8.GetString(b))
End Using
Reading binary data out of the database, and I need to convert it back into a Digital Persona fingerprint template. I'm not familiar with serialization and deserialization, so I could use a bit of help. Here's what I tried:
Dim rsBioData As SqlDataReader = SQL.ExecuteReader
Dim byteTemplate As Byte
Dim memStreamTemplate As MemoryStream
If rsBioData.HasRows Then
While rsBioData.Read
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template = DirectCast(template.DeSerialize(memStreamTemplate), DPFP.Template)
End While
End If
rsBioData.Close()
I receive an error that template.DeSerialize(memStreamTemplate) does not create a value.
For kicks, here's how I serialized the object to place it into the database. I assume this part is working, since the binary data shows up in SQL server--just can't read it back out to see.
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
SQL.Parameters.AddWithValue("biometricData", serializedTemplate)
Thanks
Here's how I was finally able to do it. I was SO close the first time around.
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template.DeSerialize(memStreamTemplate)