使用EXIF数据将图像上载到服务器

时间:2014-06-04 22:02:12

标签: c# image windows-phone-8 exif

我没有尝试在设备或服务器上读取EXIF数据,但数据需要存在于服务器上。

我目前正在将图像发送到服务器,方法是将其转换为byte[],然后转换为base64 Convert.ToBase64(byte[])并使用JSON发送 - 请参阅下面的代码。

但是当我在另一端收到文件时,它没有任何EXIF数据。如果我使用假SD卡从仿真器上取下图像,则EXIF数据存在。

有没有人知道如何上传图像然后在另一端重建它以便EXIF数据保持不变?

图像到base64到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));

1 个答案:

答案 0 :(得分:1)

我必须完全更改我的代码,以删除与Bitmaps或其他格式的任何连接,并通过它使用byte[]。见代码:

在应用上:

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);

在服务器上:

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);
}