views:

247

answers:

2

I have a sql image type field coming back in an EDM (Entity Data Model) .NET service call. The image field is stored as binary in sql server 2008. However, I'm not sure what is happening to the image data, or what type of encoding. And since the webservices are generated behind the scenes, I can’t figure out if the service is doing something funky to the image binary data before sending it across the wire.

Anyone done anything of the sort?

I've tried base64 decoding the image, tried setting the NSURLConnection request encoding to NSUTF8StringEncoding, all with no luck

Here's a sample packet sniff:

HTTP/1.1 200 OK Date: Tue, 09 Mar 2010 19:41:13 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 DataServiceVersion: 1.0; Cache-Control: no-cache Content-Type: application/json;charset=utf-8 Content-Length: 41809

{ "d" : [ { "__metadata": { "uri": "http://11.111.11.111:8002/domain/myservice.svc/StudentByGroupID(createDate=datetime\'2009-12-15T11%3A16%3A18.14\',firstName=\'Niansong\',groupId=1,isActive=1,lastName=\'Liu\',studentGroupId=28,studentId=6)", "type": "MyModel.StudentByGroupID" }, "studentId": 6, "firstName": "Niansong", "lastName": "Liu", "createDate": "\/Date(1260875778140)\/", "updateDate": null, "isActive": 1, "image": "AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABVTeXN0ZW0uRHJhd2luZy5CaXRtYXABAAAABERhdGEHAgIAAAAJAwAAAA8DAAAABA0AAAL/2P/gABBKRklGAAEBAAABAAEAAP/bAEMACQYHCAcGCQgHCAoKCQsNFg8NDAwNGxQVEBYgHSIiIB0fHyQoNCwkJjEnHx8tPS0xNTc6OjojKz9EPzhDNDk6N//bAEMBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGcAfwMBIgACEQEDEQH/xAAfA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YtqkQBILQqP0asqOb9xKp7xsMfhV7TJUXULJmIAaLknHo1claGjOmlLVEjX943L3cxPvIaj82VvvSuR7saE0rVn+7pt4fpA3+FTr4f15/u6TefjERVciGplZpJCp+djxx8xr1iytxHsVuEjVYwPXaAP6V5zF4Y8QEgnSbgAHncAP616Nc3UWnWtxql02FjJ8oepzxitKcUrmkZXWhyXjHUS+rT2a4KTFVYKMkMOB36YxWAql7iSJn3LEoTAweBx69wKl3Mbk6heuI5ppCI1Pbgk/p/MVSgYs905+bL4Xdz3z/St6CesjPFOzUew43NzHPIjTsQRx8oAOOmOKoSXMrTBNxx34q9MqlVYcsQRkdsZ/wrMJ2yMSMN27jrWzVjjbK95PIxVAx3Kepx7Yqhc7/MPXOKutjyyx5JJFV2AMse0DoRxUEM6qBZNTubeaQ7ooERBz14BP8AMVb8XfvLFgB9zafphgP61n+D3Z4pIgfvjeM9iOD/AErT1rM2mzqVw/lsDn2GQf0rl2keuvfot9zic0oOGpmaaSwzjk1ueVctJKBkM3UYqe2mZhaMDjEZwfx/+vVS2+yMw+2faQf9jH+FaUcGjbFHmaiNowBsBArKpbZo1p36M2JPFGuvw+qXOD285h/Kqr61qT58zUp29mmc/wBaKKzUUza9iJLq4nkWNJ9zyMFUHPJPA6n3rvtSkF7dRfahm3iiMsUI6Y/vn3Y9B2HXnoUU+RRWh04eTle5xUpe4vbd52+d2LRgdEABOfxI/KhFkhXI+bcOSD3/AP1Giiuql8JyV9ZsJtqxY2HeWJJfnnOeBkgdaz7hFMucZ5+X8/8A9VFFaNHOUpQqx7CD6n8qrMpMg55yQAfWiioJOg8JsXtg8OBcQyMy56OuRkH8/wBa3NRkWZEZOFkJ3KR/n1oorm+0etR/hI4DpgUoIoorY8ok2hsAitzQRGtpGZEBHIL7FJyMjv1oorCt8BtR+I//2Qs=", "studentGroupId": 28, "groupId": 1 } ] }

+1  A: 

That return is a JSON payload (application/json) with a base64 encoded image on a UTF-8 charset. If you don't want to parse JSON on your own, there is json-framework you could try, or TouchJSON.

Remus Rusanu
I realize its json, and I can parse that fine. The problem is when I try to create a UIImage from the string of image data:UIImage* image = [UIImage imageWithData:[NSData dataWithBase64EncodedString:pictureDataString]];where dataWithBase64EncodedString is a decoding method found on the web, it doesn't work. The UIImage is always nil, meaning the UIImage class couldnt convert the decoded string to any image data. I've tried encoding and then decoding a local UIImage and that seems to work fine, just can't get the data from the json packet to decode to anything.
Bryan
Take the base64 encoded text and go to online decoder, like http://www.motobit.com/util/base64-decoder-encoder.asp. With the text in your post, the decoded result is binary that contains this string embeded `System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a` which tells me its a serialized CLR System.Drawing, not a valid image format (.png, .jpg, .gif or even .bmp). Apparently the EDMX is intended to be consumed only by another CLR app... which does not suprise me at least...
Remus Rusanu
BTW, probably this is isn't EDMX at fault here, most likely the original content in the database is saved as a serialized Drawing instead of a proper image format like `Image.Save(ImageFormat.Png)` or something similar, so you'll have to track down the app that saves the image in the db and fix that.
Remus Rusanu
Awesome, good catch. Thanks and I'll let ya know what I find
Bryan
A: 

Here is the .net image related code:

Codes for saving images into database
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath);// “imgPath” is the file path of a temporary image uploaded by a user.
student.image = new System.Data.Linq.Binary(Utility.Convertor.ImageToByteArray(img));
img.Dispose();
Codes for writing image bytes from server to client
System.Drawing.Image img = Utility.Convertor.ByteArrayToImage(stu.image.ToArray());
Response.ContentType = ("image/" + Utility.Gadget.GetImageFormat(img));
MemoryStream ms = new MemoryStream();
img.Save(ms, img.RawFormat);
byte[] result = ms.ToArray();
ms.Close();
ms.Dispose();
Response.BinaryWrite(result);

Below are encapsulated methods for storing / restoring students’ images:
Serializer for serializing a image to bytes
/// 
/// Convert a System.Drawing.Image object to byte array formatted by System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.
/// 
/// 
/// 
public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
      BinaryFormatter sf = new BinaryFormatter();
      MemoryStream ms = new MemoryStream();
      sf.Serialize(ms, imageIn);
      byte[] result = ms.ToArray();
      ms.Close();
      ms.Dispose();
      return result;
}
Deserializer for restoring a image from bytes
/// 
/// Convert a byte array formatted by System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to a System.Drawing.Image object.
/// 
/// 
/// 
public static System.Drawing.Image ByteArrayToImage(byte[] byteArrayIn)
{
       BinaryFormatter sf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream(byteArrayIn);
       System.Drawing.Image returnImage = (System.Drawing.Image)sf.Deserialize(ms);
       ms.Close();
       ms.Dispose();
       return returnImage;
}
Bryan