Discussion:
[Python.NET] How to use BitmapSource from System.Windows.Media.Imaging ?
Daniel Krause
2013-01-20 19:40:49 UTC
Permalink
I want to use BitmapSource from System.Windows.Media.Imaging
(Documentation here:
http://msdn.microsoft.com/de-de/library/system.windows.media.imaging.bitmapsource(v=vs.100).aspx)

It would be great if someone could help me with this, as I do not have yet
much experience with python for .NET, and none at all with .NET itself.

My test script:

import clr
clr.AddReference("System.Windows")
import System.Windows
bitmap = System.Windows.Media.Imaging.BitmapSource()

The interpreter info is:
Traceback (most recent call last):
File "C:\Users\mdk\workspace\testbitmap.py", line 4, in <module>
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
AttributeError: Media

I tried also:

import clr
clr.AddReference("System.Windows.Media")
import System.Windows
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()


Traceback (most recent call last):
File "C:\Users\mdk\workspace\testbitmap.py",
line 2, in <module>
clr.AddReference("System.Windows.Media")
System.IO.FileNotFoundException: Unable to find assembly
'System.Windows.Media'.
bei Python.Runtime.CLRModule.AddReference(String name)

And I also tried:

import clr
clr.AddReference("System.Windows")
from System.Windows import Media
bitmapsrc = Media.Imaging.BitmapSource()

Traceback (most recent call last):
File
"C:\Users\mdk\workspace\lr_control\src\lr_control\camera\testbitmap.py",
line 3, in <module>
from System.Windows import Media
ImportError: cannot import name Media
Daniel Krause
2013-01-21 19:10:57 UTC
Permalink
Hi Jojo,

thanks for your help.

The following code is running (64bit-Windows):

import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc

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

Best regards
Daniel
Hi,
In C#, the System.Windows.Media can just be shown if you add the
"PresentationCore" as part of your reference. The path of
PresentationCore.dll is in C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\v3.0 if you are using Visual Studio
2008. I think that might help if you add that as your reference in
clr.AddReference. I found some explanation in here -
http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/fdfff143-c1ae-41cd-bbeb-8ff6c1c879ec
I believe this will help you.
HTH
Thanks and best regards,
Jojo Maquiling
On Mon, Jan 21, 2013 at 3:40 AM, Daniel Krause
Post by Daniel Krause
I want to use BitmapSource from System.Windows.Media.Imaging
http://msdn.microsoft.com/de-de/library/system.windows.media.imaging.bitmapsource(v=vs.100).aspx
Post by Daniel Krause
)
It would be great if someone could help me with this, as I do not have
yet
Post by Daniel Krause
much experience with python for .NET, and none at all with .NET itself.
import clr
clr.AddReference("System.Windows")
import System.Windows
bitmap = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py", line 4, in <module>
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
AttributeError: Media
import clr
clr.AddReference("System.Windows.Media")
import System.Windows
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py",
line 2, in <module>
clr.AddReference("System.Windows.Media")
System.IO.FileNotFoundException: Unable to find assembly
'System.Windows.Media'.
bei Python.Runtime.CLRModule.AddReference(String name)
import clr
clr.AddReference("System.Windows")
from System.Windows import Media
bitmapsrc = Media.Imaging.BitmapSource()
File
"C:\Users\mdk\workspace\lr_control\src\lr_control\camera\testbitmap.py",
line 3, in <module>
from System.Windows import Media
ImportError: cannot import name Media
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-01-21 22:26:42 UTC
Permalink
You should be wary of that hard coded path.

My best guess as to why the namespace is not provided is that the .net framework that is being loaded is pre .net 3.0. I think python.net compiles to 2.0 by default but I'd need to check that.

That Windows.Media.Imaging namespace only became available in .net 3.0 onward.

To do this properly, you'd likely be looking to compile or use python.net in a mode that brings in the standard libs for .net 3.0 or higher. Then it should just be available in the GAC for that .net version.

