2014-01-10 57 views
0

CameraCaptureTask拍攝圖像後,應將其上傳到服務器。上傳的服務器端JPG似乎有正確的文件大小,但已損壞。此外,imageBuffer似乎將所有字節設置爲0.任何有關下面代碼錯誤的想法?在silverlight中上傳時JPG損壞

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(); 
    }     
} 
+0

「SaveJpeg」是否將流重新定位回位置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(); 
    }     
} 
+0

現在完美工作,謝謝! – user3153110

+0

請鍵入文字描述你改變了什麼,這樣未來這個問題的訪問者將不必基本區分這兩段代碼來找出爲什麼一個工作而另一個不工作。 –

+1

設置ms.Position = 0後,它開始正確工作,將流重新定位爲0。 – user3153110