Is it possible to convert a byte[] to a bitmapsource?

你说的曾经没有我的故事 提交于 2019-12-13 05:14:20

问题


So far I have:

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

And then I would imagine I would be able to get a BitmapSource from img somehow? If this is totally wrong please feel free to correct me.


回答1:


Try using BitmapSource.Create(). But you need to create pallete first:

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



回答2:


Got the easiest solution:

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

Haven't tested it yet but my code was taken from: Here



来源:https://stackoverflow.com/questions/25626770/is-it-possible-to-convert-a-byte-to-a-bitmapsource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!