0

So I wrote my scraper and passed a C# class (card) into IronPython, which it then happily loaded with what I think is binary image data into a byte[] like so:

imageurl = "http://blabla.com/Image.ashx?id=" + card.Id + "&type=card"
imageresult = urllib2.urlopen(imageurl).read()
if imageresult == '':
    print 'Could not find image for ' + card.Title
card.AddImage(imageresult) # AddImage(byte[])

I then persist this and pull it from the database with NHibernate and tried to pull it back with this on my MVC front end:

var ms = new MemoryStream(card.Image);
var image = Image.FromStream(ms); // ***Parameter is not valid.***

Had I just written this out to a file instead of a C# object with Python, I'm fairly sure this would work. My question is, is there a good way to tell what the conversions will look like between IronPython and CLR data types? My binary sucks, I'm just not real sure what to do about it, in this case.

Daniel Harvey
  • 381
  • 5
  • 16

2 Answers2

1

It looks like byte[] cannot be persisted. Check out this question:

NHibernate MappingException: no persister for byte[]

it may be that you're not getting the same message because it's happening in IronPython not in C#, due to C#'s type checking.

Here's another link a possible solution by serializing byte[] as ASCII:

http://guildsocial.web703.discountasp.net/dasblogce/2009/04/03/NHibernateMappingToBinaryData.aspx

Here's a snippet from that post:

return new ASCIIEncoding().GetString(bytes);

public static string ConvertByteArrayToString(byte[] bytes)
{
    try
    {
        return new ASCIIEncoding().GetString(bytes);
    }
    catch (Exception)
    {
        return "";
    }

}
Community
  • 1
  • 1
0

DrNewman hit the nail basically on the head. The problem was the string format it ended up in coming out of iron python. Rather than deal with converting it to the correct format in python then brining it back with C#, I just elected to hand that part off to the C#. What I ended up doing was calling AddImage from iron python and just passing the url. Then doing the last step of the scraping (the image) with C#

Daniel Harvey
  • 381
  • 5
  • 16