By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

Saving pics in Access

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Brad:

That's not going to work b/c you need to read it in as a stream.
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp

HTH,

Bill
www.devbuzz.com
www.knowdotnet.com

"Brad Allison" <tr*******@sbcglobal.net> wrote in message
news:Om*************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
* "Brad Allison" <tr*******@sbcglobal.net> scripsit:
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.
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

P: n/a
\\\
Dim b As New Bitmap("C:\WINDOWS\Angler.bmp")


Herfried,

Does the picture have to be a bitmap to do this?

Nov 20 '05 #4

P: n/a
scorpion53061 schrieb:
\\\
Dim b As New Bitmap("C:\WINDOWS\Angler.bmp")


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

P: n/a
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
Nov 20 '05 #6

P: n/a
Thanks for the responses. I will try this code when I get home tonight.

Brad

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.