Distributing a python script with .net dependencies can get rather ugly.

-brad
Post by Daniel Krause
Hi Jojo,
thanks for your help.
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
<class 'System.Windows.Media.Imaging.BitmapSource'>
Best regards
Daniel
Hi,
In C#, the System.Windows.Media can just be shown if you add the
"PresentationCore" as part of your reference. The path of
PresentationCore.dll is in C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\v3.0 if you are using Visual Studio
2008. I think that might help if you add that as your reference in
clr.AddReference. I found some explanation in here -
http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/fdfff143-c1ae-41cd-bbeb-8ff6c1c879ec
I believe this will help you.
HTH
Thanks and best regards,
Jojo Maquiling
On Mon, Jan 21, 2013 at 3:40 AM, Daniel Krause
Post by Daniel Krause
I want to use BitmapSource from System.Windows.Media.Imaging
http://msdn.microsoft.com/de-de/library/system.windows.media.imaging.bitmapsource(v=vs.100).aspx
)
It would be great if someone could help me with this, as I do not have yet
much experience with python for .NET, and none at all with .NET itself.
import clr
clr.AddReference("System.Windows")
import System.Windows
bitmap = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py", line 4, in <module>
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
AttributeError: Media
import clr
clr.AddReference("System.Windows.Media")
import System.Windows
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py",
line 2, in <module>
clr.AddReference("System.Windows.Media")
System.IO.FileNotFoundException: Unable to find assembly
'System.Windows.Media'.
bei Python.Runtime.CLRModule.AddReference(String name)
import clr
clr.AddReference("System.Windows")
from System.Windows import Media
bitmapsrc = Media.Imaging.BitmapSource()
File
"C:\Users\mdk\workspace\lr_control\src\lr_control\camera\testbitmap.py",
line 3, in <module>
from System.Windows import Media
ImportError: cannot import name Media
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Daniel Krause
2013-01-22 18:33:59 UTC
Permalink
Thanks for the reminder, I will keep the path in mind.

Python for .NET is installed using
pythonnet-2.0dev.clr4.0.win-amd64-py2.7.exe. As I understand it, it uses
.NET 4.0 as default? If that is true, it should work, but I had to add the
path to get the script running.
Post by b***@public.gmane.org
You should be wary of that hard coded path.
My best guess as to why the namespace is not provided is that the .net
framework that is being loaded is pre .net 3.0. I think python.netcompiles to 2.0 by default but I'd need to check that.
That Windows.Media.Imaging namespace only became available in .net 3.0 onward.
To do this properly, you'd likely be looking to compile or use python.netin a mode that brings in the standard libs for .net 3.0 or higher. Then it
should just be available in the GAC for that .net version.
Distributing a python script with .net dependencies can get rather ugly.
-brad
Hi Jojo,
thanks for your help.
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF
")
clr.AddReference("PresentationCore")
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
<class 'System.Windows.Media.Imaging.BitmapSource'>
Best regards
Daniel
Hi,
In C#, the System.Windows.Media can just be shown if you add the
"PresentationCore" as part of your reference. The path of
PresentationCore.dll is in C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\v3.0 if you are using Visual Studio
2008. I think that might help if you add that as your reference in
clr.AddReference. I found some explanation in here -
http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/fdfff143-c1ae-41cd-bbeb-8ff6c1c879ec
I believe this will help you.
HTH
Thanks and best regards,
Jojo Maquiling
On Mon, Jan 21, 2013 at 3:40 AM, Daniel Krause
Post by Daniel Krause
I want to use BitmapSource from System.Windows.Media.Imaging
http://msdn.microsoft.com/de-de/library/system.windows.media.imaging.bitmapsource(v=vs.100).aspx
Post by Daniel Krause
)
It would be great if someone could help me with this, as I do not have
yet
Post by Daniel Krause
much experience with python for .NET, and none at all with .NET itself.
import clr
clr.AddReference("System.Windows")
import System.Windows
bitmap = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py", line 4, in <module>
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
AttributeError: Media
import clr
clr.AddReference("System.Windows.Media")
import System.Windows
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py",
line 2, in <module>
clr.AddReference("System.Windows.Media")
System.IO.FileNotFoundException: Unable to find assembly
'System.Windows.Media'.
bei Python.Runtime.CLRModule.AddReference(String name)
import clr
clr.AddReference("System.Windows")
from System.Windows import Media
bitmapsrc = Media.Imaging.BitmapSource()
File
"C:\Users\mdk\workspace\lr_control\src\lr_control\camera\testbitmap.py",
line 3, in <module>
from System.Windows import Media
ImportError: cannot import name Media
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-01-22 20:05:06 UTC
Permalink
As discussed here:

