473,722 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArgumentExcepti on 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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A
AA AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA
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
ArgumentExcepti on ("Invalid parameters used") is always thrown. I've used
an alternative route i.e "return Image.FromStrea m(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(st ring imageText){

imageText = imageText.Repla ce("\r\n", String.Empty);

Byte[] bitmapData;

bitmapData = Convert.FromBas e64String(image Text);

MemoryStream streamBitmap = new MemoryStream(bi tmapData);

try

{

return new Bitmap(streamBi tmap);

}

catch (ArgumentExcept ion e)

{

throw e;

}

}
Nov 16 '05 #1
8 4563
iyuen <iy***@counstru ction.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
iyuen <iy***@counstru ction.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
iyuen <iy***@counstru ction.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.co m>
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.Leng th];

// Read the bytes.
pobjStream.Read (pbytBytes, 0, pbytBytes.Lengt h);
}

// Create the property bag.
VBRUN._Property Bag pobjPropBag = new VBRUN.PropertyB agClass();

// Set the contents.
pobjPropBag.Con tents = 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.IPicture Disp pobjPicture = (stdole.IPictur eDisp)
pobjPropBag.Rea dProperty("imag e", null);

// Now get the image from the handle.
Image pobjImage = Image.FromHbitm ap((IntPtr) pobjPicture.Han dle, (IntPtr)
pobjPicture.hPa l);

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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A AA AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA 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
ArgumentExcepti on ("Invalid parameters used") is always thrown. I've used
an alternative route i.e "return Image.FromStrea m(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(st ring imageText){

imageText = imageText.Repla ce("\r\n", String.Empty);

Byte[] bitmapData;

bitmapData = Convert.FromBas e64String(image Text);

MemoryStream streamBitmap = new MemoryStream(bi tmapData);

try

{

return new Bitmap(streamBi tmap);

}

catch (ArgumentExcept ion 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.c om> wrote in
message news:OJ******** ******@TK2MSFTN GP11.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.Leng th];

// Read the bytes.
pobjStream.Read (pbytBytes, 0, pbytBytes.Lengt h);
}

// Create the property bag.
VBRUN._Property Bag pobjPropBag = new VBRUN.PropertyB agClass();

// Set the contents.
pobjPropBag.Con tents = 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.IPicture Disp pobjPicture = (stdole.IPictur eDisp)
pobjPropBag.Rea dProperty("imag e", null);

// Now get the image from the handle.
Image pobjImage = Image.FromHbitm ap((IntPtr) pobjPicture.Han dle, (IntPtr)
pobjPicture.hPa l);

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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A
AA

AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA
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
ArgumentExcepti on ("Invalid parameters used") is always thrown. I've used an alternative route i.e "return Image.FromStrea m(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(st ring imageText){

imageText = imageText.Repla ce("\r\n", String.Empty);

Byte[] bitmapData;

bitmapData = Convert.FromBas e64String(image Text);

MemoryStream streamBitmap = new MemoryStream(bi tmapData);

try

{

return new Bitmap(streamBi tmap);

}

catch (ArgumentExcept ion 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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.c om> wrote in message news:OJ******** ******@TK2MSFTN GP11.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.Leng th];

// Read the bytes.
pobjStream.Read (pbytBytes, 0, pbytBytes.Lengt h);
}

// Create the property bag.
VBRUN._Property Bag pobjPropBag = new VBRUN.PropertyB agClass();

// Set the contents.
pobjPropBag.Con tents = 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.IPicture Disp pobjPicture = (stdole.IPictur eDisp)
pobjPropBag.Rea dProperty("imag e", null);

// Now get the image from the handle.
Image pobjImage = Image.FromHbitm ap((IntPtr) pobjPicture.Han dle, (IntPtr) pobjPicture.hPa l);

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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A
AA

AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA
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 ArgumentExcepti on ("Invalid parameters used") is always thrown. I've used an alternative route i.e "return Image.FromStrea m(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(st ring imageText){

imageText = imageText.Repla ce("\r\n", String.Empty);

Byte[] bitmapData;

bitmapData = Convert.FromBas e64String(image Text);

MemoryStream streamBitmap = new MemoryStream(bi tmapData);

try

{

return new Bitmap(streamBi tmap);

}

catch (ArgumentExcept ion e)

{

throw e;

}

}



Nov 16 '05 #7
Hey Nicholas....sin ce you're similar with COM...let me ask some more
questions...

