How to convert an iTextSharp.Image to vb .net MemoryStream - vb.net

I'm having a little trouble with the iTexSharp library for .Net.
I want to generate a QR code and show the resulting image in a web page (or save it into a file, or wathever). Problem is, the class BarcodeQRCode only has the method GetImage() wich returns an iTextShrap.text.image object. From there I've tried to use the RawData property to create a Memory Stream, and that Memory Stream to create a Bitmap, but i get the Invalid Parameter Error. This is the code
1 Dim oQR As New BarcodeQRCode("DATA TO BE ENCODED", 1, 1, Nothing)
2 Dim oMS As New MemoryStream(oQR.GetImage().RawData)
3 Dim oBitmap As New Bitmap(oMS)
4 oBitmap.Save("C:\Users\MyUser\Documents\codigoQR.png", System.Drawing.Imaging.ImageFormat.Png)
(Error in line 3)
So there seems to be a problem with the Byte Array RawData is returning. Does anyone know the correct way to do what i'm trying, or if I'm missing something?

Related

VB.net - SQLite query response turning empty after first interaction

so I'm using SQLite in a VB.net project with a populated database. I'm using the Microsoft.Data.Sqlite.Core and System.Data.SQLite NuGet package libraries. So the problem presents when I'm trying to get the result of a query. At first the SQLiteDataReader gets the response and all the elements of the desired table. I know this cause in the debugger I have a breakpoint after the setting the object and when I check the parameters of the SQLiteDataReader object the Result View shows all the elements of my table, but as soon as I remove the mouse from the object and check it again the Result View turns out empty, without even resuming with the next line of code. Does anyone know if its a known bug or something cause Ive used this exact method of querying a table in another project and it works.
The code:
Public Function RunQuery(com As String)
If CheckConnection() Then
command.CommandText = com
Dim response As SQLiteDataReader
response = command.ExecuteReader
Dim len As Integer = response.StepCount
Dim col As Integer = response.FieldCount
Dim resp(len, col) As Object
For i = 0 To col - 1
Using response
response.Read()
For j = 0 To len - 1
resp(i, j) = response.GetValue(j)
Next
End Using
Next
Debugger with populated result view
Debugger with empty result view
edit: added the for loop to show that its not only on the debugger that the result view is empty. When using response.Read() it throws an exception "System.InvalidOperationException: 'No current row'"
As I have told you in the comment, a DataReader derived class is a forward only retrieval object. This means that when you reach the end of the records returned by the query, that object is not capable to restart from the beginning.
So if you force the debugger to enumerate the view the reader reaches the end of the results and a second attempt to show the content of the reader fails.
The other part of your problem is caused by a misunderstanding on how to work on the reader. You should loop over the Read result not using a StepCount property. This property as far as I know, is not standard and other data providers don't support it. Probably it is present in the SQLite Provider because for them it is relatively easy to count the number of records while other providers don't attempt do calculate that value for performance reasons.
However, there are other ways to read from the reader. One of them is to fill a DataTable object with its Load method that conveniently take a DataReader
Dim data As DataTable = New DataTable()
Using response
data.Load(response)
End Using
' Now you have a datatable filled with your data.
' No need to have a dedicated bidimensional array
A DataTable is like an array where you have Rows and Columns instead of indexes to iterate over.

Buffer cannot be null

I try to download files from gridview .. I save files in database and then I display in grid-view I try this
I save files in database table not in folder so I try to download files
when i do this document is download but there is problem when i debug the code and check then in this line
Dim row = db_stu.dResult.Tables(0).Rows(i)
dResult shows
docid document docname docextension
1014 System.Byte[] Book2.xlsx .xlsx
and then when i further proceed docname shows "1912218726836.xlsx" this and also file download as a corrupt
These two lines together are wrong:
Dim binary() As Byte = TryCast(structDb.dstResult.Tables(0).Rows(i).Item("document"), Byte())
Dim ms As MemoryStream = New MemoryStream(binary)
The reason to use TryCast is that the object that you're trying to cast may not be the type you're trying to cast it as. In that case, TryCast will return Nothing. Use of TryCast should ALWAYS be followed by a test for Nothing, which you haven't done. You're using the result as though you're sure that there will be an object of that type. If you know that then you should be using DirectCast rather than TryCast.
Even if you do know that the reference will not be to an object of a different type and you use DirectCast though, if you cast a null reference, i.e. Nothing, then you're still going to get Nothing back. So, you first need to determine whether structDb.dstResult.Tables(0).Rows(i).Item("document") can refer to an object of a type other than Byte(). If it can't then use DirectCast rather than TryCast. Either way, it appears that that expression can produce Nothing so you need to check for Nothing either way, e.g.
Dim binary() As Byte = TryCast(structDb.dstResult.Tables(0).Rows(i).Item("document"), Byte())
If binary IsNot Nothing Then
Dim ms As MemoryStream = New MemoryStream(binary)
'...
End If
EDIT: If the column is nullable then you need to first test whether the row contains null and then only use the data if there is some:
Dim row = structDb.dstResult.Tables(0).Rows(i)
If Not row.IsNull("document") Then
'There is data so go ahead and use it.
Dim binary = DirectCast(row("document"), Byte())
'...

