silverlight中如何将BitmapImage转化为Stream或byte数组?

时间:2022-04-23
本文章向大家介绍silverlight中如何将BitmapImage转化为Stream或byte数组?,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上一篇"base64编码在silverlight中的使用"里已经提到WriteableBitmap对象可以借助FluxJpeg转化为base64字符串,而WriteableBitmap又能从BitmapSource直接构造,so ... 问题解决了

先将BitmapImage转化为WriteableBitmap,然后得到base64字符串,然后可以得到base64的byte[]数组,再然后您可以把byte[]变成Stream

关键代码:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1 

WriteableBitmap wb = new WriteableBitmap(img.Source as BitmapSource);//将Image对象转换为WriteableBitmap

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;