我可以将byte []转换为图像:
byte[] myByteArray = ...; // ByteArray to be converted
MemoryStream ms = new MemoryStream(my);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
Image img = new Image();
img.Source = bi;
但是我无法将图像转换回字节[]!
我在Internet上找到了一种适用于WPF的解决方案:
var bmp = img.Source as BitmapImage;
int height = bmp.PixelHeight;
int width = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
Silverlight库是如此之小,以至于BitmapImage类没有名为Format的属性!
有谁能解决我的问题的主意。
我在互联网上搜索了很长时间,以找到解决方案,但是没有解决方案,它可以在Silverlight中使用!
谢谢!
最佳答案
(您缺少的每像素位数方法只是详细说明了如何按像素存储颜色信息)
正如安东尼建议的那样,WriteableBitmap将是最简单的方法- checkout http://kodierer.blogspot.com/2009/11/convert-encode-and-decode-silverlight.html以获取一种方法来获取argb字节数组:
public static byte[] ToByteArray(this WriteableBitmap bmp)
{
// Init buffer
int w = bmp.PixelWidth;
int h = bmp.PixelHeight;
int[] p = bmp.Pixels;
int len = p.Length;
byte[] result = new byte[4 * w * h];
// Copy pixels to buffer
for (int i = 0, j = 0; i < len; i++, j += 4)
{
int color = p[i];
result[j + 0] = (byte)(color >> 24); // A
result[j + 1] = (byte)(color >> 16); // R
result[j + 2] = (byte)(color >> 8); // G
result[j + 3] = (byte)(color); // B
}
return result;
}
关于silverlight - Silverlight:图像到字节[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1958470/