Extracting Image from database & displaying in picture box

G

Guest

I want to display the image from database to picture box through ado.net and vb.net

I have some images present in a sql server 2000 table stored under 'image' datatype. I want to extract and display them in a picture box present in a vb.net form

I appreciate any help on this

Thanks
PS
 
C

Cor Ligthert

Hi PS,

Reading it from a file, writing it to a file, putting it in the database,
reading it to the database,

Used is an XML file just to make it easy as sample.

I hope this helps a little bit.

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
 
C

Cor Ligthert

Hi Bill,

I do not know your relation to the maker of that sample, however in my
opinion uses it very much more code and resources (disk) than is needed.

I know that I have to complete that website I started with, I started with
it however I am in doubt if it has real value to complete it there is
already so much. Herfried did look to it, however that is not someone to be
a judge, will you have a look for me to it?

(I have still your email adres, to send you the link).

Cor
 
G

Guest

Hi Co

I am getting an error "Invalid Parameter Used" at
Me.PictureBox1.Image = Image.FromStream(ms

I appreciate your hel

Thanks
P

----- Cor Ligthert wrote: ----

Hi PS

Reading it from a file, writing it to a file, putting it in the database
reading it to the database

Used is an XML file just to make it easy as sample

I hope this helps a little bit

Co

Private abyt() As Byt
Private fo As New OpenFileDialo
Private sf As New SaveFileDialo
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e A
System.EventArgs) Handles Button1.Clic
'Reading a picture and put it in a bytearra
If fo.ShowDialog = DialogResult.OK The
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 erro
Dim ms As New IO.MemoryStream(abyt
Me.PictureBox1.Image = Image.FromStream(ms
End I
End Su

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

Private Sub Button3_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button3.Clic
'writing a bytearray to a datase
Dim ds As New DataSe
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) = aby
Dim sf As New SaveFileDialo
If sf.ShowDialog = DialogResult.OK The
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema
End I
End Su

Private Sub Button4_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button4.Clic
'reading a picture from a datase
Dim ds As New DataSe
If fo.ShowDialog = DialogResult.OK The
ds.ReadXml(fo.FileName
End I
abyt = CType(ds.Tables(0).Rows(0)(0), Byte()
Dim ms As New IO.MemoryStream(abyt
Me.PictureBox1.Image = Image.FromStream(ms
End Su
 
C

Cor Ligthert

Hi I should document it better once I think.

I tested it again and it works fine for me, however, you should first with
button 1 read a file from disk, and than if you want write that to a dataset
on disk and save the dataset and than you can read the dataset and than the
image is displayed from. Button 1, 3 and 4.

I tell this because this sentence you showed is two times in the sample,
ones with button 1 and once with button 4 so which one is it.

:)

Cor
 
W

William Ryan eMVP

I don't have any relation with Mahesh other than a professional admiration
of his work. I'd love to see what you have instead but I don't think in the
overall context of what he's doing that it's wasteful. I did a review of
two of his books including the GDI+ title
http://www.c-sharpcorner.com/Store/Books/0-321-16077-0.asp which that
example is from. I think it's a great book.

As far as being wasteful, I don't think storing blobs in a Database is ever
going to win any efficiency awards. All of the serialization and
deserialization and the like is never going to be as easy as opening a file
on a network share or local drive. However if your requirement is that you
have to do this, then it's as good of a method as any.

However I always love to see other ideas so by all means, zing me the code
(dotnetguru At Comcast Dot Net)

Thanks again,

Bill
--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
C

Cor Ligthert

Hi PS,

This should be all you need
Where the zeros are of course the indexes.
And do not close the memorystream.

When it is not working show me your code than I look to it tomorrow because
it is evening here again.

Dim abyt() as byte = DirectCast(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)

Cor
 
C

Cor Ligthert

Hi Ps,

An easy one I think,
Dim cmd As New SqlCommand("Select Signature From imagetest
where name='David'", cn)
Dim abyt() As Byte =
DirectCast(ds.Tables("imagetest").Rows(0)("signature"), Byte())

The field name is case sensetive luckily you wrote no * in the select so I
could see this.

Signature signature

Probably that is the error?

Cor
 
C

Cor Ligthert

Hi PS,

There are, however this one should work, the message parameters has in my
opinion nothing to do with the picture on what row do you get it?

Cor
 
G

Guest

Hi Cor

I have signatures drawn on the form using xy coordinates and stored in 'image' datatype of sql server 2000 table
I want to extract them from database table and draw them on a vb.net form as X & Y points/coords and make an image of signature again

Is there a way to do this

Your help/ suggestions is greatly appreciated

Thanks
P
 
C

Cor Ligthert

Hi PS,

There are two persons active in the newsgroups from which I think they can
can give you an answer on your question they are both active in different
newsgroups.

microsoft.public.dotnet.languages.vb
microsoft.public.dotnet.drawings

Are you sure it are not OLE Access 97 images or like that than you have to
make some little changes in the code I did show you.
I thought that I had it in VB.net however I cannot find it anymore, this is
it in C#, when you can not convert it tell me, than I do it and than I have
it in VB.net as well again.

http://groups.google.com/[email protected]

I hope this helps?

Cor
 
G

Guest

Thanks Cor

I am sure they are not OLE Access 97 images. All I have to do is extract the signatures from table and draw on form or picturebox at xy coords and get the image signature

Anyways, Thanks for your immense help and very quick and prompt replies

Rgds
P
----- Cor Ligthert wrote: ----

Hi PS

There are two persons active in the newsgroups from which I think they ca
can give you an answer on your question they are both active in differen
newsgroups

microsoft.public.dotnet.languages.v
microsoft.public.dotnet.drawing

Are you sure it are not OLE Access 97 images or like that than you have t
make some little changes in the code I did show you
I thought that I had it in VB.net however I cannot find it anymore, this i
it in C#, when you can not convert it tell me, than I do it and than I hav
it in VB.net as well again

http://groups.google.com/[email protected]

I hope this helps

Co
 
G

Guest

Hi there

Even I am having EXECTLY the same problem.... I have tried .gifs and even .jpg's
and I still get the Invalid Parameters error.

Any progress on this issue?

Thanks!
Steve
 

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