2017-04-10 64 views
1

我在將一些JPG或PNG圖像插入MySQL時遇到問題。其中一些圖像已損壞。在MySQL中插入PNG或JPG會破壞圖像

截圖腐敗的JPG:

JPG Corrupt

腐敗PNG的截圖:

PNG Corrupt

什麼是錯我的代碼?

代碼:

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click 
    Dim cmd As New MySqlCommand 
    Dim SQL As String 
    Dim FileSize As UInt32 
    Dim rawData() As Byte = IO.File.ReadAllBytes(ListBox1.SelectedItem) 
    Dim fs As FileStream 
    Try 
     fs = New FileStream(ListBox1.SelectedItem.ToString, FileMode.Open, FileAccess.Read) 
     FileSize = fs.Length 
     rawData = New Byte(FileSize) {} 
     fs.Read(rawData, 0, FileSize) 
     'fs.Close() 
     MysqlConn.Open() 
     SQL = "INSERT INTO xcollectibles.foto (foto) VALUES(@foto)" 
     cmd.Connection = MysqlConn 
     cmd.CommandText = SQL 
     cmd.Parameters.AddWithValue("@foto", rawData) 
     cmd.ExecuteNonQuery() 
     fs.Close() 
     MessageBox.Show("File Inserted into database successfully!", 
     "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) 
     MysqlConn.Close() 
    Catch ex As Exception 
     MessageBox.Show("There was an error: " & ex.Message, "Error", 
       MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
End Sub 

我也曾嘗試:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    MysqlConn.Open() 
    Me.Cursor = Cursors.WaitCursor 
    For i = 0 To Me.ListBox1.Items.Count - 1 
     ProgressBar1.Maximum = Me.ListBox1.Items.Count - 1 
     Me.ListBox1.SetSelected(i, True) 
     Dim cmd As New MySqlCommand 
     Dim SQL As String 
     Dim filesize As UInt32 
     Dim mstream As New System.IO.MemoryStream() 
     If TextBox1.Text = ".jpg" Then 
      PictureBox1.Image.Save(mstream, Imaging.ImageFormat.Jpeg) 

     ElseIf TextBox1.Text = ".png" Then 
      PictureBox1.Image.Save(mstream, Imaging.ImageFormat.Png) 

     ElseIf TextBox1.Text = ".bmp" Then 
      PictureBox1.Image.Save(mstream, Imaging.ImageFormat.Png) 

     End If 


     'Dim bmp As New Bitmap(Width, Height) 
     'Dim g As Graphics = Graphics.FromImage(bmp) 
     'g.Clear(Color.Transparent) 
     'bmp.Save(mstream, System.Drawing.Imaging.ImageFormat.Png) 


     'End If 
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     Dim arrImage() As Byte = mstream.GetBuffer() 
     filesize = mstream.Length 
     mstream.Close() 
     SQL = "INSERT INTO xcollectibles.foto (id_product,foto) VALUES ((Select id from xcollectibles.product where product.name='" & ComboBox1.Text & "'), @foto) " 
     ProgressBar1.Value = i 
     cmd.Connection = MysqlConn 
     cmd.CommandText = SQL 
     cmd.Parameters.AddWithValue("@foto", arrImage) 
     cmd.ExecuteNonQuery() 
    Next 
    MessageBox.Show("File Inserted into database successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) 
    MysqlConn.Dispose() 
    ProgressBar1.Value = 0 
    Me.Cursor = Cursors.Default 
End Sub 

我現在嘗試不同的東西.... 保存在計算機裏的路徑的文件,並保存mysql中的路徑

我試試這個添加文件

System.IO.Directory.CreateDirectory("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images" & "\" & ComboBox1.Text) 
     Dim SaveFile As New System.IO.StreamWriter("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images" & "\" & ComboBox1.Text & "\" & TextBox3.Text) 

     If TextBox1.Text = ".jpg" Then 
      PictureBox1.Image.Save("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images\mypic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 
     ElseIf TextBox1.Text = ".bmp" Then 
      PictureBox1.Image.Save("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images\mypic.bmp", System.Drawing.Imaging.ImageFormat.Bmp) 
     ElseIf TextBox1.Text = ".png" Then 
      PictureBox1.Image.Save("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images\mypic.png", System.Drawing.Imaging.ImageFormat.Png) 
     End If 

但我想節約用

System.IO.Directory.CreateDirectory("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images" & "\" & ComboBox1.Text) 

該文件夾中的文件和文件與TextBox3.Text

Dim SaveFile As New System.IO.StreamWriter("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images" & "\" & ComboBox1.Text & "\" & TextBox3.Text) 

的名字,因爲與保存的

的爲例
PictureBox1.Image.Save("C:\Users\Jamyz\Source\Repos\xCollectibles\xCollectibles\xCollectibles\Images\mypic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 

該文件被覆蓋........

非常感謝您.......

+0

_「我的代碼有什麼問題???」_ - 除了事實上,你試圖把圖像放入數據庫中的第一位,你的意思是?圖像屬於文件系統,而不是數據庫。 – CBroe

+1

@CBroe:這是不正確的,根本沒有什麼幫助。儘管如此,在設計新應用程序時應該記住一條經驗法則。 –

+1

我認爲這個問題很簡單。你不應該在數據庫中使用圖像!特別是你的圖像看起來是高分辨率的,因此它會被破壞,因爲你可能無法將所有字節保存到數據庫中。試試用16 * 16px的圖標,你會發現它可能有效。 – Mederic

回答

0

您必須將圖像轉換爲字節數組,然後才能將其存儲在數據庫中。我使用Oracle,但我認爲Sql Server是一樣的。我從上傳中捕獲照片,將圖像縮放爲縮略圖,然後將其轉換爲字節數組以存儲在數據庫中。請參見下面的代碼:

If Not file1.PostedFile Is Nothing And file1.PostedFile.ContentLength > 0 Then 
     Session("ThePhoto") = "" 
     Dim TheStream As Stream = file1.PostedFile.InputStream 
     Dim origimage As System.Drawing.Image 
     origimage = System.Drawing.Image.FromStream(TheStream) 

     Dim ms2 As New System.IO.MemoryStream 
     origimage = ScaleImage(origimage, 320, 200) ' Thumbnail 
     origimage.Save(ms2, Imaging.ImageFormat.Jpeg) 
     Dim MyPhoto() As Byte = ms2.GetBuffer ' The scaled image is now stored in memory as a byte array 
     Session("ThePhoto") = MyPhoto ' put it into the session to retreive later 
     ms2.Dispose() 
     origimage.Dispose() 
    End If 

我的圖像存儲在會話對象,因爲有其他事情怎麼回事,我不能像尚未保存到數據庫中,直到用戶點擊保存按鈕。將字節數組傳遞給存儲過程以保存到數據庫非常簡單。

0

感謝您使用我的幫助。問題是解決的

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Me.Cursor = Cursors.WaitCursor 
    For i = 0 To Me.ListBox1.Items.Count - 1 
     ProgressBar1.Maximum = Me.ListBox1.Items.Count - 1 
     Me.ListBox1.SetSelected(i, True) 
     Dim cmd As New MySqlCommand 
     Dim SQL As String 
     'Dim FileSize As UInt32 
     Dim rawData() As Byte = IO.File.ReadAllBytes(ListBox1.SelectedItem) 


     Dim mstream As New System.IO.MemoryStream() 
     Dim arrImage() As Byte = mstream.GetBuffer() 
     mstream.Close() 

     'Save Image in Folder 
     Dim strBasePath As String 
     Dim strFileName As String 
     strFileName = TextBox3.Text 
     strBasePath = Application.StartupPath & "\Images" & ComboBox1.Text & "\" 
     ' >> Check if Folder Exists 
     If Directory.Exists(strBasePath) = False Then 
       Call Directory.CreateDirectory(strBasePath) 
      End If 
     ' >> Save Picture 
     If TextBox1.Text = ".jpg" Then 
      Call PictureBox1.Image.Save(strBasePath & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Jpeg) 
     ElseIf TextBox1.Text = ".png" Then 
      Call PictureBox1.Image.Save(strBasePath & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Png) 
     ElseIf TextBox1.Text = ".bmp" Then 
      Call PictureBox1.Image.Save(strBasePath & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Bmp) 
     End If 
     'Save Image in Folder 

     MysqlConn.Close() 
     MysqlConn.Open() 
     SQL = "INSERT INTO xcollectibles.foto (id_product,name,path) VALUES ((Select id from xcollectibles.product where product.name='" & ComboBox1.Text & "'), @name, @path) " 
     ProgressBar1.Value = i 
     cmd.Connection = MysqlConn 
     cmd.CommandText = SQL 
     cmd.Parameters.AddWithValue("@name", TextBox3.Text) 
     cmd.Parameters.AddWithValue("@path", strBasePath) 
     cmd.ExecuteNonQuery() 
    Next 
    MessageBox.Show("File Inserted into database successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) 
    MysqlConn.Dispose() 
    ProgressBar1.Value = 0 
    Me.Cursor = Cursors.Default 
End Sub