http://stackoverflow.com/questions/8517159/how-to-detect-at-runtime-that-net-version-4-5-currently-running-your-code

Things can get muddled. strictly speaking, using clr4.0 with a framework 2.0 profile would result in the absence of the namespace.

It's probably best to try and confirm, regardless of the filename's suggestion.
Post by Daniel Krause
Thanks for the reminder, I will keep the path in mind.
Python for .NET is installed using pythonnet-2.0dev.clr4.0.win-amd64-py2.7.exe. As I understand it, it uses .NET 4.0 as default? If that is true, it should work, but I had to add the path to get the script running.
You should be wary of that hard coded path.
My best guess as to why the namespace is not provided is that the .net framework that is being loaded is pre .net 3.0. I think python.net compiles to 2.0 by default but I'd need to check that.
That Windows.Media.Imaging namespace only became available in .net 3.0 onward.
To do this properly, you'd likely be looking to compile or use python.net in a mode that brings in the standard libs for .net 3.0 or higher. Then it should just be available in the GAC for that .net version.
Distributing a python script with .net dependencies can get rather ugly.
-brad
Post by Daniel Krause
Hi Jojo,
thanks for your help.
import clr
import sys
sys.path.append("C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WPF")
clr.AddReference("PresentationCore")
from System.Windows.Media.Imaging import BitmapSource
bitmapsrc = BitmapSource
print bitmapsrc
<class 'System.Windows.Media.Imaging.BitmapSource'>
Best regards
Daniel
Hi,
In C#, the System.Windows.Media can just be shown if you add the
"PresentationCore" as part of your reference. The path of
PresentationCore.dll is in C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\v3.0 if you are using Visual Studio
2008. I think that might help if you add that as your reference in
clr.AddReference. I found some explanation in here -
http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/fdfff143-c1ae-41cd-bbeb-8ff6c1c879ec
I believe this will help you.
HTH
Thanks and best regards,
Jojo Maquiling
On Mon, Jan 21, 2013 at 3:40 AM, Daniel Krause
Post by Daniel Krause
I want to use BitmapSource from System.Windows.Media.Imaging
http://msdn.microsoft.com/de-de/library/system.windows.media.imaging.bitmapsource(v=vs.100).aspx
)
It would be great if someone could help me with this, as I do not have yet
much experience with python for .NET, and none at all with .NET itself.
import clr
clr.AddReference("System.Windows")
import System.Windows
bitmap = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py", line 4, in <module>
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
AttributeError: Media
import clr
clr.AddReference("System.Windows.Media")
import System.Windows
bitmapsrc = System.Windows.Media.Imaging.BitmapSource()
File "C:\Users\mdk\workspace\testbitmap.py",
line 2, in <module>
clr.AddReference("System.Windows.Media")
System.IO.FileNotFoundException: Unable to find assembly
'System.Windows.Media'.
bei Python.Runtime.CLRModule.AddReference(String name)
import clr
clr.AddReference("System.Windows")
from System.Windows import Media
bitmapsrc = Media.Imaging.BitmapSource()
File
"C:\Users\mdk\workspace\lr_control\src\lr_control\camera\testbitmap.py",
line 3, in <module>
from System.Windows import Media
ImportError: cannot import name Media
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...