Read data from JSON file, add new data following data structure VB.NET

Okay, So I have been trying to wrap my small head around this topic for some time now. All I want to do is:
1) Read all data from a .json file
2) Add data to the json file, while still following structure (Adding objects withing java somehow maybe?)
3) Save file back
I have figured out how to download json.net and add it to my project. I just have no clue how to use it.
I am a big noob at java and vb, so please don't reply with a bunch of unnecessary stuff that won't help. (Really irritated by this already :|)
I am writing this with a GUI, so no console stuff, as I seen from most of the sources on the interweb.
Things I have tried: Reading all lines and storing into var, array and or string.
Dim str() As String = IO.File.ReadAllLines("C:\MCHCI_Profile.txt")
I got this from somewhere but threw and error of 1 dimensional array
Dim singleChar As Char
singleChar = str.Chars(14)
Somethings with streamreader and writer but not too much, as it confuses me.
Using sr As StreamReader = New StreamReader("C:\MCHCI_Profile.txt")
Do
ListBox1.Items.Add(sr.ReadLine())
Loop Until sr.EndOfStream
End Using
^This seemed to work, it added all the right data into combobox and kept json structure, but I don't know what to do with it.
Final conclusion
It seems like the only real way to do this is with json.net
So please let me know how to read data, add simple objects to it and save it back
Thank you !!!
As of now i don't get what actually you are trying to achieve. Let me assume that
1. You are accepting a text file content to a one dimensional array.
2. then you are selecting a single character from particular index from that array, isn't it?
this will achieve the first option without fail.
Dim str() As String = File.ReadAllLines("D:\sample.txt")
if you want particular line of text then you can take it from the array by using the index value as like the following:
Dim lineOfText As String = str(14)
if you want a single character from particular line of text then you can take it from the array by using the index value as like the following:
Dim singleChar As Char=str(14).ToCharArray()(2)

"System.Drawing.Image" cannot be converted to '1-dimensional array

I'm making a registration form with a picture in VB.Net using an Access database. Utilizing the following code, I am getting an error.
Me.Student_instructorTableAdapter.Insert(txtname,txtpass, ***PictureBox1.Image***)
Me.Student_instructorTableAdapter.Fill(Me.DatabaseDataSet4.student_instructor)
MsgBox("Successfully added", MsgBoxStyle.Information)
The PictureBox1.Image is saving into an Access Database ("picture") which throws an error:
"System.Drawing.Image" cannot be converted to '1-dimensional array"
What should I use instead of PictureBox1.Image when I call Insert in order to avoid the exception?
(Should be a comment but I need to post code)
We need the full exception message to know exactly what it's expecting (1-dimensional array of what?) Most likely it's a Byte array.
If so, you can convert an image to a Byte array like this...
Public Function ImageToByteArray(imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Return ms.ToArray()
End Function
You can convert a Byte array back to an image like this...
Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
See here for more information
I'm not familiar with access data types so it may support images directly. If so, you need to make sure that you've got the right data type selected for that column/field.
Incidentally, if you don't have to, don't use Access (or at least the storage engine/JET) for new projects - it's slow, unreliable with more than 10 users and has serious security issues. If you don't want a full-up SQL Server database, consider SQL Compact Edition (CE) or SQL Express.

Serialize/Deserialize two-dimensional array

For some reason my previous question was considered too vague. So let me be more specific.
I have a 2 dimensional array of type single.
I want to serialize it to save in an Access database.
The suggestion was to save it as a Memo field which is fine.
I want to later read the Memo field and deserialize it to retrieve the original array.
I have searched extensively on the web and here and can not find the answer. I believe I am serializing the array correctly but do not know how to deserialize it.
This code appears to work for serializing but I can not figure out how to deserialize:
Dim f As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ms As New MemoryStream
f.Serialize(ms, arLHS)
Dim byArr As Byte() = ms.ToArray
I then save byArr to the Memo field.
Please provide sample code.
You can deserialize it via base64 converter:
Dim str_b64 As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(str_b64))
Dim intArr2(,) As Int32 = f.Deserialize(ms2)
This may look somewhat awkward, but it works - tested in a console app in VS 2010.
Credit goes here. Through this link, you can also find the full version of the code to play with.
I changed the data type of the Access field from Memo to OLE Object and that seems to work. When I look at the data in Access it tells me the field is "Long binary data" and I was able to use the following code after reading the record from Access:
Dim f As New System.Runtime.Serialization.Formatters.BinaryFormatter
Dim byArr As Byte()
byArr = DirectCast(dtLHS.Rows(0).Item("LHS"), Byte())
Dim theArrayAsString As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(theArrayAsString))
Dim newLHS(,) as single = f.Deserialize(ms2)
Is this correct?