Insert & receive picture from ms access query db vb.net - vb.net

so, I want to insert a picture to my ms-access DB and it works fine.
inserting code :
Dim ms As New System.IO.MemoryStream
Dim bmpImage As New Bitmap(GunaPictureBox1.Image)
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
Dim d As New DBConnect
d.Editdata(String.Format("INSERT INTO TeachersTable (TeacherFN, TeacherLN, [TeacherImage]) values('{0}','{1}','{2}')", GunaTextBox1.Text, GunaTextBox2.Text, bytImage))
display code :
Dim dt As DataTable = New DBConnect().selectdata(String.Format("SELECT [TeachersTable.TeacherImage] FROM TeachersTable where TeachersTable.ID= {0}", 1))
Dim EditTeacher As New EditTeacher
Dim pictureData As Byte() = DirectCast(dt.Rows(0)(1), Byte())
Dim stream As New IO.MemoryStream(pictureData)
EditTeacher.GunaPictureBox1.Image = Image.FromStream(stream)
EditTeacher.Show()
but now I want to display it on a picture box but when I execute the code its shows me an error said "Parameters is not valid" & I don't know where is the problem!

Related

Added collection of images from SQL datareader on ItextSharp

I need to get a collection of images from a datareader or even from a datatable (if recommended to use this) of sql.
My intent is to populate the cell with the variable "myimage" (and I succeed perfectly)
In the variable "myimage2" I would like to insert the collection of images obtained from a datareader or a datatable, what can I do?
Currently I can insert only one image using the "getvalue" method as written in the code.
Dim connessione As New SqlConnection
Dim comando As New SqlCommand
connessione.ConnectionString = stringa_database
connessione.Open()
comando = connessione.CreateCommand()
comando.CommandType = CommandType.Text
comando.CommandText = "SELECT u.id_nota, u.immagine From immagini_aggiuntive u INNER JOIN note q ON q.id = id_nota where id_nota= " & id
Dim lettura As SqlDataReader = comando.ExecuteReader()
lettura.Read()
Dim imageByte2 As Byte() = CType(lettura.GetValue(1), Byte())
Dim ms2 As New MemoryStream(imageByte2, 0, imageByte2.Length)
Dim myImage2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ms2)
'fine blocco
Dim imageByte As Byte() = CType(row.Cells(11).Value, Byte())
Dim ms As New MemoryStream(imageByte, 0, imageByte.Length)
Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ms)
Dim cellimg As PdfPCell = New PdfPCell()
cellimg.AddElement(myImage)
cellimg.AddElement(myImage2)
pdfTable.AddCell(cellimg)
lettura.Close()
connessione.Close()

VB NET check SQL server image data type for null or empty

