[IronPython] Convert between python array and .NET Array

Curt Hagenlocher curt at hagenlocher.org
Sat Jun 12 22:27:59 PDT 2010


It looks like the automatic overload resolution may be failing -- at least,
it's worth trying to resolve the overload manually. This would be something
like

from System import Array, Byte, IntPtr
Marshal.Copy.Overloads[Array[Byte], int, IntPtr, int](bytes, 0,
bmData.Scan0, total_bytes)


On Sat, Jun 12, 2010 at 10:07 PM, Marcel <marcel.vandendungen at gmail.com>wrote:

> 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
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20100612/a0166123/attachment.htm>


More information about the Users mailing list