Discussion:
[Python.NET] Problem with System.ArgumentException in call to method from dll
Daniel Krause
2013-01-22 19:58:05 UTC
Permalink
I want to use a method from an API to control a camera (xiApi.NETX64.dll).

The method is described in two ways:
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap
object. Supports UNSAFE buffer policy mode.
Parameters :
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap
object. Supports SAFE buffer policy mode.
Parameters :
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
//

The code in a c#-sample looks like this (I skipped the initialisation of
myCam, but if it helps I can provide the complete code):
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.

I tried to keep the python code as close as possible, but I get errors I do
not understand:
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##

Console output:
<class 'System.Windows.Media.Imaging.BitmapSource'>
Traceback (most recent call last):
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType" kann
nicht
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder,
CultureInfo
culture, Boolean needsSpecialCast)

bei System.Reflection.MethodBase.CheckArguments(Object[] parameters,
Binder b
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)

bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,
Bind
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)

bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invok
eAttr, Binder binder, Object[] parameters, CultureInfo culture)

bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr
kw, M
ethodBase info, MethodInfo[] methodinfo)
b***@public.gmane.org
2013-01-22 20:10:07 UTC
Permalink
You are setting the bitmapsrc variable to be equal to the class BitmapSource. I assume you mean to get the value of an output parameter, rather than pass a class object into the method.

Though I do not speak german. But I think that's it.
Post by Daniel Krause
I want to use a method from an API to control a camera (xiApi.NETX64.dll).
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports UNSAFE buffer policy mode.
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports SAFE buffer policy mode.
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType" kann nicht
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder b
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, Bind
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invok
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, M
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
Daniel Krause
2013-01-22 21:10:37 UTC
Permalink
That is right, I want to get the value of the output parameter.

The following variant looks much more logical to me, but than I get another
error:
bitmapsrc = cam.GetImage(timeout)
TypeError: No method matches given arguments

So it seems that I have to pass the "out"-parameter to the method as well,
but that was not working either.
Post by b***@public.gmane.org
You are setting the bitmapsrc variable to be equal to the class
BitmapSource. I assume you mean to get the value of an output parameter,
rather than pass a class object into the method.
Though I do not speak german. But I think that's it.
Post by Daniel Krause
I want to use a method from an API to control a camera
(xiApi.NETX64.dll).
Post by Daniel Krause
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports UNSAFE buffer policy mode.
Post by Daniel Krause
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports SAFE buffer policy mode.
Post by Daniel Krause
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
The code in a c#-sample looks like this (I skipped the initialisation of
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
I tried to keep the python code as close as possible, but I get errors I
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
Post by Daniel Krause
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType"
kann nicht
Post by Daniel Krause
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder,
CultureInfo
Post by Daniel Krause
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters,
Binder b
Post by Daniel Krause
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object
obj, Bind
Post by Daniel Krause
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)
Post by Daniel Krause
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invok
Post by Daniel Krause
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args,
IntPtr kw, M
Post by Daniel Krause
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Tribble, Brett
2013-01-22 22:51:07 UTC
Permalink
Did you try:

from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource() # <- set to an instance of type BitmapSource, not the class.
cam.GetImage(bitmapsrc, timeout)

If you need to pass it a variable that is of type BitmapSource, but which is set to null, then you might be able to use System.Reflection to do this, but I haven't tried...

From: PythonDotNet [mailto:pythondotnet-bounces+btribble=ea.com-+ZN9ApsXKcFQFI55V6+***@public.gmane.orgg] On Behalf Of Daniel Krause
Sent: Tuesday, January 22, 2013 1:11 PM
To: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: Re: [Python.NET] Problem with System.ArgumentException in call to method from dll

That is right, I want to get the value of the output parameter.

The following variant looks much more logical to me, but than I get another error:
bitmapsrc = cam.GetImage(timeout)
TypeError: No method matches given arguments

So it seems that I have to pass the "out"-parameter to the method as well, but that was not working either.


2013/1/22 brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org> <brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org>>
You are setting the bitmapsrc variable to be equal to the class BitmapSource. I assume you mean to get the value of an output parameter, rather than pass a class object into the method.

