ArgumentException thrown when converting a memorystream to image

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;

}

}
 
J

Jon Skeet [C# MVP]

iyuen said:
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.:

<snip>

What is "StdPicture format"? It's not a file format I'm familiar with -
and it may not be a format the Bitmap class is familiar with either.
Does your code work with a normal JPEG or GIF converted to Base64 and
put into XML?
 
I

iyuen

StdPicture is an VB6 Object that stores bitmap. It's assumed that some
bitmaps are StdPicture object. In fac my task will be....to convert some
code from VB6 to .NET.

In VB6 code, the "picture" are loaded into StdPicture object. My task is to
convert them into Image object in .NET.

--
-----------------------------------------------------------------
Isaac Yuen
CEM
University of Alberta
(e-mail address removed)
 
J

Jon Skeet [C# MVP]

iyuen said:
StdPicture is an VB6 Object that stores bitmap. It's assumed that some
bitmaps are StdPicture object. In fac my task will be....to convert some
code from VB6 to .NET.

In VB6 code, the "picture" are loaded into StdPicture object. My task is to
convert them into Image object in .NET.

Well, there are two approaches here:

1) Try your XML/Base64 code with a JPEG
2) Try loading a StdPicture bitmap directly into a Bitmap, without
using XML

My guess is that the first will work and the second won't - in which
case, you'll have to convert all your StdPicture format pictures into a
more standardised format.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

iyuen said:
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.:

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;

}

}
 
I

iyuen

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
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
Nicholas Paldino said:
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]
- (e-mail address removed)

iyuen said:
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.:

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;

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

iyuen,

It doesn't work because you are storing a property bag (which contains a
bitmap) and not the actual bitmap. You need to reconstitute the property
bag in order to get at the bitmap.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

iyuen said:
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
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
message news:[email protected]...
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]
- (e-mail address removed)

iyuen said:
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.:
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;

}

}
 
I

iyuen

Hey Nicholas....since you're similar with COM...let me ask some more
questions...

Originally I didn't use the propertybag.Content to retrieve the byte array.
I've found some nifty code from a website that allows me to do the same
thing.

Here is the source:
http://www.mvps.org/emorcillo/vb6/multimedia/arrayfrompicture.shtml

The output from the code is the same as the byte array i generated using
property bag. Mind you though....I got the exact ArgumentException when I
do the conversion in .NET.


--
-----------------------------------------------------------------
Isaac Yuen
CEM
University of Alberta
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
Nicholas Paldino said:
iyuen,

It doesn't work because you are storing a property bag (which contains a
bitmap) and not the actual bitmap. You need to reconstitute the property
bag in order to get at the bitmap.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

iyuen said:
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
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
message news:[email protected]...
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]
- (e-mail address removed)

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.:
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 I've
used
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;

}

}
 
I

iyuen

Hey Nick....
i've used SavePicture() method from this site
http://www.mvps.org/emorcillo/vb6/multimedia/arrayfrompicture.shtml to
convert the StdPicture too. Same result....I get an ArgumentException
("Invalid parameter used").

Do you have any clue?

--
-----------------------------------------------------------------
Isaac Yuen
CEM
University of Alberta
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
Nicholas Paldino said:
iyuen,

It doesn't work because you are storing a property bag (which contains a
bitmap) and not the actual bitmap. You need to reconstitute the property
bag in order to get at the bitmap.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

iyuen said:
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
(e-mail address removed)
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
message news:[email protected]...
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]
- (e-mail address removed)

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.:
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 I've
used
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;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top