Re: ArgumentException thrown when converting a memorystream to image

From: iyuen (iyuen_at_counstruction.ualberta.ca)
Date: 06/16/04


Date: Wed, 16 Jun 2004 11:06:18 -0600

Thanks Nicholas. I've been avoiding to use com but i guess it's inevitable.

Do you know why my code doesn't work?

-- 
-----------------------------------------------------------------
Isaac Yuen
CEM
University of Alberta
iyuen@ualberta.construction.ca
-----------------------------------------------------------------
| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:OJDJ347UEHA.2816@TK2MSFTNGP11.phx.gbl...
> iyuen,
>
>     From what I can tell, you are storing the contents of the image in a
> PropertyBag, and then taking the Contents property (a byte array), and
then
> storing that in your file somewhere (or a text encoded version of it).
>
>     I've been able to figure out how to do this (it took me a while, but
> it's possible).
>
>     First, add a reference to msvbvm60.dll.  If in VS.NET, go to the
> references and add it.  Do NOT add it from the list that is given to you
on
> the "COM" tab.  Rather, go to the "COM" tab and browse for the file.  This
> will cause all of the VB objects to be included in the interop dll.
>
>     Once you have that, the following code will read the contents from a
> file and place them into a byte array.  This byte array will be what was
> originally returned to you through the Contents property on the
PropertyBag:
>
> // The bytes.
> byte[] pbytBytes;
>
> // Read the contents from the file.
> using (FileStream pobjStream = new FileStream(@"c:\temp\pic2.txt",
> FileMode.Open, FileAccess.Read))
> {
>     // Get the bytes.
>     pbytBytes = new byte[pobjStream.Length];
>
>     // Read the bytes.
>     pobjStream.Read(pbytBytes, 0, pbytBytes.Length);
> }
>
> // Create the property bag.
> VBRUN._PropertyBag pobjPropBag = new VBRUN.PropertyBagClass();
>
> // Set the contents.
> pobjPropBag.Contents = pbytBytes;
>
>     At this point, you need to change it to an Image instance.  You can
get
> the IPictureDisp interface implementation, and then get the image from
> there:
>
> // Get the item now. This is an IPictDisp.
> stdole.IPictureDisp pobjPicture = (stdole.IPictureDisp)
> pobjPropBag.ReadProperty("image", null);
>
> // Now get the image from the handle.
> Image pobjImage = Image.FromHbitmap((IntPtr) pobjPicture.Handle, (IntPtr)
> pobjPicture.hPal);
>
>     And that should be it.  Of course, you should do some cleanup (release
> the property bag instance, and the picture instance).  Other than that,
you
> should be fine.
>
>     Hope this helps
>
>
> -- 
>                - Nicholas Paldino [.NET/C# MVP]
>                - mvp@spam.guard.caspershouse.com
>
> "iyuen" <iyuen@counstruction.ualberta.ca> wrote in message
> news:%23eZgv66UEHA.1888@TK2MSFTNGP11.phx.gbl...
> > 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