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?
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?