是否可以将byte []转换为bitmapsource?

时间:2014-09-02 15:13:12

标签: c# bitmap bytearray bitmapsource

到目前为止,我有:

using (MemoryStream ms = new MemoryStream(imageData))
{
     Bitmap img = (Bitmap)Image.FromStream(ms);
}

然后我想我能以某种方式从img获得一个BitmapSource吗?如果这是完全错误的,请随时纠正我。

2 个答案:

答案 0 :(得分:0)

尝试使用BitmapSource.Create()。但是你需要先创建调色板:

var colors = new List<Color>();
colors.Add(Colors.Red);
colors.Add(Colors.Blue);
colors.Add(Colors.Green);
var palette = new BitmapPalette(colors);

答案 1 :(得分:0)

获得最简单的解决方案:

using (MemoryStream ms = new MemoryStream(imageData))
{
    BitmapDecoder bitmapDecoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
    BitmapSource photo = new WriteableBitmap(bitmapDecoder.Frames.Single());
}

Haven尚未对其进行测试,但我的代码取自:Here