2017-04-10 18 views
1

JPG 또는 PNG 이미지를 MySQL에 삽입하는 데 문제가 있습니다. 이러한 이미지 중 일부가 손상되었습니다. 손상된 JPG의MySQL에 PNG 또는 JPG를 삽입하면 이미지가 손상됩니다.

스크린 샷 : 손상된 PNG의

JPG Corrupt

스크린 샷 :

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) 

의 폴더와 파일을 저장하고

의 exemple에 있기 때문에 함께 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