howto put a Byte Stream directly in a picture-box?

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

Hi,

In my application I receive a Byte Stream (Dim bytFile() As Byte) which
contains a jpeg-picture, which I want to display in a picturebox.

I want to display it directly from the bytfile() without first writing it to
a file and than reading it. Does anybody knows how to do this?



I hae alreaddy a solution with writing it to a file, but the next time i
want to do it I get an exception that the file is still in sue when I want
to write to it. So to get aroudn this kind of problems I want something
without file...

My code with the file:
Dim strPicLoc As String
strPicLoc = "C:\test.jpeg"
'picture
RetStatus = EIDlib1.GetPicture(MapColPicture, CertifCheck)
Dim br As New BinaryWriter(File.OpenWrite(strPicLoc))
Dim bytFile() As Byte
bytFile = MapColPicture.GetValue("Picture")
br.Write(bytFile)
br.Flush()
br.Close()
br = Nothing

'show the picture in the picturebox!
picPicture.Image = Image.FromFile(strPicLoc)

Thanks a lot in advance,

Pieter
 
DraguVaso said:
In my application I receive a Byte Stream (Dim bytFile() As Byte) which
contains a jpeg-picture, which I want to display in a picturebox.

That's not a byte stream - that's a byte array.
I want to display it directly from the bytfile() without first writing it to
a file and than reading it. Does anybody knows how to do this?

Sure - construct a MemoryStream with the byte array, and then use
Image.FromStream.
 
Ok I found it alreaddy myself!
Thanks anyways!

Dim bytFile() As Byte
bytFile = MapColPicture.GetValue("Picture")
'show the picture in the picturebox!
Dim mstr As New MemoryStream(bytFile)
picPicture.Image = Image.FromStream(mstr)
 
Pieter,

You have your answer, however here it is more complete so that you can do
all actions with it when it is your next question about a database. I have
eliminiated the to much writing using a memorystream what is mostly showed
in samples, although in your question that is needed and used as last part
in the sample.

Maybe you can use it.

Cor

\\\it needs a picturebox and four buttons on a page.
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
///

Cor
 
Back
Top