我用它来从数据库中获取图像字节
cmd.CommandText = "select imagedate from projectimages where imagename = '" + _ ListBox1.Text + "' and CSVprojectref=checksum('" + textBoxFileRef.Text + "')" Dim img As Object = cmd.ExecuteScalar()
现在我该如何将它添加到PictureBox.image.我在检索图像并在PictureBox中显示它时遇到了很多麻烦.
数据类型是sql数据库中的Image,我使用此代码将图像保存到db
Dim ms As New IO.MemoryStream If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg) End If If imageFilename.Contains("png") Then imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Png) End If If imageFilename.Contains("gif") Then imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Gif) End If If imageFilename.Contains("bmp") Then imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp) End If Dim bytes() As Byte = ms.ToArray Dim img As String = Convert.ToBase64String(bytes) Dim cmd As New OleDb.OleDbCommand("insert projectimages values('" + imageNameTemp + "','" + img + "',CHECKSUM('" + textBoxFileRef.Text + "'))",con) cmd.ExecuteNonQuery()
经过5-6小时的搜索论坛和博客以及我喜欢的一切…将图像保存到数据库
1-数据类型应该是数据库中的图像
OpenFileDialog1.ShowDialog() imageFilename = OpenFileDialog1.FileName Dim imageUpload As Image imageUpload = Image.FromFile(OpenFileDialog1.FileName) If imageFilename <> "" Then Dim imageNameTemp As String imageNameTemp = imageFilename While (imageNameTemp.Contains("\")) imageNameTemp = imageNameTemp.Remove(0,imageNameTemp.IndexOf("\") + 1) End While Dim ms As New IO.MemoryStream If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp) End If 'Dim cmd As New sqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageFilename + "',@BLOBData,con) Dim b() As Byte = ms.ToArray() Dim cmd As New sqlCommand("INSERT INTO projectimages (imagename,csvprojectref) VALUES ('" + imageNameTemp + "',con) cmd.Parameters.Add("@BLOBData",sqlDbType.Image,b.Length).Value = b ' Dim cmd As New sqlCommand("insert projectimages(imagename,csvprojectref) values('imagma',con) cmd.ExecuteNonQuery() ' cmdTemp.Parameters.Add("@photo",b.Length).Value = b End If
cmd.CommandText = "select imagedate from projectimages where imagename = '" + ListBox1.Text + "' and CSVprojectref=checksum('" + textBoxFileRef.Text + "')" cmd.Connection = con Dim da As New sqlDataAdapter(cmd) Dim ds As New DataSet() da.Fill(ds,"projectimages") Dim c As Integer = ds.Tables(0).Rows.Count If c > 0 Then Dim bytBLOBData() As Byte = _ ds.Tables(0).Rows(c - 1)("imagedate") Dim stmBLOBData As New MemoryStream(bytBLOBData) PictureBox1.Image = Image.FromStream(stmBLOBData) End If