Marcel
Marcel

Reputation: 2158

How to get a C# Image over to Monotouch

I am connecting from monotouch through wcf to my windows c# host and wish to stream an image back to monotouch so I can display this.

My image is held in an System.Drawing.Image object which is not available in monotouch (it uses UIImage).

I was hoping to convert the Image on the host to a string like so:

        Image im = Image.FromFile(path);
        MemoryStream ms = new MemoryStream();
        im.Save(ms, im.RawFormat);
        byte[] array = ms.ToArray();
        return Convert.ToBase64String(array);

And then using the opposite within MonoTouch to get my Image back again:

        byte[] array = Convert.FromBase64String(imageString);
        Image image = Image.FromStream(new MemoryStream(array));
        return image;

This works fine in a 'pure' .net environment but monotouch doesn't recognise the Image object so it fails on that end. How can I convert the byte[] back into a UIImage?

I tried things like this:

  UIImage img = (UIImage)UIImage.FromObject(bytes);

to no avail...

Any help much appreciated!

Upvotes: 2

Views: 1374

Answers (1)

jonathanpeppers
jonathanpeppers

Reputation: 26495

Instead of passing im.RawFormat, try using one of:

  • ImageFormat.Bmp
  • ImageFormat.Png

Depending on which format your images usually are. These are in System.Drawing.Imaging, see here.

This might cause trouble for other client applications, I would recommend having the client send something to determine what format the server returns.

Upvotes: 3

Related Questions