Ovog puta problem je sa čuvanjem slike u DB (SQL 2005 express).
1. Kad izaberem sliku sa diska snimanje u bazu radi kako treba
2. Kad sliku izvučem iz baze u picturebox-u se prikaže normalno, ali pri snimanju iste te slike dobijam grešku "A generic error occurred in GDI+" u liniji gde se slika snima u MemoryStream
Pokušavao sam sve i svašta, ali bez većih uspeha tako da molim vas za pomoć. Polje za sliku u DB-u je tipa IMAGE
Kod za snimanje slike
Code:
......
If Not cPerson.Image Is Nothing Then
Dim ms As New IO.MemoryStream
cPerson.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b() As Byte = ms.GetBuffer
sqlComm.Parameters.Add("@Picture", SqlDbType.Image, ms.Length).Value = b
ms.Close()
Else
sqlComm.Parameters.Add("@Picture", SqlDbType.Image).Value = DBNull.Value
End If
.......
......
If Not cPerson.Image Is Nothing Then
Dim ms As New IO.MemoryStream
cPerson.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b() As Byte = ms.GetBuffer
sqlComm.Parameters.Add("@Picture", SqlDbType.Image, ms.Length).Value = b
ms.Close()
Else
sqlComm.Parameters.Add("@Picture", SqlDbType.Image).Value = DBNull.Value
End If
.......
Kod za čitanje slike
Code:
If dr("Picture").Equals(DBNull.Value) = False Then
Dim ms As New IO.MemoryStream(CType(dr("Picture"), Byte()))
cPerson.Image = Image.FromStream(ms)
ms.Close()
End If
If dr("Picture").Equals(DBNull.Value) = False Then
Dim ms As New IO.MemoryStream(CType(dr("Picture"), Byte()))
cPerson.Image = Image.FromStream(ms)
ms.Close()
End If
Kod za "biranje" slike
Code:
With OpenFileDialog1
.Filter = "Bitmap|*.bmp|JPEG|*.jpg"
.FilterIndex = 2
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Dim picFile As New IO.FileStream(.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim b(CInt(picFile.Length - 1)) As Byte
picFile.Read(b, 0, CInt(picFile.Length))
picFile.Close()
Dim bmp As New Drawing.Bitmap(New IO.MemoryStream(b))
cPerson.Image = bmp
End If
End With
With OpenFileDialog1
.Filter = "Bitmap|*.bmp|JPEG|*.jpg"
.FilterIndex = 2
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Dim picFile As New IO.FileStream(.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim b(CInt(picFile.Length - 1)) As Byte
picFile.Read(b, 0, CInt(picFile.Length))
picFile.Close()
Dim bmp As New Drawing.Bitmap(New IO.MemoryStream(b))
cPerson.Image = bmp
End If
End With
Naučio sam...
Da je važnije biti ljubazan nego biti u pravu
Da je važnije biti ljubazan nego biti u pravu