Can anyone tell me what is wrong with this code?

I want to get the ocntents of the JPEG image into a byte array. After
the theStream.read call, the theBytes array is still filled with all
nulls. and iCount is zero.

I used this article
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiomemorystreamclasstopic.asp)
as a starting point.

Eventually, what I want to have a function that will read an image
from disk in any format (BMP, TIF, JPG etc), convert it to some other
format, and then return it in a Byte array


Thanks for any help

RichardF



Imports System
Imports System.IO
Imports System.Text
Imports System.Drawing

Module Module1

Sub Main()

Dim theImage As Image
Dim theStream As New MemoryStream
Dim theBytes As Byte()
Dim iCount As Integer

Try
theImage = Image.FromFile("C:\Images\Test.jpg")
theImage.Save(theStream, Imaging.ImageFormat.Jpeg)
theBytes = New Byte(theStream.Length - 1) {}
iCount = theStream.Read(theBytes, 0, theStream.Length - 1)
Catch ex As Exception
End Try

End Sub

End Module

Re: Getting images into byte arrays by MarkR

MarkR
Wed Feb 16 17:30:41 CST 2005

I think you should try the Image.LockBits() function to access the image
contents, rather than reading back from a stream. This site is for C#, but
the algorithm should be apparent -- essentially, you call LockBits for a
portion of the image, then walk the BitmapData array.
http://www.codeproject.com/cs/media/csharpgraphicfilters11.asp

If you insist on reading from the stream, I suspect you need to find a way
to flush the stream. I'm not directly familiar with the function, but maybe
there's a Stream.Flush() or Stream.Close() function that will ensure your
StreamReader has the latest version...

Good luck,
/m


"RichardF" <noone@nowhere.com> wrote in message
news:ibk7119m9pgvg5g1kksas9cqdd8d1ighek@4ax.com...
> Can anyone tell me what is wrong with this code?
>
> I want to get the ocntents of the JPEG image into a byte array. After
> the theStream.read call, the theBytes array is still filled with all
> nulls. and iCount is zero.
>
> I used this article
> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiomemorystreamclasstopic.asp)
> as a starting point.
>
> Eventually, what I want to have a function that will read an image
> from disk in any format (BMP, TIF, JPG etc), convert it to some other
> format, and then return it in a Byte array
>
>
> Thanks for any help
>
> RichardF
>
>
>
> Imports System
> Imports System.IO
> Imports System.Text
> Imports System.Drawing
>
> Module Module1
>
> Sub Main()
>
> Dim theImage As Image
> Dim theStream As New MemoryStream
> Dim theBytes As Byte()
> Dim iCount As Integer
>
> Try
> theImage = Image.FromFile("C:\Images\Test.jpg")
> theImage.Save(theStream, Imaging.ImageFormat.Jpeg)
> theBytes = New Byte(theStream.Length - 1) {}
> iCount = theStream.Read(theBytes, 0, theStream.Length - 1)
> Catch ex As Exception
> End Try
>
> End Sub
>
> End Module



Re: Getting images into byte arrays by Robbe

Robbe
Wed Feb 16 19:02:00 CST 2005

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=799

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.mastervb.net/home/ng/forumvbcode/post10017013.aspx
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp



"RichardF" <noone@nowhere.com> wrote in message
news:ibk7119m9pgvg5g1kksas9cqdd8d1ighek@4ax.com...
> Can anyone tell me what is wrong with this code?
>
> I want to get the ocntents of the JPEG image into a byte array. After
> the theStream.read call, the theBytes array is still filled with all
> nulls. and iCount is zero.
>
> I used this article
> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiomemorystreamclasstopic.asp)
> as a starting point.
>
> Eventually, what I want to have a function that will read an image
> from disk in any format (BMP, TIF, JPG etc), convert it to some other
> format, and then return it in a Byte array
>
>
> Thanks for any help
>
> RichardF
>
>
>
> Imports System
> Imports System.IO
> Imports System.Text
> Imports System.Drawing
>
> Module Module1
>
> Sub Main()
>
> Dim theImage As Image
> Dim theStream As New MemoryStream
> Dim theBytes As Byte()
> Dim iCount As Integer
>
> Try
> theImage = Image.FromFile("C:\Images\Test.jpg")
> theImage.Save(theStream, Imaging.ImageFormat.Jpeg)
> theBytes = New Byte(theStream.Length - 1) {}
> iCount = theStream.Read(theBytes, 0, theStream.Length - 1)
> Catch ex As Exception
> End Try
>
> End Sub
>
> End Module



Re: Getting images into byte arrays by Jon

Jon
Wed Feb 16 20:59:15 CST 2005

RichardF <noone@nowhere.com> wrote:
> Can anyone tell me what is wrong with this code?
>
> I want to get the ocntents of the JPEG image into a byte array. After
> the theStream.read call, the theBytes array is still filled with all
> nulls. and iCount is zero.

You're writing to the stream, but then not "rewinding" the stream to
the start again before you're reading it.

Set the position to 0 before the read.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Getting images into byte arrays by Matt

Matt
Thu Feb 17 02:21:05 CST 2005

Hello RichardF,

How about

theBytes = theStream.ToArray()

--
Matt Berther
http://www.mattberther.com

> Can anyone tell me what is wrong with this code?
>
> I want to get the ocntents of the JPEG image into a byte array. After
> the theStream.read call, the theBytes array is still filled with all
> nulls. and iCount is zero.
>
> I used this article
>
> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
> f/html/frlrfsystemiomemorystreamclasstopic.asp)
>
> as a starting point.
>
> Eventually, what I want to have a function that will read an image
> from disk in any format (BMP, TIF, JPG etc), convert it to some other
> format, and then return it in a Byte array
>
> Thanks for any help
>
> RichardF
>
> Imports System
> Imports System.IO
> Imports System.Text
> Imports System.Drawing
> Module Module1
>
> Sub Main()
>
> Dim theImage As Image
> Dim theStream As New MemoryStream
> Dim theBytes As Byte()
> Dim iCount As Integer
> Try
> theImage = Image.FromFile("C:\Images\Test.jpg")
> theImage.Save(theStream, Imaging.ImageFormat.Jpeg)
> theBytes = New Byte(theStream.Length - 1) {}
> iCount = theStream.Read(theBytes, 0, theStream.Length - 1)
> Catch ex As Exception
> End Try
> End Sub
>
> End Module
>