JPG 또는 PNG 이미지를 MySQL에 삽입하는 데 문제가 있습니다. 이러한 이미지 중 일부가 손상되었습니다. 손상된 JPG의MySQL에 PNG 또는 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)
의 폴더와 파일을 저장하고
의 exemple에 있기 때문에 함께 TextBox3.TextDim 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
@CBroe : 정확하지 않고 전혀 도움이되지 않습니다. 새로운 애플리케이션을 설계 할 때 명심해야 할 것은 엄지 손가락의 규칙 일 수 있습니다. –
나는 문제가 아주 간단하다고 생각한다. 데이터베이스에 이미지를 사용해서는 안됩니다! 특별히 당신의 이미지는 고해상도로 보이기 때문에 데이터베이스에 모든 바이트를 저장할 수 없기 때문에 이미지가 손상됩니다.아이콘 16 * 16px로 시도해 보면 효과가있을 것입니다. – Mederic