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 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
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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,...
|
by: vikaspa |
last post by:
Can anyone help me how to load images using xml
|
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...
|
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?
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |