How to convert an Image into a Byte array?

  • Thread starter Thread starter Don
  • Start date Start date
D

Don

Is there any way to convert an Image into a Byte array that does not require
saving the image to disk?

- Don
 
Don said:
Is there any way to convert an Image into a Byte array that does not
require
saving the image to disk?

Save the image to a 'MemoryStream' object. 'MemoryStream' has a 'ToArray'
method which will return the bytes stored in the stream.
 
Don,

I was sure I had a sample for this, I was wrong, so I made a new one.
(Your problem is in the button event part)

\\\The sample needs two pictureboxes and a button on a form
Private Sub Form1_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fo As New OpenFileDialog
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim abyt As Byte()
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim abyt As Byte()
Dim ms As New IO.MemoryStream
PictureBox1.Image.Save(ms, Imaging.ImageFormat.Bmp)
abyt = ms.GetBuffer
Dim ms2 As New IO.MemoryStream(abyt)
Me.PictureBox2.Image = Image.FromStream(ms2)
Me.PictureBox1.Image = Nothing
End Sub
////
I hope this helps a little bit?

Cor
 
Back
Top