Re: Getting images into byte arrays

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: MarkR (mark_at_bogus.yahoo.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 23:30:41 GMT

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



Relevant Pages

  • Re: increasing the size of a byte array and reading streams
    ... > reading the stream below (where buf is a byte array). ... When you're reading a file, for example, and you know that the file is ... There are times, however, when it makes sense to read a stream in small ... write those uncompressed bytes to your secondary MemoryStream. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Getting images into byte arrays
    ... If you insist on reading from the stream, I suspect you need to find a way ... > I want to get the ocntents of the JPEG image into a byte array. ... > Imports System.IO ...
    (microsoft.public.dotnet.framework)
  • Re: (Simple?) Unicode Question
    ... I have a very simple question eluding me. ... While reading a file, all I get is basically an array of bytes. ... This is no different, IMO, than treating a byte stream vs a image file. ...
    (comp.lang.python)
  • Re: Reading Zipped data from database record
    ... I used another program to extract the binary data into a file, ... not an array. ... How would I go about reading the array as a Zip stream? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fastest way to save an array of doubles on disk?
    ... > Imports System.IO ... > ' lets open a new stream and read in the array ... > For Each dbl As Double In anotherDoubles ...
    (microsoft.public.dotnet.languages.vb)