Though I do not speak german. But I think that's it.
Post by Daniel Krause
I want to use a method from an API to control a camera (xiApi.NETX64.dll).
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports UNSAFE buffer policy mode.
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports SAFE buffer policy mode.
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType" kann nicht
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder b
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, Bind
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invok
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, M
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-01-23 01:00:30 UTC
Permalink
Just to clarify: neither version of the methods you are calling are marked "unsafe" in the proper c# .net manner are they? I would not be surprised to find PythonNet not providing access to "unsafe" methods. I would need to take a trip through the source to verify.
Post by Daniel Krause
That is right, I want to get the value of the output parameter.
bitmapsrc = cam.GetImage(timeout)
TypeError: No method matches given arguments
So it seems that I have to pass the "out"-parameter to the method as well, but that was not working either.
You are setting the bitmapsrc variable to be equal to the class BitmapSource. I assume you mean to get the value of an output parameter, rather than pass a class object into the method.
Though I do not speak german. But I think that's it.
Post by Daniel Krause
I want to use a method from an API to control a camera (xiApi.NETX64.dll).
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports UNSAFE buffer policy mode.
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports SAFE buffer policy mode.
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType" kann nicht
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder b
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, Bind
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invok
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, M
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Daniel Krause
2013-01-23 06:08:09 UTC
Permalink
@Brett

When I try

bitmapsrc = BitmapSource()

I get the following error:

Traceback (most recent call last):
File "C:\Users\mdk\workspace\\camera\testbitmap.py", line 8, in <module>
bitmapsrc = BitmapSource()
TypeError: cannot instantiate abstract class

I will have a look at System.Reflection, thanks for the hint.

@Brad
The documentation does not say anything about "safe" or "unsafe". I do not
have the source for the dll, only for the examples shipped with the dll. If
you would like to have a look at them, I could send them to you.


Daniel
Post by b***@public.gmane.org
Just to clarify: neither version of the methods you are calling are marked
"unsafe" in the proper c# .net manner are they? I would not be surprised
to find PythonNet not providing access to "unsafe" methods. I would need
to take a trip through the source to verify.
That is right, I want to get the value of the output parameter.
bitmapsrc = cam.GetImage(timeout)
TypeError: No method matches given arguments
So it seems that I have to pass the "out"-parameter to the method as well,
but that was not working either.
Post by b***@public.gmane.org
You are setting the bitmapsrc variable to be equal to the class
BitmapSource. I assume you mean to get the value of an output parameter,
rather than pass a class object into the method.
Though I do not speak german. But I think that's it.
On Jan 22, 2013, at 2:58 PM, Daniel Krause <
Post by Daniel Krause
I want to use a method from an API to control a camera
(xiApi.NETX64.dll).
Post by Daniel Krause
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports UNSAFE buffer policy mode.
Post by Daniel Krause
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports SAFE buffer policy mode.
Post by Daniel Krause
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
The code in a c#-sample looks like this (I skipped the initialisation
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
I tried to keep the python code as close as possible, but I get errors
##
import clr
import sys
\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
Post by Daniel Krause
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType"
kann nicht
Post by Daniel Krause
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder,
CultureInfo
Post by Daniel Krause
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters,
Binder b
Post by Daniel Krause
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object
obj, Bind
Post by Daniel Krause
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)
Post by Daniel Krause
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invok
Post by Daniel Krause
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args,
IntPtr kw, M
Post by Daniel Krause
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Tribble, Brett
2013-01-23 18:51:48 UTC
Permalink
My personal solution would be to wrap the camera code in a very thin assembly that manages this stuff and provides a clean interface for python.net to hook into. I'm lazy that way though...

From: PythonDotNet [mailto:pythondotnet-bounces+btribble=ea.com-+ZN9ApsXKcFQFI55V6+***@public.gmane.orgg] On Behalf Of Daniel Krause
Sent: Tuesday, January 22, 2013 10:08 PM
To: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: Re: [Python.NET] Problem with System.ArgumentException in call to method from dll

@Brett

When I try

bitmapsrc = BitmapSource()

I get the following error:

Traceback (most recent call last):
File "C:\Users\mdk\workspace\\camera\testbitmap.py", line 8, in <module>
bitmapsrc = BitmapSource()
TypeError: cannot instantiate abstract class

I will have a look at System.Reflection, thanks for the hint.

@Brad
The documentation does not say anything about "safe" or "unsafe". I do not have the source for the dll, only for the examples shipped with the dll. If you would like to have a look at them, I could send them to you.


