Convert Images using IO.MemoryStream

G

Guest

I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte
array containing the resulting JPEG Image as follows:

Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As Long)
As Byte()
'find the encoder with the image/jpeg mime-type
dim codecs as ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim ici As ImageCodecInfo
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
ici = codec
End If
Next
'Create a collection of encoder parameters
Dim ep As EncoderParameters = New EncoderParameters(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParameter(Encoder.Quality, Quality)

'THIS WORKS OK
BitMapIn.Save("c:\testbitmap.jpg", ici, ep)
Dim s As New System.IO.FileStream("c:\testbitmap.jpg",
IO.FileMode.Open, IO.FileAccess.Read)
Dim byt(CInt(s.Length() - 1)) As Byte
s.Read(byt, 0, byt.Length)
s.Close()

'THIS DOES NOT WORK and ONLY GIVES 0's in Byt Array
Dim s As New System.IO.MemoryStream
BitMapIn.Save(s, ici, ep)
Dim byt(CInt(s.Length() - 1)) As Byte
s.Read(byt, 0, CInt(s.Length))
Return byt
End Function

Why doesn't using the MemoryStream work?
 
K

Ken Tucker [MVP]

Hi,

This is how I do it.

Private Function StoreImage(ByVal bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFormat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function


Ken
--------------------
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte
array containing the resulting JPEG Image as follows:

Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As Long)
As Byte()
'find the encoder with the image/jpeg mime-type
dim codecs as ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim ici As ImageCodecInfo
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
ici = codec
End If
Next
'Create a collection of encoder parameters
Dim ep As EncoderParameters = New EncoderParameters(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParameter(Encoder.Quality, Quality)

'THIS WORKS OK
BitMapIn.Save("c:\testbitmap.jpg", ici, ep)
Dim s As New System.IO.FileStream("c:\testbitmap.jpg",
IO.FileMode.Open, IO.FileAccess.Read)
Dim byt(CInt(s.Length() - 1)) As Byte
s.Read(byt, 0, byt.Length)
s.Close()

'THIS DOES NOT WORK and ONLY GIVES 0's in Byt Array
Dim s As New System.IO.MemoryStream
BitMapIn.Save(s, ici, ep)
Dim byt(CInt(s.Length() - 1)) As Byte
s.Read(byt, 0, CInt(s.Length))
Return byt
End Function

Why doesn't using the MemoryStream work?
 
G

Guest

Ken, I found my error. I convert the BitMap to JPEG into a memory stream
then use .Read method to read into an array. I forgot to set the .Position
in the Memory stream to 0 so I effectively started reading at the end of the
file.

However, your method is much slicker and probably a lot quicker so I've
changed to just read the Buffer. Thanks again for the help.
 

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