|
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 | |
Share:
|
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 | | |
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 | | |
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 | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by Thomas |
last post: by
|
2 posts
views
Thread by Fredo Vincentis |
last post: by
|
reply
views
Thread by erkidevries@pandora.be |
last post: by
|
1 post
views
Thread by Just D. |
last post: by
| | | | |
1 post
views
Thread by wilson |
last post: by
| | | | | | | | | | |