Saving pics in Access

B

Brad Allison

I am using a small Access database as the back end of a VB .NET program. I
am not a newbie to Access, but I have never had the need to store pictures
until now. I have a field in a Master table named "Picture" and I have set
the Date Type as OLE Object. I know that this might be the wrong newsgroup
to post this to, but I was wondering if this is the correct data type to
handle pictures.

I use the following to store the pic in the datarow:
dogRow.Item("Picture") = CType(pbDog.Image, Bitmap)

The error message I am getting is this: Object must inplement IConvertible

Thanks for the information.

Brad
 
H

Herfried K. Wagner [MVP]

* "Brad Allison said:
I am using a small Access database as the back end of a VB .NET program. I
am not a newbie to Access, but I have never had the need to store pictures
until now. I have a field in a Master table named "Picture" and I have set
the Date Type as OLE Object. I know that this might be the wrong newsgroup
to post this to, but I was wondering if this is the correct data type to
handle pictures.

I use the following to store the pic in the datarow:
dogRow.Item("Picture") = CType(pbDog.Image, Bitmap)

The error message I am getting is this: Object must inplement IConvertible

\\\
Dim b As New Bitmap("C:\WINDOWS\Angler.bmp")
Dim ms As New MemoryStream
b.Save(ms, ImageFormat.Bmp)
Dim abyt(ms.Length - 1) As Byte

' Let's assume that 'abyt' is a byte array containing bitmap data read
' from the db after filling it...

ms.Seek(0, SeekOrigin.Begin)
ms.Read(abyt, 0, ms.Length)

' Byte array is filled.

Dim mx As New MemoryStream(abyt)
Me.BackgroundImage = Bitmap.FromStream(mx)

' Keep the memory stream upen until you don't need the image any more.
///
 
H

Herfried K. Wagner [MVP]

scorpion53061 said:
Does the picture have to be a bitmap to do this?

No, not necessarily. It will work with JPEG, GIF, PNG, and maybe TIFF, too.
 
C

Cor Ligthert

Hi Bard,

Every thing you want to know about it reading/writting, used is a xml
dataset.

In this complete example is everything skipped that is not really needed.
(Which are often in others samples about this, however not the Herfried one)

I hope this helps?

Cor

Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top