Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DraguVaso - 27 Sep 2004 11:59 GMT
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
Jon Skeet [C# MVP] - 27 Sep 2004 12:09 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

DraguVaso - 27 Sep 2004 12:09 GMT
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)

> Hi,
>
[quoted text clipped - 28 lines]
>
> Pieter
Cor Ligthert - 27 Sep 2004 13:41 GMT
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
DraguVaso - 28 Sep 2004 08:46 GMT
Thanks Cor!

> Pieter,
>
[quoted text clipped - 66 lines]
>
> Cor

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.