2014-02-10 41 views
0

我有一個關於使用BLOB數據類型在數據庫上保存圖像的問題,它成功保存圖像,但是當我使用我的圖片框檢索它時,它會將我的圖像毀壞。生病告訴你我的應用程序的截圖。我正在使用vb.net。如何在不破壞圖片的情況下將圖像保存到數據庫中?

img was ruin.

這是我在代碼保存在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 

請大家幫幫我。

+0

您是否在出發路線上驗證了路途中的長度?這可能有助於嘗試診斷破壞的情況。 –

+0

沒有先生。我還沒有,順便說一下,先生們如何驗證VS的長度? –

+0

無論如何,你知道如何將圖像存儲在數據庫之前進行轉換嗎? –

回答

1

OKAY.Use MEDIUMBLOB or LONGBLOB for the field。

0

你試過psren的回答嗎?

改變BLOB LONGBLOB到

這對我的作品!

+0

你爲什麼複製一個已經存在的答案? –

+0

對不起,但我只是澄清他/她的問題的解決方案,因爲我也遇到了他/她的問題,並發現psren的答案,因爲他/她沒有迴應。我只是想確保他/她已經檢查了psren的答案。 – HazzoN

+0

對不起,如果我犯了一個錯誤,我不會再做 – HazzoN

相關問題