天天看點

silverlight中如何将BitmapImage轉化為Stream或byte數組?

上一篇"base64編碼在silverlight中的使用"裡已經提到WriteableBitmap對象可以借助FluxJpeg轉化為base64字元串,而WriteableBitmap又能從BitmapSource直接構造,so ... 問題解決了

先将BitmapImage轉化為WriteableBitmap,然後得到base64字元串,然後可以得到base64的byte[]數組,再然後您可以把byte[]變成Stream

關鍵代碼:

3 WriteableBitmap wb = new WriteableBitmap(img.Source as BitmapSource);//将Image對象轉換為WriteableBitmap

5 byte[] b = Convert.FromBase64String(GetBase64Image(wb));//得到byte數組

将byte[]還原為圖檔:

1 byte[] b = ...//這裡的b為上面生成的base64編碼的byte數組

2 MemoryStream ms = new MemoryStream(b);

3 BitmapImage bitImage = new BitmapImage();

4 bitImage.SetSource(ms);

5 img2.Source = bitImage;

作者:菩提樹下的楊過