Daniel

2013/1/23 brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org> <brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org>>
Just to clarify: neither version of the methods you are calling are marked "unsafe" in the proper c# .net manner are they? I would not be surprised to find PythonNet not providing access to "unsafe" methods. I would need to take a trip through the source to verify.

On Jan 22, 2013, at 4:10 PM, Daniel Krause <m.daniel.krause-gM/Ye1E23mwN+***@public.gmane.org<mailto:m.daniel.krause-gM/Ye1E23mwN+***@public.gmane.org>> wrote:


That is right, I want to get the value of the output parameter.

The following variant looks much more logical to me, but than I get another error:
bitmapsrc = cam.GetImage(timeout)
TypeError: No method matches given arguments

So it seems that I have to pass the "out"-parameter to the method as well, but that was not working either.


2013/1/22 brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org> <brad-DLbl0k+***@public.gmane.org<mailto:brad-DLbl0k+***@public.gmane.org>>
You are setting the bitmapsrc variable to be equal to the class BitmapSource. I assume you mean to get the value of an output parameter, rather than pass a class object into the method.

Though I do not speak german. But I think that's it.
Post by Daniel Krause
I want to use a method from an API to control a camera (xiApi.NETX64.dll).
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports UNSAFE buffer policy mode.
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills WritableBitmap object. Supports SAFE buffer policy mode.
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in milliseconds).
//
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET<http://xiApi.NET> import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType" kann nicht
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder b
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, Bind
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invok
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, M
ethodBase info, MethodInfo[] methodinfo)
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET<http://Python.NET> mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org>
http://mail.python.org/mailman/listinfo/pythondotnet
Daniel Krause
2013-01-23 20:07:29 UTC
Permalink
I could solve the problem using a subclass of BitmapSource: BitmapImage has
a constructor.

from System.Windows.Media.Imaging import BitmapImage

