473,498 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object to binary[] from COM in .NET using C#

Greetings:
It turned out that using COM objects from .NET C# isn't straight forward at
all.
Now, I have successfully used reflection to instantiate the object, and
invoked the methods and properties through Type.InvokeMember, but...

One property whose value I retrieve through :

TImagingObject.InvokeMember("JPGData", BindingFlags.GetProperty, null,
certificateImage, null);

returns an Object.

Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write?!
I can't cast into an array, of course, and I can't find the right methods to
access the binary data and instantiate an array with it.

Sincerely,
A. Gharbeia

Nov 16 '05 #1
15 4374
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType().ToString())?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Thank you for your response, Mattias.

Using .GetType().ToString() directly on the reference returned by System.Type.InvokeMember() (i.e. without first assigning it to an object), returns System.Byte[*].

However, passing this reference also directly to "Response.BinaryWrite()" (i.e. by placing the whole InvokeMember call within the parentheses), yields:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.HttpResponse.BinaryWrite(byte[])' has some invalid arguments

I also tried assigning the reference to an Object object and using System.Convert.ChangeType(object, invocation.GetType()) to convert object to the type returned by the invocation and do InvokeMember() once more, this time assigning the returned reference to object. And I got the same error!!
"Mattias Sjögren" <ma********************@mvps.org> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType().ToString())?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
One more thing:
Doing the whole thing in JScript.NET is much easier and straighforward,
since calling the COM methods doesn't neccessate using Type.InvokeMember.
However, attempting to output the result to the HTTP stream using
Response.BinaryWrite yields a similar error:

System.InvalidCastException: Specified cast is not valid.

A. Gharbeia

"A. Gharbeia" <ah***@heia.org> wrote in message
news:uC**************@tk2msftngp13.phx.gbl...
Thank you for your response, Mattias.

Using .GetType().ToString() directly on the reference returned by
System.Type.InvokeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].

However, passing this reference also directly to "Response.BinaryWrite()"
(i.e. by placing the whole InvokeMember call within the parentheses),
yields:

Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.HttpResponse.BinaryWrite(byte[])' has some invalid arguments

I also tried assigning the reference to an Object object and using
System.Convert.ChangeType(object, invocation.GetType()) to convert object to
the type returned by the invocation and do InvokeMember() once more, this
time assigning the returned reference to object. And I got the same error!!
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType().ToString())?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #4
Using .GetType().ToString() directly on the reference returned by System.Type.InvokeMember() (i.e. without first assigning it to an object), returns System.Byte[*].


In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
Using .GetType().ToString() directly on the reference returned by
System.Type.InvokeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].
In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #6
I tried:

Object o = ImagingComponent.InvokeMember("JPGData",
BindingFlags.GetProperty, null, certificateImage, null);
Array b = Array.CreateInstance(typeof(byte), ((Array)o).LongLength);
b.CopyTo(b, 0);
Response.BinaryWrite(b);

and got the same compiler error:

CS1502: The best overloaded method match for
'System.Web.HttpResponse.BinaryWrite(byte[])' has some invalid arguments
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
Using .GetType().ToString() directly on the reference returned by
System.Type.InvokeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].
In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #7
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCopy().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #8
Although I don't know how to create a byte[] with a dynamic size (that will
only be known at runtime), I tried:

Object r = ImaginCOM.InvokeMember("JPGData", BindingFlags.GetProperty,
null, certificateImage, null);
byte[] b;
Buffer.BlockCopy( ((Array)r), 0, b, 0, ((Array)r).Length );

and got:

CS0154: The property or indexer 'System.Web.UI.Page.Buffer' cannot be used
in this context because it lacks the get accessor

I'm trying this and other tricks with JScript.NET in addition to C#. I with
I could find a solution in JScript.NET, as the code there is much cleaner. I
tried to play on the JScript Array/.NET System.Array duality but reached no
where either.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eM**************@TK2MSFTNGP09.phx.gbl...
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCopy().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #9
Although I don't know how to create a byte[] with a dynamic size (that will
only be known at runtime), I tried:

