"Hevel, Shawn" <[EMAIL PROTECTED]> wrote:

> I'm reading the file in out of my hard drive.  I think run the
> ConvertStringToByteArray which converts it to a Byte[].  I then try to
> convert the Byte[] into the image.
> 
> StreamReader sr = new StreamReader(outFilePathAndName);

StreamReader is for turning bytes (on disk, or wherever) into text,
using an encoding (defaulting to UTF8Encoding).

> convertedImage = ConvertStringToByteArray(sr.ReadToEnd());
> 
> public static Byte[] ConvertStringToByteArray(string stringToConvert)
> {
>      return (new UnicodeEncoding()).GetBytes(stringToConvert);

And here you're converting the string back into bytes, using a different
encoding - Unicode. Of course, decoding a string from bytes using one
encoding and reencoding with another is going to get you a different set
of bytes than originally. UTF8 tries to stick ASCII chars into one byte,
and everything else gets 2 or more bytes; Unicode (on .NET) is UTF-16,
and tries to stick every char into 2 bytes, but possibly ends up being 4
bytes (IIRC).

If you really must create an image out of bytes, rather than from the
disk directly, then consider something like:

  byte[] bytes;
  using (Stream fin = File.OpenRead(inFile))
  {
      bytes = new byte[fin.Length];
      fin.Read(bytes, 0, bytes.Length);
  }

That will work because disk streams have a reliable Length and will read
all in one go, rather than bit by bit like socket streams etc.

You can then turn that byte array into an image using your current
MemoryStream techniques etc.

> Lead Programmer Analyst

No offence intended - but lord help us! ;)

-- Barry

-- 
http://barrkel.blogspot.com/

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to