|
-
Mar 28th, 2011, 12:15 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Problem with Save Image
i have problem when save image use MS Access Database.
field name is pict -> OLE OBJECT
hire my code when save image :
vb.net Code:
'simpan file gambar Dim img As Image = ucPhoto.getGambar If img IsNot Nothing Then Dim imgPar As New OleDbParameter Dim fs As FileStream Dim sfname As String = ucPhoto.FileName fs = New FileStream(sfname, FileMode.Open, FileAccess.Read) Dim picByte As Byte() = New Byte(CInt(fs.Length - 1)) {} fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length)) fs.Close() imgPar.OleDbType = OleDbType.Binary imgPar.ParameterName = "pict" imgPar.Value = picByte End If
when i run that code is not problem. but when i run code to display image i get error:
vb.net Code:
If dr.Item("pict") IsNot Nothing Then Dim fsImage As New IO.FileStream("img.jpg", IO.FileMode.Create) Dim blob As Byte() = DirectCast(dr.Item("pict"), Byte()) fsImage.Write(blob, 0, blob.Length) fsImage.Close() fsImage = Nothing ucPhoto.setImage(Image.FromFile("img.jpg")) End If
error at line :
Code:
Dim blob As Byte() = DirectCast(dr.Item("gambar"), Byte())
error message :
Code:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
thank you
-
Mar 28th, 2011, 12:23 AM
#2
Thread Starter
Addicted Member
Re: Problem with Save Image
I try to check again my code. and it turns out dr.Item field ("pict") NULL.
means that there is fail when saving the image. where lies the fault?
I show the following sql code:
vb.net Code:
scmd.CommandText = "insert into inv_t_products (prod_id,prod_name,unit,unitprice,id_group_prod,supp_id,pict) " & _
"values(@prodid,@prodname,@unit,@unitprice,@idgroup,@idsupp,@pict)"
-
Mar 28th, 2011, 12:52 AM
#3
Re: Problem with Save Image
Your "save" code doesn't actually save anything.
Also, there's no point testing your DataRow or DataReader field for Nothing because it will never be Nothing.
-
Mar 28th, 2011, 01:03 AM
#4
Thread Starter
Addicted Member
Re: Problem with Save Image
now I've managed to save and display the data, there are a few lines of code is wrong so that images can not be saved.
but now I have a problem when displaying data, there is an error message when I view the picture for the second time.
error description :
Code:
The process cannot access the file 'img.jpg' because it is being used by another process
-
Mar 28th, 2011, 01:16 AM
#5
Re: Problem with Save Image
Perhaps this might help
The MS-Access database has a table 'table1' with an OLE Object field 'ImageField' to store images.
Code:
Public Class Form1
' Resides in the Debug folder
Private FileName As String = "untitled.bmp"
' MDB resides in the Debug folder
Private ConnectionString As String = _
<Text>
Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=db.mdb;
</Text>.Value
Private Connection As New OleDb.OleDbConnection(ConnectionString)
Private da As New OleDb.OleDbDataAdapter("select ImageField from table1", _
ConnectionString)
Private ds As New DataSet()
Private cb As New OleDb.OleDbCommandBuilder(da)
Private Sub Form1_Load() Handles MyBase.Load
da.Fill(ds)
' simple demo only loads one image
If ds.Tables(0).Rows.Count = 1 Then
Exit Sub
End If
Dim br As New IO.BinaryReader(New IO.FileStream(FileName, _
IO.FileMode.Open, _
IO.FileAccess.Read, _
IO.FileShare.Read))
Dim row As DataRow
row = ds.Tables(0).NewRow
row.Item("ImageField") = br.ReadBytes(CInt(br.BaseStream.Length))
ds.Tables(0).Rows.Add(row)
da.Update(ds)
End Sub
Private Sub Form1_Shown() Handles Me.Shown
' Load First row image
PictureBox1.Image = Image.FromStream( _
New IO.MemoryStream(CType(ds.Tables(0).Rows(0).Item("ImageField"), Byte())))
End Sub
End Class
-
Mar 28th, 2011, 01:18 AM
#6
Re: Problem with Save Image
 Originally Posted by Tengkorak
now I've managed to save and display the data, there are a few lines of code is wrong so that images can not be saved.
but now I have a problem when displaying data, there is an error message when I view the picture for the second time.
error description :
Code:
The process cannot access the file 'img.jpg' because it is being used by another process
The Image.FromFile method locks the file. To unlock it, you must dispose the Image.
Do you really need to create the files at all? Can you not just create the Image object from a MemoryStream, as demonstrated by KI? You might also check out my thread on saving and loading images in databases in the VB.NET CodeBank forum.
-
Mar 28th, 2011, 01:42 AM
#7
Thread Starter
Addicted Member
Re: Problem with Save Image
thank you for the solution given, now I've managed to save and display images.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|