【发布时间】:2017-04-10 10:32:01
【问题描述】:
我在将一些 JPG 或 PNG 图像插入 MySQL 时遇到问题。其中一些图像已损坏。
损坏的 JPG 截图:
损坏的 PNG 的屏幕截图:
我的代码有什么问题?
代码:
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)
文件被覆盖........
非常感谢......
【问题讨论】:
-
“我的代码有什么问题???” – 除了您试图将图像放入数据库这一事实之外,您的意思是?图片属于文件系统,而不是数据库。
-
@CBroe:这是不正确的,也不是很有帮助。不过,在设计新应用程序时,这可能是一个经验法则。
-
我认为问题很简单。您不应该在数据库中使用图像!特别是您的图像看起来具有高分辨率,因此它会损坏,因为您可能无法将所有字节保存到数据库中。尝试使用 16*16px 的图标,您会发现它可能有效。
-
存储图像数据而不是文件名会使数据库不必要地膨胀。但是,始终使用 SQL 参数,使用 Add 而不是 AddWithValue,
GetBuffer是错误的。见stackoverflow.com/a/31370711
标签: mysql vb.net png jpeg corrupt