473,549 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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. GetImageEncoder s()
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 EncoderParamete rs = New EncoderParamete rs(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParamete r(Encoder.Quali ty, Quality)

'THIS WORKS OK
BitMapIn.Save(" c:\testbitmap.j pg", ici, ep)
Dim s As New System.IO.FileS tream("c:\testb itmap.jpg",
IO.FileMode.Ope n, IO.FileAccess.R ead)
Dim byt(CInt(s.Leng th() - 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.Memor yStream
BitMapIn.Save(s , ici, ep)
Dim byt(CInt(s.Leng th() - 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 9186
Hi,

This is how I do it.

Private Function StoreImage(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function
Ken
--------------------
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:71******** *************** ***********@mic rosoft.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. GetImageEncoder s()
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 EncoderParamete rs = New EncoderParamete rs(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParamete r(Encoder.Quali ty, Quality)

'THIS WORKS OK
BitMapIn.Save(" c:\testbitmap.j pg", ici, ep)
Dim s As New System.IO.FileS tream("c:\testb itmap.jpg",
IO.FileMode.Ope n, IO.FileAccess.R ead)
Dim byt(CInt(s.Leng th() - 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.Memor yStream
BitMapIn.Save(s , ici, ep)
Dim byt(CInt(s.Leng th() - 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(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function
Ken
--------------------
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:71******** *************** ***********@mic rosoft.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. GetImageEncoder s()
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 EncoderParamete rs = New EncoderParamete rs(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParamete r(Encoder.Quali ty, Quality)

'THIS WORKS OK
BitMapIn.Save(" c:\testbitmap.j pg", ici, ep)
Dim s As New System.IO.FileS tream("c:\testb itmap.jpg",
IO.FileMode.Ope n, IO.FileAccess.R ead)
Dim byt(CInt(s.Leng th() - 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.Memor yStream
BitMapIn.Save(s , ici, ep)
Dim byt(CInt(s.Leng th() - 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(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function
Ken
--------------------
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:71******** *************** ***********@mic rosoft.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. GetImageEncoder s()
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 EncoderParamete rs = New EncoderParamete rs(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParamete r(Encoder.Quali ty, Quality)

'THIS WORKS OK
BitMapIn.Save(" c:\testbitmap.j pg", ici, ep)
Dim s As New System.IO.FileS tream("c:\testb itmap.jpg",
IO.FileMode.Ope n, IO.FileAccess.R ead)
Dim byt(CInt(s.Leng th() - 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.Memor yStream
BitMapIn.Save(s , ici, ep)
Dim byt(CInt(s.Leng th() - 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
1628
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 account the background of the image when choosing color for the text. Making a huge white box or just putting a single-color text across the entire...
2
3002
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 before uploading them, he didn't do it. If there was a way to do this using ASP, it would solve all problems.
0
1745
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 .bmp files in the same folder as the script. I run my .py script (from IDLE) and all the backgrounds are loaded as intended. in my script I load the...
1
2266
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, maybe freeware or open source library for that? The primary thing that I need is to decrease the image to show a small version on the page. The...
0
3374
by: vikaspa | last post by:
Can anyone help me how to load images using xml
6
3139
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 can I do this? I have used an array to store all the images. If you have any code for this please send it to me or a website link. Else logic as...
0
983
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
3499
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 determine if there are people in the images. Can someone please give me some guidance on how to do this, I am completely new to Java Advanced...
1
4820
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 check the pixel data for these images using getdata() method i,I find that they are slightly different
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.