Search code examples
c#imagewindows-phone-8exif

Upload Image to server with EXIF data


I'm not trying to read the EXIF data on either the device or server, but the data needs to be present on the server.

I am currently sending the image to the server via converting it to a byte[] and then to base64 Convert.ToBase64(byte[]) and sending it using JSON - see code below.

But when I get the file at the other end, it doesn't have any EXIF data. If I get the image off the emulator with the fake SD card, the EXIF data exists.

Does anyone know how to upload the image and then reconstruct it at the other end so that the EXIF data stays intact?

Image to bytes to base64 to JSON

BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
image = ResizeImage(image);
byte[] imageBytes;
using (MemoryStream ms = new MemoryStream())
{
    WriteableBitmap btmMap = new WriteableBitmap(image);
    System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, Image.PixelHeight, 0, 100);
    image = null;
    imageBytes = ms.ToArray();
 }
 Base64Image imagestring = new Base64Image();
 imagestring.imagestring = Convert.ToBase64String(imageBytes);
 string json = JsonConvert.SerializeObject(imagestring);

JSON To base64 to byte[] to image

using (StreamReader sr = new StreamReader(inputStream))
{
    postData = sr.ReadToEnd();
}
JavaScriptSerializer deserializer = new JavaScriptSerializer();
Dictionary<string, object> jsonObjects = (Dictionary<string, object>)deserializer.DeserializeObject(postData);
string base64image = jsonObjects["imagestring"].ToString();
byte[] imagebytes = Convert.FromBase64String(base64image);
BitmapImage bitmapImage = new BitmapImage();
MemoryStream ms = new MemoryStream(imagebytes);
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnDemand;
bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage.Rotation = Rotation.Rotate0;
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
bitmapImage.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap wBmp = new WriteableBitmap(bitmapImage);
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wBmp));

Solution

  • I had to change my code completely to cut out any connection to Bitmaps or other formats, and just used byte[] through it. See code:

    On the app:

    ImageData imagedata = new ImageData();
    byte[] imagebytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imagebytes, 0, int.Parse(e.ChosenPhoto.Length.ToString()));
    imagedata.imagestring = Convert.ToBase64String(imagebytes);
    string json = JsonConvert.SerializeObject(imagedata);
    

    At the server:

    JavaScriptSerializer deserializer = new JavaScriptSerializer();
    deserializer.MaxJsonLength = 50000000;
    Dictionary<string, object> jsonObjects = (Dictionary<string, object>)deserializer.DeserializeObject(postData);
    string base64image = jsonObjects["imagestring"].ToString();              
    byte[] imagebytes = Convert.FromBase64String(base64image);
    Guid imagename = Guid.NewGuid();
    if (!Directory.Exists(EM.ImagePath))
       Directory.CreateDirectory(EM.ImagePath);
    using (FileStream sw = new FileStream(EM.ImagePath + imagename + ".jpg", FileMode.CreateNew))
    {
       sw.Write(imagebytes, 0, imagebytes.Length);
    }