[IronPython] Convert between python array and .NET Array

Marcel marcel.vandendungen at gmail.com
Sun Jun 13 07:07:56 CEST 2010


I think I simplified my example a little too far. I'm trying to
display a Bitmap from bytes generated in Python code.

        bytes = array.array('c') # tried 'B' also
        # generate bitmap bytes ...

        bitmap = Bitmap(width, height, PixelFormat.Format24bppRgb)

        bmData = bitmap.LockBits(Rectangle(0, 0, bitmap.Width,
bitmap.Height),
          ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)

        total_bytes = (bmData.Stride) * bmData.Height

        Marshal.Copy(bytes, 0, bmData.Scan0, total_bytes);

        bitmap.UnlockBits(bmData)

The IronPython exception I'm getting is:
TypeError: expected IntPtr, got array

Casting the bytes array to IntPtr changed the error into:
TypeError: expected int, got array

I'm using IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4927

Any idea?

Thanks,
-- Marcel

On Jun 11, 11:27 pm, David Escobar <davidesco... at ieee.org> wrote:
> This worked for me (based on the code you provided):
>
> import clr
> from System import Array
>
> dest = Array[str](bytes)
>
> On Fri, Jun 11, 2010 at 9:51 PM, Marcel <marcel.vandendun... at gmail.com>wrote:
>
> > I have a python method that returns a Python byte array.array('c').
>
> > Now, I want to copy this array using
> > System.Runtime.InteropServices.Marshal.Copy. This method however
> > expects a .NET Array.
>
> > import array
> > from System.Runtime.InteropServices import Marshal
>
> > bytes = array.array('c')
> > bytes.append('a')
> > bytes.append('b')
> > bytes.append('c')
> > Marshal.Copy(bytes, dest, 0, 3)
>
> > Is there a way to make this work without copying the data? If not, how
> > do I convert the data in the Python array to the .NET array?
>
> > Thanks,
> > -- Marcel
> > _______________________________________________
> > Users mailing list
> > Us... at lists.ironpython.com
> >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
> _______________________________________________
> Users mailing list
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list