Originally I didn't use the propertybag.Con tent 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 ArgumentExcepti on 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.c om> wrote in
message news:ur******** ******@TK2MSFTN GP09.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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.c om> wrote

in
message news:OJ******** ******@TK2MSFTN GP11.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.Leng th];

// Read the bytes.
pobjStream.Read (pbytBytes, 0, pbytBytes.Lengt h);
}

// Create the property bag.
VBRUN._Property Bag pobjPropBag = new VBRUN.PropertyB agClass();

// Set the contents.
pobjPropBag.Con tents = 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.IPicture Disp pobjPicture = (stdole.IPictur eDisp)
pobjPropBag.Rea dProperty("imag e", null);

// Now get the image from the handle.
Image pobjImage = Image.FromHbitm ap((IntPtr) pobjPicture.Han dle, (IntPtr) pobjPicture.hPa l);

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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A > AA
AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA > 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 > ArgumentExcepti on ("Invalid parameters used") is always thrown.
I've used
> an alternative route i.e "return Image.FromStrea m(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(st ring imageText){
>
> imageText = imageText.Repla ce("\r\n", String.Empty);
>
> Byte[] bitmapData;
>
> bitmapData = Convert.FromBas e64String(image Text);
>
> MemoryStream streamBitmap = new MemoryStream(bi tmapData);
>
> try
>
> {
>
> return new Bitmap(streamBi tmap);
>
> }
>
> catch (ArgumentExcept ion 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 ArgumentExcepti on
("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.c om> wrote in
message news:ur******** ******@TK2MSFTN GP09.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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.c om> wrote

in
message news:OJ******** ******@TK2MSFTN GP11.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.Leng th];

// Read the bytes.
pobjStream.Read (pbytBytes, 0, pbytBytes.Lengt h);
}

// Create the property bag.
VBRUN._Property Bag pobjPropBag = new VBRUN.PropertyB agClass();

// Set the contents.
pobjPropBag.Con tents = 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.IPicture Disp pobjPicture = (stdole.IPictur eDisp)
pobjPropBag.Rea dProperty("imag e", null);

// Now get the image from the handle.
Image pobjImage = Image.FromHbitm ap((IntPtr) pobjPicture.Han dle, (IntPtr) pobjPicture.hPa l);

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.co m

"iyuen" <iy***@counstru ction.ualberta. ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>bHQAAH4AA ABCTX4AAAAAAAAA PgAAACgAAAAQAAA AEAAAAAEAAQAAAA AAQAAAAAAAAAAAA A > AA
AAAAAAAAAAAAAAA A////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAV UAAAFVA > 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 > ArgumentExcepti on ("Invalid parameters used") is always thrown.
I've used
> an alternative route i.e "return Image.FromStrea m(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(st ring imageText){
>
> imageText = imageText.Repla ce("\r\n", String.Empty);
>
> Byte[] bitmapData;
>
> bitmapData = Convert.FromBas e64String(image Text);
>
> MemoryStream streamBitmap = new MemoryStream(bi tmapData);
>
> try
>
> {
>
> return new Bitmap(streamBi tmap);
>
> }
>
> catch (ArgumentExcept ion 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
5148
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 exception, can anyone see what might cause that exception? (By the way, i am trying to throw the header part of the image away and extract only
1
5575
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 From File byte arr;
0
3605
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 into byte from a stream. Exception (System.ArgumentException: Invalid parameter used) occurs when reading from byte over a memory-stream back to the Bitmap object. Please help, I'm really stuck here! Here's the code I use (Sorry for a long...
3
3086
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 displayed. Can anybody know this issue and help me to resolve this. In past i received the response saying that i should download the image first and then parse the actual response and modify the src attribute of the
13
2848
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 System.Drawing.Bitmap("C:\2068.bmp") ms = New MemoryStream bmp.Save(ms, bmp.RawFormat)
1
6136
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 marked line. I read various articles about it but none of them seems to apply as I am not writing to a file or to a response in ASP, instead it's a pure VB class that is used from a winforms applciation. I am using VS 2005. Private Function...
3
3021
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 to show the read image. i have the image available in a datarow which is read from db. As control only has the imageurl property ,i need to save the read image to an image image file and then assign it to the control .
6
2841
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 this. Public Class DesTestForm Private Sub DesTestForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
2
2685
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 image-variable, then using the method of creating thumbnails for the resizing. Next, I write this to a temp file on the disk, and read the file back to a byte array in order to insert/update the SQL table. Now, this works well, and even fast enough, no...
0
8860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9382
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.