我有一個關於使用BLOB數據類型在數據庫上保存圖像的問題,它成功保存圖像,但是當我使用我的圖片框檢索它時,它會將我的圖像毀壞。生病告訴你我的應用程序的截圖。我正在使用vb.net。如何在不破壞圖片的情況下將圖像保存到數據庫中?
這是我在代碼保存在BLOB數據類型的圖像文件。
Dim filename As String = Me.OpenFileDialog1.FileName
Dim FileSize As UInt32
Dim conn As New MySqlConnection
conn = New MySqlConnection("server=localhost;user=root;password=;database=ticketing_system;")
conn.Open()
Dim mstream As New System.IO.MemoryStream()
Me.PbPicture.Image = Image.FromFile(Me.OpenFileDialog1.FileName)
Me.PbPicture.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand
Dim sql As String
mstream.Close()
' DBconn.Close()
sql = "INSERT INTO clientreports(img)VALUES(@File)"
Try
' DBconn.Open()
With sqlcmd
.CommandText = sql
.Connection = conn
.Parameters.AddWithValue("@File", arrImage)
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
這裏是我的代碼在圖片框上顯示我的圖像。
Dim strSQL As String
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
conn = New MySqlConnection("server=localhost;user=root;password=;database=ticketing_system;")
Dim SQLConnection As MySqlConnection = New MySqlConnection
'Dim connection As New SqlConnection("connection string here")
Dim command As New MySqlCommand("SELECT img FROM errdeschis where err_id='31'", conn)
conn.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
conn.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.
PictureBox1.Image = Image.FromStream(stream)
End Using
請大家幫幫我。
您是否在出發路線上驗證了路途中的長度?這可能有助於嘗試診斷破壞的情況。 –
沒有先生。我還沒有,順便說一下,先生們如何驗證VS的長度? –
無論如何,你知道如何將圖像存儲在數據庫之前進行轉換嗎? –