By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Convert Images using IO.MemoryStream

P: n/a
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?
--
Dennis in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
--------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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?
--
Dennis in Houston
Nov 21 '05 #2

P: n/a
Thanks, I'll try it. Any idea why my code doesn't work?
--
Dennis in Houston
"Ken Tucker [MVP]" wrote:
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
--------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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?
--
Dennis in Houston

Nov 21 '05 #3

P: n/a
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.
--
Dennis in Houston
"Ken Tucker [MVP]" wrote:
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
--------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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?
--
Dennis in Houston

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.