bitmapsrc = BitmapImage()
...
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
Post by Tribble, Brett
My personal solution would be to wrap the camera code in a very thin
assembly that manages this stuff and provides a clean interface for
python.net to hook into. I’m lazy that way though…****
** **
*From:* PythonDotNet [mailto:pythondotnet-bounces+btribble=
*Sent:* Tuesday, January 22, 2013 10:08 PM
*Subject:* Re: [Python.NET] Problem with System.ArgumentException in call
to method from dll****
** **
@Brett****
** **
When I try****
** **
bitmapsrc = BitmapSource()****
** **
I get the following error:****
** **
Traceback (most recent call last):****
File "C:\Users\mdk\workspace\\camera\testbitmap.py", line 8, in <module>
****
bitmapsrc = BitmapSource()****
TypeError: cannot instantiate abstract class****
** **
I will have a look at System.Reflection, thanks for the hint.****
** **
@Brad****
The documentation does not say anything about "safe" or "unsafe". I do not
have the source for the dll, only for the examples shipped with the dll. If
you would like to have a look at them, I could send them to you.****
** **
** **
Daniel****
** **
Just to clarify: neither version of the methods you are calling are marked
"unsafe" in the proper c# .net manner are they? I would not be surprised
to find PythonNet not providing access to "unsafe" methods. I would need
to take a trip through the source to verify. ****
** **
wrote:****
****
That is right, I want to get the value of the output parameter.****
** **
The following variant looks much more logical to me, but than I get
another error:****
bitmapsrc = cam.GetImage(timeout)****
TypeError: No method matches given arguments****
** **
So it seems that I have to pass the "out"-parameter to the method as well,
but that was not working either.****
** **
** **
You are setting the bitmapsrc variable to be equal to the class
BitmapSource. I assume you mean to get the value of an output parameter,
rather than pass a class object into the method.
Though I do not speak german. But I think that's it.****
Post by Daniel Krause
I want to use a method from an API to control a camera
(xiApi.NETX64.dll).
Post by Daniel Krause
//
void GetImage( out WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports UNSAFE buffer policy mode.
Post by Daniel Krause
out WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
void GetImage(WriteableBitmap image, int timeout)
Description : This function acquires image and returns fills
WritableBitmap object. Supports SAFE buffer policy mode.
Post by Daniel Krause
WriteableBitmap image : WPF BitmapSource to be filled.
int timeout : Time interval required to wait for the image (in
milliseconds).
Post by Daniel Krause
//
The code in a c#-sample looks like this (I skipped the initialisation of
//
using System.Windows.Media.Imaging;
int timeout = 10000;
BitmapSource myBitmapSrc;
myCam.GetImage(out myBitmapSrc, timeout);
//
This code I can compile, and it is working.
I tried to keep the python code as close as possible, but I get errors I
##
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
Post by Daniel Krause
clr.AddReference("PresentationCore")
clr.AddReference("xiAPI.NETX64")
from xiApi.NET import *
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
File "C:\Users\mdk\workspace\camera\testbitmap.py",
line 17, in <module>
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
System.ArgumentException: Das Objekt mit dem Typ "System.RuntimeType"
kann nicht
Post by Daniel Krause
in den Typ "System.Drawing.Bitmap&" konvertiert werden.
bei System.RuntimeType.TryChangeType(Object value, Binder binder,
CultureInfo
Post by Daniel Krause
culture, Boolean needsSpecialCast)
bei System.Reflection.MethodBase.CheckArguments(Object[] parameters,
Binder b
Post by Daniel Krause
inder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object
obj, Bind
Post by Daniel Krause
ingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)
Post by Daniel Krause
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invok
Post by Daniel Krause
eAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args,
IntPtr kw, M
Post by Daniel Krause
ethodBase info, MethodInfo[] methodinfo)****
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet****
** **
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet****
** **
** **
Barton
2013-01-24 10:44:59 UTC
Permalink
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
Barton
2013-01-23 10:52:39 UTC
Permalink
The Python.Runtime is a bit tricky when it comes to out parameters:
In C# DateTime.TryParse(String, out DateTime) becomes
Post by Daniel Krause
d = DateTime(0) # just a dummy to call the method on
d2 = DateTime(0) # another dummy to satisfy the out parameter
(could be the same instance, d)
# d3 is were the result is passed out
Post by Daniel Krause
result, d3 = d.TryParse("2013/01/22", d2)
d3.ToString()
u'1/22/2013 12:00:00 AM'
Post by Daniel Krause
# this is the same behavior as iPy
I can't test this - I'm on Linux, but:
Here you've given the type (class)
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##

Console output:
<class 'System.Windows.Media.Imaging.BitmapSource'>

What you need is an instance, perhaps:
bitmapsrc = BitmapSource() # or something to that effect.
Post by Daniel Krause
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-01-23 14:23:00 UTC
Permalink
Out of curiosity Barton, could you pass "None" rather than constructing instances? Or does it truly need the argument to have a type in order to disambiguate overloaded methods?

Also, it is my understanding that in iPy, out parameters are often omitted from the method's arguments. But in Python.Net they remain in the header. I assume this is because removing the out parameters potentially creates a lot of ambiguity between overloads?

I'm wondering if it might be appropriate for PythonNet to define a type to make it possible to remove ambiguity.

i.e.:
c#:
public bool DoJob(int data1, int data2, out string result) { …}

python:
doer = Doer()
doResult = doer.DoJob(1,2,clr.OutParam(string))
if (doResult[0]):
print(doResult[1])
else:
print("error")

The key being: python net will never ever never let an OutParam object through to the clr. Therefore, the following should throw an exception.

clr.OutParam(clr.OutParam)

Therefore, you would never run into reflective ambiguity.
Post by Barton
In C# DateTime.TryParse(String, out DateTime) becomes
Post by Daniel Krause
d = DateTime(0) # just a dummy to call the method on
d2 = DateTime(0) # another dummy to satisfy the out parameter (could be the same instance, d)
# d3 is were the result is passed out
Post by Daniel Krause
result, d3 = d.TryParse("2013/01/22", d2)
d3.ToString()
u'1/22/2013 12:00:00 AM'
Post by Daniel Krause
# this is the same behavior as iPy
Here you've given the type (class)
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
bitmapsrc = BitmapSource() # or something to that effect.
Post by Daniel Krause
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
Barton
2013-01-24 10:52:52 UTC
Permalink
Two things spring instantly to mind (thanks for the reminder)
1) A patch has been submitted that tries very cleverly to allow out
params to be omitted. This sounds like it's worth pursuing.
2) iPy's Reference type is something that I looked into implementing
which would (or could) allow the wrapper to function more like (or just
the same as) the C# being wrapped, writing the value directly on the
given out param.

