473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ArgumentException thrown when converting a memorystream to image

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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA
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;

}

}
Nov 16 '05 #1
8 4522
iyuen <iy***@counstruction.ualberta.ca> 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.:


<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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
iyuen <iy***@counstruction.ualberta.ca> 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.:


<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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
iyuen <iy***@counstruction.ualberta.ca> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2****************@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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA 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;

}

}

Nov 16 '05 #5
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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OJ**************@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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2****************@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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA
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;

}

}


Nov 16 '05 #6
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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:OJ**************@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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2****************@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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA
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;

}

}



Nov 16 '05 #7
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/mu...mpicture.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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ur**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:OJ**************@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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2****************@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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA > 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;
>
> }
>
> }
>
>



Nov 16 '05 #8
Hey Nick....
i've used SavePicture() method from this site
http://www.mvps.org/emorcillo/vb6/mu...mpicture.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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ur**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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
iy***@ualberta.construction.ca
-----------------------------------------------------------------

| University of Alberta
| Phone: (780) 492-2276
| Fax: (780) 492-0249
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:OJ**************@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]
- mv*@spam.guard.caspershouse.com

"iyuen" <iy***@counstruction.ualberta.ca> wrote in message
news:%2****************@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>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAA AAEAAQAAAAAAQAAAAAAAAAAAAA > 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;
>
> }
>
> }
>
>



Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: halise irak via .NET 247 | last post by:
I get an "ArgumentException: Invalid parameter used at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)" exception. it is too ridicilious to get such an...
1
by: Arcnet | last post by:
Using MemoryStream I Have a problem to create a new Image from byte array that originaly was created from an older image (Everything is being preformed in the same Thread) //Get Image...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
13
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
1
by: Martin Widmer | last post by:
Hi Folks In my object I am trying to implement a function in order to render the picture to XML (to generate RDL for SQL Server). That Function causes the error mentioned in the subject at the...
3
by: Raghu Raman | last post by:
Hi i want to save the read the image file and show it on a image control of a mobile webform .But the mobile control does not support html images ,so i am forced to use the server image control...
6
by: =?Utf-8?B?QkJN?= | last post by:
Hi, I have an app that is crashing due to a System.ArgumentException. At this point it's just a simple app to test some basic object values. The main app is a Windows App that looks like...
2
by: Bjorn Sagbakken | last post by:
Hi. This story is about uploading jpg's, then resize them to fixed width or height and storing them to an SQL table. The only way I have found so far is to read the uploaded file to an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.