I need to check for null value of SQL server image data type ,
here is my code :
Dim cmd As New SqlCommand("select id_image1 from work_tbl where
work_id = 12345 ", conn)
Dim stream As New MemoryStream()
conn.Open()
Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
conn.Close()
Dim bitmap As New Bitmap(stream)
picturebox1.Image = bitmap
I need to stop the procedure with msgbox if my image column is empty .
Dim cmd As New SqlCommand("select id_image1 from work_tbl where
work_id = 12345 ", conn)
Dim stream As New MemoryStream()
conn.Open()
if DBNull(cmd.ExecuteScalar())=false
Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(stream)
picturebox1.Image = bitmap
end if
conn.Close()

image in the SQL_database in vb.net

I have a problem to record the image in the SQL_database in vb.net.Enter as a file without a problem but I can not write in SQL.ABCreateNewBarcode is a PictureBox. I have a problem to take BackgroundImage from PictureBox to save in SQL. I save BackgroundImage in BarcodeImg folder but I can not save in SQl
Private Sub btnSaveBarcode_Click(sender As Object, e As EventArgs) Handles btnSaveBarcode.Click
'Create Image Object
Dim ABCreateNewBarcode As Object
ABCreateNewBarcode = CreateObject("BARCODE.BarcodeCtrl.1")
ABCreateNewBarcode.Text = txtNewBarcode.Text
ABCreateNewBarcode.typename = "Code128"
DirBarcodeImg = Application.StartupPath & "\barcodeimg"
'Save Image
If txtNewBarcode.Text = "" Then
MsgBox("Click the Create Button to create a New Barcode")
ElseIf Directory.Exists(DirBarcodeImg) = False Then
Call Directory.CreateDirectory(DirBarcodeImg)
Else
ABCreateNewBarcode.SaveAsBySize(DirBarcodeImg & "\" & txtInsertPartName.Text & ".png", 300, 130)
Dim ImageToSave As Image = ABCreateNewBarcode.BackgroundImage
Dim ms As New MemoryStream
ImageToSave.Save(ms, ImageToSave.RawFormat)
Dim buffer As Byte() = MS.GetBuffer()
'Add SQL Parameters
SQL.AddParam("#name", txtInsertPartName.Text)
SQL.AddParam("#image", buffer)
'Run Imsert Command
SQL.ExecQuery("INSERT INTRO information (PartName,BarcodeImg) " &
"VALUES (#name,#image) ")
End If
End Sub
After chaging you request "INTO" instead of "INTRO" try this line:
SQL.Parameters.AddWithValue("#image", txtInsertPartName.Text).SqlDbType = SqlDbType.Image
and replace :
Dim ms As New MemoryStream
ImageToSave.Save(ms, ImageToSave.RawFormat)
Dim buffer As Byte() = MS.GetBuffer()
by this:
Dim fs As FileStream
fs = New FileStream(imagename, FileMode.Open, FileAccess.Read)
'a byte array to read the image
Dim picbyte As Byte() = New Byte(fs.Length - 1) {}
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
'open the database using odp.net and insert the data
Dim buffer As Byte() =picbyte

Digital Person fingerprint image retrieving

I am using the following code to save the finger print scanned through Digital Persona's device.
Dim cls As New ClsDataAccess
Dim con = New SqlConnection(cls.SqlConnectiontring)
Dim cmd As New SqlCommand
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
'cmd.Parameters.Add(New SqlParameter("#fn", SqlDbType.VarChar, 10)).Value = "Joe"
'sql.DbType
Dim param(0) As SqlParameter
'Dim t As Integer = Join(serializedTemplate, ",")
param(0) = New SqlParameter("#biometricData", serializedTemplate)
'Public OnlineConnectionString As String = "Data Source = 203.234.5.678; Database = mydb; User CndID = username; Password = xxxxxx;"
'Dim cmd As New SqlCommand("Insert Into tbltestbio (biovalue) Values (#biometricData)", con)
'cmd.Parameters.Add(param)
Dim pictureParameter As SqlClient.SqlParameter = New SqlClient.SqlParameter("#Picture", SqlDbType.Binary)
pictureParameter.Value = serializedTemplate
cmd.Parameters.Add(pictureParameter)
The problem is that when I try to retrieve the image using memory stream like an ordinary image, it won't load. Any other image using my Object Browser is being displayed by this code. What am I doing wrong? Below is the code for image retrieval.
Sub ImageLoadFun(ByVal barrImg() As Byte)
Try
Dim ms As New MemoryStream(barrImg)
Dim returnImage As Image = Image.FromStream(ms)
PictureBox1.Image = returnImage.Image
EmployeeDrawPicture(img)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
It seems you get "template" from sensor and not "image".
Template is a list of minutiae and other info.
You need to check in your FingerPrint API if there's method to get image.
Be careful, usually image, from fingerprint sensor, is not in RGB format but in greyscale.

Loop through each row, create button for each record. Cannot get image from database

I have a form that I am trying to populate with a control for each item on my database (SQLCe). Problem is that one of the items I am trying to return from the database is an image. However, my original code gave me an error:
Value of type "Byte' cannot be converted to 'System.Drawing.Image'
Here is my original code
Private Sub btnCategories_Click(sender As Object, e As EventArgs) Handles btnCategories.Click
Dim dt As DataTable = ProducT_CATEGORYTableAdapter.GetData
For Each row As DataRow In dt.Rows
Dim btn As New btnCategoryTabs()
btn.lblCategoryName.Name = DirectCast(row("Category_Name"), String)
btn.lblCategoryName.Text = btn.lblCategoryName.Name
btn.picPCategoryPicture.Image = DirectCast(row("Image"), Byte) 'Error Here'
'Add categories to the Panel
flpMainPanel.Controls.Add(btn)
Next
End Sub
I am sure that I have to convert the image, so I started messing around with this bit of code:
Dim Stream As New MemoryStream()
Dim image As Byte() = CType('Can't figure out what to put here), Byte())
Stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(Stream)
Any help will be appreciated.
Thanks.
If you have stored image data in your database then it would be a Byte() i.e. and array, not just a single Byte. You then have to convert that Byte array to an Image. You're on the right track. Here's one I prepared earlier:
Dim connection As New SqlConnection("connection string here")
Dim command As New SqlCommand("SELECT Picture FROM MyTable WHERE ID = 1", connection)
connection.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
connection.Close()
Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.'
Using stream As New IO.MemoryStream(pictureData)
'Read the stream and create an Image object from the data.'
picture = Image.FromStream(stream)
End Using
http://www.vbforums.com/showthread.php?469562-Saving-Images-in-Databases&highlight=
In your case specifically, that becomes:
'Create a stream in memory containing the bytes that comprise the image.'
Using stream As New IO.MemoryStream(DirectCast(row("Image"), Byte()))
'Read the stream and create an Image object from the data.'
btn.picPCategoryPicture.Image = Image.FromStream(stream)
End Using