bitmapimage与byte[]相互转换 --转
2010-05-15 13:31
337 查看
作者:飘遥(Zhenxing Zhou) tags:WPF Image byte WPF BitmapImage 有时要实现bitmapimage与byte[]相互转换,这里实现两个静态方法,直接调用即可。
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;
}
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;
}
相关文章推荐
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- Image、Byte[]、Bitmap相互转换
- WPF中bitmapimage与byte[]格式相互转换
- WPF Image控件 Source: Byte[] ,BitmapImage 相互转换
- WPF Image控件 Source: Byte[] ,BitmapImage 相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- WriteableBitmap/BitmapImage/MemoryStream/byte[]相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- BitmapImage与byte[]相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换
- Byte[]、Image、Bitmap 之间的相互转换