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)
Related
Am trying to copy my image value to text file. my code is below.
If fldtype = "System.Byte[]" Then
Dim bits As Byte() = CType(drow(dc), Byte())
Using ms As New MemoryStream(bits)
Dim sw As New StreamWriter(ms)
Dim sr As New StreamReader(ms)
Dim myStr As String = sr.ReadToEnd()
MessageBox.Show(myStr)
fldvalue = fldvalue + "," + myStr
End Using
I find this to be one of the easiest ways to write a Byte[] to a string:
If fldtype = "System.Byte[]" Then
Dim bits As Byte() = CType(drow(dc), Byte())
Dim s As String = Convert.ToBase64String(bits)
End If
Works in reverse, too:
Dim newBytes() As Byte = Convert.FromBase64String(s)
The answer linked in the original comments could prove to be better options. Answering these questions would go a long way:
Why write an image out to text in the first place?
What do you mean to do with it once it's there?
Do you need to be able to retrieve that string and re-form the original image?
Question #3 is especially important because the information you are capturing matters when it comes to image format information. That could be lost, making it difficult to reconstitute the original image 100%.
I have this code
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
and this one
Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))
They both do the same thing. What I want to know is, what are the pros and the cons of using either?
If your looking for one line commands then go with a function that returns that value, but leaves the ability for debugging with development.
Function:
Private Function GetOnlineData(url As String) As String
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(url)
Dim oUTF8 As New UTF8Encoding()
Return oUTF8.GetString(bHTML)
End Function
Usage:
Me.tbShowArea.Text = GetOnlineData(Me.tbURL.Text)
A very important aspect missing in both sets of code is the fact that WebClient implements IDisposable so it must be disposed of after use by calling .Dispose(). This is to prevent memory leaks from resources that do not get de-allocated. Remember the garbage collector does not call .Dispose() for you. You must explicitly call it in your code.
The first block of code can be written as:
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
oCliente.Dispose()
Or, even better, using a Using as:
Using oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData("Me.tbURL.Text")
Dim oUTF8 As New UTF8Encoding()
Dim Me_tbShowArea_Text = oUTF8.GetString(bHTML)
End Using
The second block of code is unable to be written to be able to call .Dispose() so it should be avoided.
Basically, the only difference is that the second one is chaining the functions so each one is getting the result of the previous one processing it and passing it to the next one, while the in first one each function is assigning the result to a variable.
There is no real difference in your code, but in other scenarios you may need to do more processing on the same result, in which case you use the variable. Let's say you want the WebClient to do more than one thing. Using the first, you can do something like:
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
bHTML = oCliente.DownloadData(Me.tbURL2.Text)
Me.tbShowArea2.Text = oUTF8.GetString(bHTML)
As you can see, we reused the variables to do the additional task. You cannot do that in the second way, you will have to repeat the whole thing:
Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))
Me.tbShowArea2.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL2.Text))
Now imagine that you have to put this in a loop for example. You can only do that with the first way.
There is no advantage in using the second apart from maybe barely reduce project size and/or make a part if the code which you know you won't touch again more compact.
That's it.
As for cons it makes debugging and tweaking far more difficult than it should be.
So far i have done the part that able to let me transfer text between the sender & receiver. Is there anyway to transfer an object by using namepipe? eg. arraylist
In vb.net you can also do this like C ….
Use serialize object and convert it to byte array transfer it and Deserialize on other end
Serialize
Dim BytArray() As Byte
Using MS As MemoryStream = Memory.Serialize(_Object)
BytArray = MS.GetBuffer()
End Using
Deserialize
Dim _Return As objType = Nothing
Using MS As System.IO.MemoryStream = New System.IO.MemoryStream(BytArray)
_Return = Memory.Deserialize(Of objType)(MS)
End Using
I am not sure about .NET or VB usage with named pipes but in Visual C++ I would pack the raw data from the array or object into a byte array and write this to the pipe. After reading this out of the pipe at the other end I would then rebuild the array or object from the raw data.
I'm trying to set the user agent for a request with XmlRead. I googled a lot about this and couldn't find the answer. Here is my chunk of code:
Dim RssData As DataSet
Dim Title As String
Dim Url As String
Dim Stream As String
Dim buffer As Integer
RssData = New DataSet()
RssData.ReadXml("http://localhost/user_agent.php")
buffer = 0
For Each RssRow As DataRow In RssData.Tables("entry").Rows
Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
Stream += Title & vbCrLf
Next
LinkLabel3.Text = Stream
For Each RssRow As DataRow In RssData.Tables("entry").Rows
Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
Url = RssRow.Item("url").ToString
LinkLabel3.Links.Add(buffer, Title.Length, Url)
buffer = buffer + Title.Length + 2
Next
The part of the code that actually performs the web request is buried pretty deep so you'd have to inherit a bunch of code to do what you asked for. Instead, let me suggest a different path, download the XML on your own with code that's easy to set that header, and then load that into the dataset. The WebClient class lets you set arbitrary headers and has a simple DownloadString method. Once you've got that you can wrap it in a MemoryStream and pass that into ReadXml(). (I couldn't find a way to read the XML as a string, that's why I was forced to read it as Stream.)
''//Will hold our downloaded XML
Dim MyXml As String
''//Create a webclient to download our XML
Using WC As New System.Net.WebClient()
''//Manually set the user agent header
WC.Headers.Add("user-agent", "your user agent here")
''//Download the XML
MyXml = WC.DownloadString("http://localhost/user_agent.php")
End Using
''//Create our dataset object
Dim RssData As New DataSet()
''//There is no direct method to load XML as a string (at least that I could find) so we will
''// convert it to a byte array and load it into a memory stream
Dim Bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(MyXml)
Using MS As New System.IO.MemoryStream(Bytes)
''//Load the stream into the reader
RssData.ReadXml(MS)
End Using
''//Your code continues normally here
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