I
iyuen
I'm having problems with converting a byte array to an image object~
My byte array is an picture in VB6 StdPicture format. I've used propertybag
to convert the picture into base64Array format in XML, and embedded
the array as some child element in an xml file, i.e.:
<Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA
AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA
AABVeQAAtDMAAP/3AAD/7wAA//8AAP//AAA=</Mask>
As I read the xml file in .NET, I parse my xml, retrieve this string, and
try to convert it into an Image object or Bitmap in the following code.
However, as I try to return the Bitmap object in the try/catch block, an
ArgumentException ("Invalid parameters used") is always thrown. I've used
an alternative route i.e "return Image.FromStream(bitmapData)" but I always
receive an exception.
I've search numerous times in googld about this problem but no one has a
firm grip of what's going on. So do all of your C#/.NET guru can tell me
what's going on and what I should do to get it to work?
I've seen a post on the web that suggests I may need a bitmap header in my
array. If that's so...how should I encode it?
Thanks,
internal static Bitmap convertImage(string imageText){
imageText = imageText.Replace("\r\n", String.Empty);
Byte[] bitmapData;
bitmapData = Convert.FromBase64String(imageText);
MemoryStream streamBitmap = new MemoryStream(bitmapData);
try
{
return new Bitmap(streamBitmap);
}
catch (ArgumentException e)
{
throw e;
}
}
My byte array is an picture in VB6 StdPicture format. I've used propertybag
to convert the picture into base64Array format in XML, and embedded
the array as some child element in an xml file, i.e.:
<Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA
AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA
AABVeQAAtDMAAP/3AAD/7wAA//8AAP//AAA=</Mask>
As I read the xml file in .NET, I parse my xml, retrieve this string, and
try to convert it into an Image object or Bitmap in the following code.
However, as I try to return the Bitmap object in the try/catch block, an
ArgumentException ("Invalid parameters used") is always thrown. I've used
an alternative route i.e "return Image.FromStream(bitmapData)" but I always
receive an exception.
I've search numerous times in googld about this problem but no one has a
firm grip of what's going on. So do all of your C#/.NET guru can tell me
what's going on and what I should do to get it to work?
I've seen a post on the web that suggests I may need a bitmap header in my
array. If that's so...how should I encode it?
Thanks,
internal static Bitmap convertImage(string imageText){
imageText = imageText.Replace("\r\n", String.Empty);
Byte[] bitmapData;
bitmapData = Convert.FromBase64String(imageText);
MemoryStream streamBitmap = new MemoryStream(bitmapData);
try
{
return new Bitmap(streamBitmap);
}
catch (ArgumentException e)
{
throw e;
}
}