将 PNG 或 JPG 插入 MySQL 会损坏图像
Inserting a PNG or JPG into MySQL is corrupting the image
我在将一些 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)
文件被覆盖.......
非常感谢......
您必须先将图像转换为字节数组,然后才能将其存储在数据库中。我用的是Oracle,但我想Sql服务器是一样的。我从上传的照片中捕获照片,将图像缩放为缩略图,然后将其转换为字节数组以存储在数据库中。请参阅下面的代码:
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
我将图像存储在会话对象中,因为还有其他事情正在发生,并且在用户单击保存按钮之前我无法将图像保存到数据库中。将字节数组传递给存储过程以保存到数据库非常简单。
感谢您帮助我使用它。问题解决
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
我在将一些 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)
文件被覆盖.......
非常感谢......
您必须先将图像转换为字节数组,然后才能将其存储在数据库中。我用的是Oracle,但我想Sql服务器是一样的。我从上传的照片中捕获照片,将图像缩放为缩略图,然后将其转换为字节数组以存储在数据库中。请参阅下面的代码:
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
我将图像存储在会话对象中,因为还有其他事情正在发生,并且在用户单击保存按钮之前我无法将图像保存到数据库中。将字节数组传递给存储过程以保存到数据库非常简单。
感谢您帮助我使用它。问题解决
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