473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Convert Images using IO.MemoryStream

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
3 9157
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thomas | last post by:
I've searched google for this topic and found a few examples, but I need an example of how to create a semi-transparent, non-intrusive watermark for images using PIL, something that takes into...
2
by: Fredo Vincentis | last post by:
Is there any possibility to resize images using ASP? I provided one of my clients with a Content Management System using SAFileup and although I told him to resize images to a reasonable size...
0
by: erkidevries | last post by:
Problem compiling Tkinter program with bmp images (using py2exe) I have a Tkinter gui program that uses bmp as backgrounds. The program itself works when I run from the source. I placed the...
1
by: Just D. | last post by:
All, Does anybody know some library to resize/convert images on the fly? I'm especially interested in JPG/GIF/TIF images. Also maybe BMP, but not critical. Are we having some kind of a standard,...
0
by: vikaspa | last post by:
Can anyone help me how to load images using xml
6
by: roopashree | last post by:
hi, currently I am able to cut,copy and paste images for only one image. Suppose I have 4 images-then I should group all the images so that I can cut/copy all 4 images and paste them. How...
0
by: predmo | last post by:
I have an ASP.net application that produces a treeview, I would like the users to be able to change the appearance TreeNode images using a CSS .... is this possible?
1
by: Naha | last post by:
Hi, I am starting a new project where I have to create a office monitoring system, whereby I need to capture images from a webcam and analyse these images using Java advanced imaging in order to...
1
by: wilson | last post by:
hi i converted some P5 type .pgm images to .jpg using x=Image.open("oldimage.pgm") imsz=x.size newimg=Image.new('L',imsz) newimg.putdata(x.getdata()) newimg.save("newimg.jpg") when i again...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.