How do I set a WPF Image's Source to a bytearray in C# code behind?

青春壹個敷衍的年華 提交于 2019-12-08 04:26:25

Your stride calculation is wrong. It is the number of full bytes per scan line, and should therefore be calculated like this:

var format = PixelFormats.Bgr24;
var stride = (width * format.BitsPerPixel + 7) / 8;

var imageSrc = BitmapSource.Create(
    width, height, 96d, 96d, format, null, imageData, stride);

Of course you also have to make sure that you use the correct image size, i.e. that the width and height values actually correspond with the data in imageBuffer.

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