RE: ArgumentException received when converting a byte array to bitmap

From: mattaku (mattaku_at_discussions.microsoft.com)
Date: 08/17/04


Date: Tue, 17 Aug 2004 14:07:03 -0700

Hah! I just had this problem and posted something on it. I read on another
thread that rotating an image helped them with a similar, but different bug.
Here is a snippet of my code that now works:

byte[] byteArray = Convert.FromBase64String( base64String );
MemoryStream memStrm = new MemoryStream( byteArray, 0, byteArray.Length );
retObj = Image.FromStream( memStrm );
                                                                        
// these next two lines is to fix a weird C#/.Net bug
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );

memStrm.Close();

Why it works, I don't know. I think it is a .Net bug myself. Try it and
see. Hope this solution isn't too late for your problem.

"iyuen" wrote:

> 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;
>
> }
>
> }
>
>
>



Relevant Pages

  • Re: ArgumentException thrown when converting a memorystream to image
    ... the following code will read the contents from a> file and place them into a byte array. ... >> 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • ArgumentException thrown when converting a memorystream to image
    ... My byte array is an picture in VB6 StdPicture format. ... the array as some child element in an xml file, ... try to convert it into an Image object or Bitmap in the following code. ...
    (microsoft.public.dotnet.languages.csharp)
  • ArgumentException received when converting a byte array to bitmap
    ... My byte array is an picture in VB6 StdPicture format. ... the array as some child element in an xml file, ... try to convert it into an Image object or Bitmap in the following code. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: ArgumentException thrown when converting a memorystream to image
    ... PropertyBag, and then taking the Contents property (a byte array), and then ... > 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pset and SetPixel Bug
    ... The method varies slightly depending on what kind of bitmap you are dealing with and also where you are getting it from. ... Once you know the address you can then point a SAFEARRAY structure at that data and access it from the array. ... Private Declare Function timeBeginPeriod _ ... Dim SourceWidth As Long, SourceHeight As Long ...
    (microsoft.public.vb.general.discussion)