Discussion:
Conversion Error
Hevel, Shawn
2007-01-05 14:22:34 UTC
Permalink
I'm trying to convert a System.Byte[] into System.Drawing.Image. I've
included my code below. When I execute System.Drawing.Image.FromStream(ms)
I get the following error: System.ArgumentException: Parameter is not
valid. Does anyone have an idea as to why this is throwing an error?



public System.Drawing.Image ByteArrayToImage(Byte[] byteArrayIn)

{

MemoryStream ms = new MemoryStream(byteArrayIn);

System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

return returnImage;

}



Thanks,



Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com <mailto:***@scfbins.com>
Home Email: ***@sc.rr.com




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

View archives and manage your subscription(s) at http://discuss.develop.com
Eames, Andrew
2007-01-05 14:34:45 UTC
Permalink
Patrick Steele
2007-01-05 17:43:58 UTC
Permalink
Shawn Wildermuth
2007-01-05 17:47:57 UTC
Permalink
Is this byte array coming from a database table? (e.g. an Image column)?


Thanks,
 
Shawn Wildermuth
Wildermuth Consulting Services, LLC
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-***@DISCUSS.DEVELOP.COM] On Behalf Of Hevel, Shawn
Sent: Friday, January 05, 2007 9:23 AM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Conversion Error

I'm trying to convert a System.Byte[] into System.Drawing.Image. I've
included my code below. When I execute System.Drawing.Image.FromStream(ms)
I get the following error: System.ArgumentException: Parameter is not
valid. Does anyone have an idea as to why this is throwing an error?



public System.Drawing.Image ByteArrayToImage(Byte[] byteArrayIn)

{

MemoryStream ms = new MemoryStream(byteArrayIn);

System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

return returnImage;

}



Thanks,



Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com <mailto:***@scfbins.com>
Home Email: ***@sc.rr.com




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

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

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

View archives and manage your subscription(s) at http://discuss.develop.com
Hevel, Shawn
2007-01-05 18:18:42 UTC
Permalink
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);
convertedImage = ConvertStringToByteArray(sr.ReadToEnd());

public static Byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}

Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com
Home Email: ***@sc.rr.com


-----Original Message-----
From: Shawn Wildermuth [mailto:***@ADOGUY.COM]
Sent: Friday, January 05, 2007 12:48 PM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Conversion Error

Is this byte array coming from a database table? (e.g. an Image column)?


Thanks,
 
Shawn Wildermuth
Wildermuth Consulting Services, LLC
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-***@DISCUSS.DEVELOP.COM] On Behalf Of Hevel, Shawn
Sent: Friday, January 05, 2007 9:23 AM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Conversion Error

I'm trying to convert a System.Byte[] into System.Drawing.Image. I've
included my code below. When I execute System.Drawing.Image.FromStream(ms)
I get the following error: System.ArgumentException: Parameter is not
valid. Does anyone have an idea as to why this is throwing an error?



public System.Drawing.Image ByteArrayToImage(Byte[] byteArrayIn)

{

MemoryStream ms = new MemoryStream(byteArrayIn);

System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

return returnImage;

}



Thanks,



Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com <mailto:***@scfbins.com>
Home Email: ***@sc.rr.com




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

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

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

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

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

View archives and manage your subscription(s) at http://discuss.develop.com
Eames, Andrew
2007-01-05 19:12:44 UTC
Permalink
Its not a string though - use BinaryReader.

You can just use Image.FromFile though (although be warned that this has the side effect of not closing the file)
Andrew

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:ADVANCED-***@DISCUSS.DEVELOP.COM] On Behalf Of Hevel, Shawn
Sent: Friday, January 05, 2007 1:19 PM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: [Spam:***** SpamScore] Re: [ADVANCED-DOTNET] Conversion Error

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);
convertedImage = ConvertStringToByteArray(sr.ReadToEnd());

public static Byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}

Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com
Home Email: ***@sc.rr.com


-----Original Message-----
From: Shawn Wildermuth [mailto:***@ADOGUY.COM]
Sent: Friday, January 05, 2007 12:48 PM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Conversion Error

Is this byte array coming from a database table? (e.g. an Image column)?


Thanks,
 
Shawn Wildermuth
Wildermuth Consulting Services, LLC
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-***@DISCUSS.DEVELOP.COM] On Behalf Of Hevel, Shawn
Sent: Friday, January 05, 2007 9:23 AM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Conversion Error

I'm trying to convert a System.Byte[] into System.Drawing.Image. I've
included my code below. When I execute System.Drawing.Image.FromStream(ms)
I get the following error: System.ArgumentException: Parameter is not
valid. Does anyone have an idea as to why this is throwing an error?



public System.Drawing.Image ByteArrayToImage(Byte[] byteArrayIn)

{

MemoryStream ms = new MemoryStream(byteArrayIn);

System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

return returnImage;

}



Thanks,



Shawn Hevel, API, AIT
Lead Programmer Analyst
Information Technology Department
South Carolina Farm Bureau Insurance Companies
Phone: (803) 936-4331
Fax: (803) 936-4629
Work Email: ***@scfbins.com <mailto:***@scfbins.com>
Home Email: ***@sc.rr.com

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

View archives and manage your subscription(s) at http://discuss.develop.com
Barry Kelly
2007-01-05 19:26:42 UTC
Permalink
"Hevel, Shawn" <***@SCFBINS.COM> 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
Continue reading on narkive:
Loading...