Object r = ImaginCOM.InvokeMember("JPGData", BindingFlags.GetProperty,
null, certificateImage, null);
byte[] b;
Buffer.BlockCopy( ((Array)r), 0, b, 0, ((Array)r).Length );

and got:

CS0154: The property or indexer 'System.Web.UI.Page.Buffer' cannot be used
in this context because it lacks the get accessor

I'm trying this and other tricks with JScript.NET in addition to C#. I with
I could find a solution in JScript.NET, as the code there is much cleaner. I
tried to play on the JScript Array/.NET System.Array duality but reached no
where either.

What do you think?

A. Gharbeia

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eM**************@TK2MSFTNGP09.phx.gbl...
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCopy().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #10
CS0154: The property or indexer 'System.Web.UI.Page.Buffer' cannot be used
in this context because it lacks the get accessor


The code looks correct, you just got the wrong Buffer class. Make it

System.Buffer.BlockCopy( ...

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #11
that same code from the past post with System.Buffer results in:

CS0165: Use of unassigned local variable 'b'

From what I understand, arrays in C# aren't dynamic and have to be
dimensioned and initialised before use.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
CS0154: The property or indexer 'System.Web.UI.Page.Buffer' cannot be usedin this context because it lacks the get accessor


The code looks correct, you just got the wrong Buffer class. Make it

System.Buffer.BlockCopy( ...

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 16 '05 #12
that same code from the past post with System.Buffer results in:

CS0165: Use of unassigned local variable 'b'


Well you have to instantiate the byte array first.

byte[] b = new byte[((Array)r).Length];

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #13
Thank you very much Mattias.
That did it.
I thought I tried that before, but it seems I did something wrong.

I hope you don't mind me asking another question: Is the reflection part
required in ASP.NET C#, or is there a way to add a reference to the COM
object so I can do away with the extra code.
Ahmad Gharbeia
Nov 16 '05 #14
Question for A. Gharbeia: do you always know what sort of object you are getting back from your InvokeMember call
For instance, why not cast your original example to an Image instance

Image tImage = (Image) TImagingObject.InvokeMember("JPGData", BindingFlags.GetProperty, null, certificateImage, null)

2 of the overloaded Image.Save() methods allow you to perist the image as a Stream

Stream tImageByteStream = new Stream()
tImage.Save(tImageByteStream, ImagingFormat.Jpeg)

and so on and so forth..

Nov 16 '05 #15
Carter:
No, I catch the return of the JPEGData property in an Object type variable.

but now I tried:

System.Drawing.Image i = (System.Drawing.Image)
TImagingComponent.InvokeMember("JPGData", BindingFlags.GetProperty, null,
certificateImage, null);
Response.ContentType = "image/jpeg";
i.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

and got:

System.InvalidCastException: Specified cast is not valid.
I would have used the native .NET imaging classes, except that I need to
embed XMP metadata in the output. I will enhirt System.Drawing.Bitmap class
to include this functionality, but untill then, I'm stuck with the COM
component that I have.
The code would have been much prettier I didn't have to use reflection in
C#, though. I have to find out how to add a reference to the COMponent's
type library or something.

Thank you.
Ahmad
Nov 16 '05 #16

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

Similar topics

5
10123
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
1
3952
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
5
7555
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
5
5656
by: Samuel R. Neff | last post by:
Are there any tools available to view a file containing a binary serialized object in a friendly format? Something to list classes and data and such? Thanks, Sam B-Line is now hiring one...
7
9534
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects...
7
11917
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
3
17128
by: nigel.thomson | last post by:
Hello All Is there an easy way to do this? I have a database that contains records witha image as one of the fields, what I want to do is export the images to a seperate folder, in whatever...
8
8910
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
2
1668
by: mpreisdorf | last post by:
I want to save the raw data of a class (without the .NET object overhead) to a binary file. For example: ref class Test { public: String^ name; Int32 number; ..... }
0
4056
by: phoenix7 | last post by:
Dear all, I want to store some data in form of a zip file into an access database. I created a table with with a column of type OLE Object, then I designed a form to insert data to the table....
1
6885
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
7379
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
5462
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,...
1
4908
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...
0
4588
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...
0
3093
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...
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.