Oh, boy; I'm starting to get excited by the possibilities.
Thanks all,
Barton
Post by b***@public.gmane.org
Out of curiosity Barton, could you pass "None" rather than constructing instances? Or does it truly need the argument to have a type in order to disambiguate overloaded methods?
Also, it is my understanding that in iPy, out parameters are often omitted from the method's arguments. But in Python.Net they remain in the header. I assume this is because removing the out parameters potentially creates a lot of ambiguity between overloads?
I'm wondering if it might be appropriate for PythonNet to define a type to make it possible to remove ambiguity.
public bool DoJob(int data1, int data2, out string result) { …}
doer = Doer()
doResult = doer.DoJob(1,2,clr.OutParam(string))
print(doResult[1])
print("error")
The key being: python net will never ever never let an OutParam object through to the clr. Therefore, the following should throw an exception.
clr.OutParam(clr.OutParam)
Therefore, you would never run into reflective ambiguity.
Post by Barton
In C# DateTime.TryParse(String, out DateTime) becomes
Post by Daniel Krause
d = DateTime(0) # just a dummy to call the method on
d2 = DateTime(0) # another dummy to satisfy the out parameter (could be the same instance, d)
# d3 is were the result is passed out
Post by Daniel Krause
result, d3 = d.TryParse("2013/01/22", d2)
d3.ToString()
u'1/22/2013 12:00:00 AM'
Post by Daniel Krause
# this is the same behavior as iPy
Here you've given the type (class)
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
bitmapsrc = BitmapSource() # or something to that effect.
Post by Daniel Krause
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.BitmapSource'>
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet

Daniel Krause
2013-01-23 18:07:48 UTC
Permalink
I tried this approach:

class PyBitmapSource(BitmapSource):
pass
bitmapsrc = PyBitmapSource()
print bitmapsrc

The console output does not really change:
TypeError: cannot instantiate abstract class
Post by Barton
In C# DateTime.TryParse(String, out DateTime) becomes
Post by Daniel Krause
d = DateTime(0) # just a dummy to call the method on
d2 = DateTime(0) # another dummy to satisfy the out parameter (could
be the same instance, d)
# d3 is were the result is passed out
Post by Daniel Krause
result, d3 = d.TryParse("2013/01/22", d2)
d3.ToString()
u'1/22/2013 12:00:00 AM'
Post by Daniel Krause
# this is the same behavior as iPy
Here you've given the type (class)
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_**POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_**FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.**BitmapSource'>
bitmapsrc = BitmapSource() # or something to that effect.
Post by Daniel Krause
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_**POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_**FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.**BitmapSource'>
Jeffrey Bush
2013-01-23 20:08:47 UTC
Permalink
As previously said you have to probably do something like:

from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = None
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_**POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_**FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
cam.GetImage.Overloads[BitmapSource, int](bitmapsrc, timeout)
cam.StopAcquisition()


On Wed, Jan 23, 2013 at 10:07 AM, Daniel Krause <
Post by Daniel Krause
pass
bitmapsrc = PyBitmapSource()
print bitmapsrc
TypeError: cannot instantiate abstract class
Post by Barton
In C# DateTime.TryParse(String, out DateTime) becomes
Post by Daniel Krause
d = DateTime(0) # just a dummy to call the method on
d2 = DateTime(0) # another dummy to satisfy the out parameter (could
be the same instance, d)
# d3 is were the result is passed out
Post by Daniel Krause
result, d3 = d.TryParse("2013/01/22", d2)
d3.ToString()
u'1/22/2013 12:00:00 AM'
Post by Daniel Krause
# this is the same behavior as iPy
Here you've given the type (class)
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_**POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_**FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.**BitmapSource'>
bitmapsrc = BitmapSource() # or something to that effect.
Post by Daniel Krause
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
cam = xiCam()
cam.OpenDevice(0)
cam.SetParam(PRM.BUFFER_**POLICY, BUFF_POLICY.SAFE)
cam.SetParam(PRM.IMAGE_DATA_**FORMAT, IMG_FORMAT.MONO8)
cam.StartAcquisition()
timeout = 1000
bitmapsrc = cam.GetImage(bitmapsrc, timeout)
cam.StopAcquisition()
##
<class 'System.Windows.Media.Imaging.**BitmapSource'>
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...