您的位置:首页 > 其它

BitMapImage与byte[]之间的转换

2011-07-20 15:07 309 查看
/article/6243906.html

byte[]转换为bitmapimage:
public static bitmapimage bytearraytobitmapimage(byte[] bytearray)
{
bitmapimage bmp = null;

try
{
bmp = new bitmapimage();
bmp.begininit();
bmp.streamsource = new memorystream(bytearray);
bmp.endinit();
}
catch
{
bmp = null;
}

return bmp;
}
bitmapimage转换为byte[]:
public static byte[] bitmapimagetobytearray(bitmapimage bmp)
{
byte[] bytearray = null;

try
{
stream smarket = bmp.streamsource;

if (smarket != null && smarket.length > 0)
{
//很重要,因为position经常位于stream的末尾,导致下面读取到的长度为0。
smarket.position = 0;

using (binaryreader br = new binaryreader(smarket))
{
bytearray = br.readbytes((int)smarket.length);
}
}
}
catch
{
//other exception handling
}

return bytearray;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: