JPG在Silverlight上传时损坏了

时间:2014-01-10 20:40:11

标签: c# silverlight memorystream bitmapimage writeablebitmap

使用CameraCaptureTask拍摄图像后,应将其上传到服务器。服务器端上传的JPG似乎具有正确的文件大小但已损坏。同样imageBuffer似乎将所有字节都设置为0.任何想法下面的代码有什么问题?

if (bitmapImage != null) {
    // create WriteableBitmap object from captured BitmapImage
    WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);

    using (MemoryStream ms = new MemoryStream())
    {
        writeableBitmap.SaveJpeg(ms, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);

        imageBuffer = new byte[ms.Length];
        ms.Read(imageBuffer, 0, imageBuffer.Length);
        ms.Dispose();
    }                
}

1 个答案:

答案 0 :(得分:0)

SaveJpeg方法更改流的当前位置。要正确保存流的内容,您需要从头开始读取它(即将位置设置为0)。试试这个:

if (bitmapImage != null) {
    // create WriteableBitmap object from captured BitmapImage
    WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);

    using (MemoryStream ms = new MemoryStream())
    {
        writeableBitmap.SaveJpeg(ms, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);

        ms.Position = 0;
        imageBuffer = new byte[ms.Length];
        ms.Read(imageBuffer, 0, imageBuffer.Length);
        ms.Dispose();
    }                
}